首页 > 学院 > 开发设计 > 正文

设计模式之简单工厂模式

2019-11-14 13:49:36
字体:
来源:转载
供稿:网友

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

一、业务逻辑:

  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 Operation12     {13         PRivate double numberA;14 15         public double NumberA16         {17             get { return numberA; }18             set { numberA = value; }19         }20 21         private double numberB;22 23         public double NumberB24         {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:Operation12     {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:Operation12     {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:Operation12     {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 : Operation13     {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 AchieveDouble12     {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             do25             {26                 double temp; //中间变量27                 try28                 {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 AchieveOperator12     {13         public static string GetFromConsoleOperator()14         {15             string oper;16             Console.WriteLine("请输入运算字符:");17             do18             {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 Caculation11     {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 Factory13     {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 }

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表