首页 > 代码库 > C#基础之简单工厂模式计算器

C#基础之简单工厂模式计算器

类库:.dll文件,使用类库来封装常用的功能,无法单独运行。

abstact class Calculator类{字段Num1,字段Num2,abstract int GetResult()方法}

Add类:Calculator类{override int GetResult()返回Num1+Num2}

Sub类{...}

Multi{...}

divi {...}

工厂类:-------------------------------------------------------------------------------

public class CaculatorFactory
    {
       public static Calc CreateCalc(string oper)
       {
           Calc calc = null;
           switch (oper)
           {
               case "+":
                  calc=new Add();
                   break;
               case "-":
                  calc=new Sub();
                   break;
               case "*":
                   calc=new Multi();
                   break;
               case "/":
                   calc=new Divi();
                   break;
           }
           return calc;
       }
    }

Main()

{

  Calculator calc = CalculatorFactory.CreateCalc(oper);

  calc.Num1=  ;

  calc.Num2=  ;

  int result = calc.GetResult();

}

 

完成

  1 偷的代码-----------------------------------------------------------------------
  2 Add.cs
  3 namespace ProOperation
  4 {
  5     public class Add : Operation
  6     {
  7         public Add(int n1, int n2)
  8             : base(n1, n2)
  9         { }
 10         public override int GetResult()
 11         {
 12             return this.NumberOne + this.NumberTwo;
 13         }
 14     }
 15 }
 16 
 17 Cheng.cs
 18 namespace ProOperation
 19 {
 20     public  class Cheng:Operation
 21     {
 22         public Cheng(int n1, int n2)
 23             : base(n1, n2)
 24         { }
 25 
 26         public override int GetResult()
 27         {
 28             return this.NumberOne * this.NumberTwo;
 29         }
 30     }
 31 }
 32 
 33 Chu.cs
 34 namespace ProOperation
 35 {
 36     public class Chu:Operation
 37     {
 38         public Chu(int n1, int n2)
 39             : base(n1, n2)
 40         { }
 41 
 42         public override int GetResult()
 43         {
 44             return this.NumberOne / this.NumberTwo;
 45         }
 46     }
 47 }
 48 
 49 Sub.cs
 50 namespace ProOperation
 51 {
 52     public class Sub : Operation
 53     {
 54         public Sub(int n1, int n2):base(n1,n2)
 55         { }
 56 
 57         public override int GetResult()
 58         {
 59             return this.NumberOne - this.NumberTwo;
 60         }
 61     }
 62 }
 63 
 64 Operation.cs
 65 namespace ProOperation
 66 {
 67     public abstract class Operation
 68     {
 69         public int NumberOne { get; set; }
 70         public int NumberTwo { get; set; }
 71         public Operation(int n1, int n2)
 72         {
 73             this.NumberOne = n1;
 74             this.NumberTwo = n2;
 75         }
 76         public abstract int GetResult();
 77     }
 78 }
 79 
 80 namespace Calculator
 81 {
 82     class Program
 83     {
 84         static void Main(string[] args)
 85         {
 86             Console.WriteLine("请输入第一个数字");
 87             int n1 = Convert.ToInt32(Console.ReadLine());
 88             Console.WriteLine("请输入第二个数字");
 89             int n2 = Convert.ToInt32(Console.ReadLine());
 90             Console.WriteLine("请输入操作符");
 91             string oper = Console.ReadLine();
 92 
 93             //根据用户输入的操作符 返回算符的父类
 94 
 95             Operation operation = GetOperation(oper, n1, n2);
 96             if (operation != null)
 97             {
 98                 int result = operation.GetResult();
 99                 Console.WriteLine("{0}{1}{2}={3}", n1, oper, n2, result);
100             }
101             else
102             {
103                 Console.WriteLine("没有你想要的运算符");
104             }
105             Console.ReadKey();
106         }
107 
108         static Operation GetOperation(string oper, int n1, int n2)
109         {
110             Operation operation = null;
111             switch (oper)
112             { 
113                 case "+":
114                     operation = new Add(n1, n2);
115                     break;
116                 case "-":
117                     operation = new Sub(n1, n2);
118                     break;
119                 case "*":
120                     operation = new Cheng(n1, n2);
121                     break;
122                 case "/":
123                     operation = new Chu(n1, n2);
124                     break;
125             }
126             return operation;
127         }
128     }
129 }

 

C#基础之简单工厂模式计算器