设计模式之简单工厂模式

  本程序分为三个部分:业务逻辑,页面逻辑,生产对象的工厂

一、业务逻辑:

  1. Operation类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace SimpleFactory.Caculation_bll
 7 {
 8     /// <summary>
 9     /// 所有具体策略类的父类
10     /// </summary>
11     public class Operation
12     {
13         private double numberA;
14 
15         public double NumberA
16         {
17             get { return numberA; }
18             set { numberA = value; }
19         }
20 
21         private double numberB;
22 
23         public double NumberB
24         {
25             get { return numberB; }
26             set { numberB = value; }
27         }
28 
29         //所有具体策略类的共同方法
30         public virtual double GetResult()
31         {
32             double result = 0;
33             return result;
34         }
35     }
36 }

  2. AddOperation类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace SimpleFactory.Caculation_bll
 7 {
 8     /// <summary>
 9     /// 加法运算的具体实现类
10     /// </summary>
11     class AddOperation:Operation
12     {
13         /// <summary>
14         /// 加法运算的具体实现
15         /// </summary>
16         /// <returns>加法运算结果</returns>
17         public override double GetResult()
18         {
19             return NumberA + NumberB;
20         }
21     }
22 }

   3. SubOperation类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace SimpleFactory.Caculation_bll
 7 {
 8     /// <summary>
 9     /// 减法运算的具体实现类
10     /// </summary>
11     class SubOperation:Operation
12     {
13         /// <summary>
14         /// 减法运算的具体实现
15         /// </summary>
16         /// <returns>减法运算结果</returns>
17         public override double GetResult()
18         {
19             return NumberA - NumberB;
20         }
21     }
22 }

  4. MulOperation类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace SimpleFactory.Caculation_bll
 7 {
 8     /// <summary>
 9     /// 乘法运算的具体实现类
10     /// </summary>
11     class MulOperation:Operation
12     {
13         /// <summary>
14         /// 乘法运算的具体实现
15         /// </summary>
16         /// <returns>乘法运算结果</returns>
17         public override double GetResult()
18         {
19             return NumberA * NumberB;
20         }
21     }
22 }

  5. DivOperation类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using SimpleFactory.Caculation_ui;
 6 
 7 namespace SimpleFactory.Caculation_bll
 8 {
 9     /// <summary>
10     /// 除法运算的具体实现类
11     /// </summary>
12     class DivOperation : Operation
13     {
14         /// <summary>
15         /// 除法运算的具体实现
16         /// </summary>
17         /// <returns>除法运算结果</returns>
18         public override double GetResult()
19         {
20             //排除除数为0的情况
21             if (NumberB==0)
22             {
23                 return 0;
24             }
25             
26             return NumberA / NumberB;
27         }
28     }
29 }

二、页面逻辑

  1. AchieveDouble类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace SimpleFactory.Caculation_ui
 7 {
 8     /// <summary>
 9     /// 获取参与运算的数字
10     /// </summary>
11     public class AchieveDouble
12     {
13         //用于计获取了多少个数字
14         private static int count = 0;
15 
16         /// <summary>
17         /// 获取参与运算的数字
18         /// </summary>
19         /// <returns>参与运算的数字</returns>
20         public static double GetFromConsoleAchieveDouble()
21         {
22             double number;
23             Console.WriteLine("请输入第{0}个数字:",count+1);
24             do
25             {
26                 double temp; //中间变量
27                 try
28                 {
29                     temp = Convert.ToDouble(Console.ReadLine());
30                 }
31                 catch (Exception)
32                 {
33                     Console.WriteLine("请刚刚输入有误,请重新输入第{0}个数字:",count+1);
34                     continue;
35                 }
36 
37                 number = temp;
38                 break;
39 
40             } while (true);
41             count++;
42             return number;
43         }
44     }
45 }

   2. AchieveOperator类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace SimpleFactory.Caculation_ui
 7 {
 8     /// <summary>
 9     /// 获取运算符
10     /// </summary>
11     class AchieveOperator
12     {
13         public static string GetFromConsoleOperator()
14         {
15             string oper;
16             Console.WriteLine("请输入运算字符:");
17             do
18             {
19                 string temp = Console.ReadLine();
20                 if (temp != "+" && temp != "-" && temp != "*" && temp != "/")
21                 {
22                     Console.WriteLine("你刚刚输入有误,请重新输入运算字符:");
23                     continue;
24                 }
25 
26                 oper = temp;
27             } while (true);
28             return oper;
29         }
30     }
31 }

  3. Caculation类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using SimpleFactory.Caculation_bll;
 6 using SimpleFactory.Caculation_factory;
 7 
 8 namespace SimpleFactory.Caculation_ui
 9 {
10     class Caculation
11     {
12         static void Main(string[] args)
13         {
14             double numberA = AchieveDouble.GetFromConsoleAchieveDouble(); //获取第一个数字
15             double numberB = AchieveDouble.GetFromConsoleAchieveDouble(); //获取第二个数字
16             string oper = AchieveOperator.GetFromConsoleOperator(); //获取运算符
17 
18             Operation operation = Factory.GetInstance(oper);
19             operation.NumberA = numberA;
20             operation.NumberB = numberB;
21             double result = operation.GetResult();
22 
23             Console.WriteLine("{0} {1} {2} = {3}",numberA,oper,numberB,result);
24         }
25     }
26 }

三、生成对象的工厂

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using SimpleFactory.Caculation_bll;
 6 
 7 namespace SimpleFactory.Caculation_factory
 8 {
 9     /// <summary>
10     /// 生成对象的工厂
11     /// </summary>
12     public class Factory
13     {
14         /// <summary>
15         /// 生成对象的工厂
16         /// </summary>
17         /// <param name="oper">传输的运算符号</param>
18         /// <returns>参与运算的对象</returns>
19         public static Operation GetInstance(string oper)
20         {
21             Operation operation = null;
22             switch (oper)
23             {   
24                 case "+":
25                     operation = new AddOperation(); //实例加法对象
26                     break;
27                 case "-":
28                     operation = new SubOperation(); //实例减法对象
29                     break;
30                 case "*":
31                     operation = new MulOperation(); //实例乘法对象
32                     break;
33                 case "/":
34                     operation = new DivOperation(); //实例除法对象
35                     break;
36             }
37             return operation;
38         }
39     }
40 }
原文地址:https://www.cnblogs.com/wangchaoyuan/p/4921527.html