程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 設計模式之簡單工廠模式,設計模式工廠

設計模式之簡單工廠模式,設計模式工廠

編輯:C#入門知識

設計模式之簡單工廠模式,設計模式工廠


  本程序分為三個部分:業務邏輯,頁面邏輯,生產對象的工廠

一、業務邏輯:

  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 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved