VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • 设计模式-简单工厂模式

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

复制代码
 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();
        }
    }
复制代码

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


相关教程