碳酸苏打水

导航

设计模式-简单工厂模式

构造出一个工厂类,需要使用类型的对象,就返回什么类型的对象
以简单计算器为例
定义运算类,这是个父类,也是抽象类,这个类定义了一个抽象方法

 1 abstract class Operation{
 2         public abstract double GetResult();
 3         private double _num1;
 4         private double _num2;
 5         public double Num1{
 6             get { return _num1; }
 7             set { _num1 = value; }
 8         }
 9         public double Num2{
10             get { return _num2; }
11             set { _num2 = value; }
12         }
13         public Operation(double num1, double num2){
14             this.Num1 = num1;
15             this.Num2 = num2;
16         }
17     }

定义四个子类分别继承这个父类

 1 class OperationForMul : Operation{
 2         public OperationForMul(double num1, double num2) : base(num1, num2) { }
 3         public override double GetResult(){
 4             return this.Num1 * this.Num2;
 5         }
 6     }
 7 
 8 class OperationForSub : Operation{
 9         public OperationForSub(double num1, double num2) : base(num1, num2) { }
10         public override double GetResult(){
11             return this.Num1 - this.Num2;
12         }
13     }
14 
15 class OperationForAdd : Operation{
16         public OperationForAdd(double num1, double num2) : base(num1, num2) { }
17         public override double GetResult(){
18             return this.Num1 + this.Num2;
19         }
20     }
21 
22 class OperationForDiv : Operation{
23         public OperationForDiv(double num1, double num2) : base(num1, num2) { }
24         public override double GetResult(){
25             if (this.Num2 == 0){
26                 return 0;
27             }return this.Num1 / this.Num2;
28         }
29     }

定义一个工厂类

public class OperationFactory{
  public static Operation ReturnOperationItem(string operation, double num1, double num2){
            Operation operationsForOperation = null;
            switch (operation){
                case "+":
                    operationsForOperation = new OperationForAdd(num1, num2);
                    break;
                case "-":
                    operationsForOperation = new OperationForSub(num1, num2);
                    break;
                case "*":
                    operationsForOperation = new OperationForMul(num1, num2);
                    break;
                case "/":
                    operationsForOperation = new OperationForDiv(num1, num2);
                    break;
            }return operationsForOperation;
        }
}

客户端实现

class Program{
        static void Main(string[] args){
            //使用简单工厂模式实现计算器
            Console.WriteLine("输入第一个参与运算的数字");
            double num1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("输入第二个参与运算的数字");
            double num2 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("输入一个运算符");
            string strOperation = Console.ReadLine();
            Console.WriteLine(OperationFactory.ReturnOperationItem(strOperation,num1,num2).GetResult());
            Console.ReadKey();
        }
    }

上面的例子也可以用委托来实现,定义一个和工厂类中方法一样的委托,将具体的方法传进去

posted on 2019-11-02 22:13  碳酸苏打水  阅读(383)  评论(0编辑  收藏  举报