zoukankan      html  css  js  c++  java
  • 设计模式---简单工厂

    简单工厂模式:就是店---很多糕点,你需要通过工厂来生成。目的是为了减少代码,先写一个父类,然后让子类继承这个父类,然后在写一个工厂类,根据switch来调用不同的子类。

    测试的时候,直接调用工厂类即可。

    直接上代码:

    下面的算法是一个 计算器的实现:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5.  
    6. namespace DesignDemo
    7. {
    8.     class Operation2
    9.     {
    10.         private double _numberA;
    11.         private double _numberB;
    12.  
    13.         public double numberA
    14.         {
    15.             get {return _numberA;}
    16.             set { _numberA = value; }
    17.         }
    18.  
    19.         public double numberB
    20.         {
    21.             get { return _numberB; }
    22.             set { _numberB = value; }
    23.         }
    24.  
    25.         public virtual double GetResult()
    26.         {
    27.             double result = 0d;
    28.             return result;
    29.         }
    30.     }
    31.  
    32.     class OperationAdd : Operation2
    33.     {
    34.         public override double GetResult()
    35.         {
    36.             double result = 0;
    37.             result = numberA + numberB;
    38.             return result;
    39.         }
    40.     }
    41.  
    42.     class OperationSub : Operation2
    43.     {
    44.         public override double GetResult()
    45.         {
    46.             double result = 0;
    47.             result = numberA - numberB;
    48.             return result;
    49.         }
    50.     }
    51.  
    52.     class OperationMul : Operation2
    53.     {
    54.         public override double GetResult()
    55.         {
    56.             double result = 0;
    57.             result = numberA * numberB;
    58.             return result;
    59.         }
    60.     }
    61.  
    62.     class OperationDiv : Operation2
    63.     {
    64.         public override double GetResult()
    65.         {
    66.             double result = 0;
    67.             if (numberB == 0)
    68.             {
    69.                 throw new Exception("除数不能为0");
    70.             }
    71.             result = numberA / numberB;
    72.             return result;
    73.         }
    74.     }
    75.  
    76.  
    77.     /************************************************************************/
    78.     /* 建立简单工厂模式:减少耦合,把变化的东西封装成类 */
    79.     /************************************************************************/
    80.  
    81.     class OperationFactory
    82.     {
    83.         public static Operation2 createOperate(string operate)
    84.         {
    85.             Operation2 per = null;
    86.             switch (operate)
    87.             {
    88.                 case "+":
    89.                     per = new OperationAdd();
    90.                     break;
    91.                 case "-":
    92.                     per = new OperationSub();
    93.                     break;
    94.                 case "*":
    95.                     per = new OperationMul();
    96.                     break;
    97.                 case "/":
    98.                     per = new OperationDiv();
    99.                     break;
    100.             }
    101.             return per;
    102.         }
    103.     }
    104. }
  • 相关阅读:
    X509Certificate2 Class
    Difference Between Octet and Byte
    雅虎被裁员工写博客表达悲凉心境:晕眩数分钟
    Ruby on rails开发从头来(windows)(三十四) Active Support
    你不得不看的超牛面试(新版)
    Mongrel创建者炮轰Rails社区,并宣称和Rails社区决裂
    Ruby on rails开发从头来(windows)(三十一) Rails的目录结构
    Ruby on rails开发从头来(windows)(三十二) Rails的配置文件
    Ruby on rails开发从头来(windows)(三十) NetBeans IDE
    新闻+旧闻:Ruby 1.9,Ruby on Rails 2.0 ,NetBeans 6.0正式版
  • 原文地址:https://www.cnblogs.com/zhuxuekui/p/4783305.html
Copyright © 2011-2022 走看看