生活随笔
收集整理的這篇文章主要介紹了
Asp.Net 设计模式 之 “简单工厂”模式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
public static Operation CreateFactory(string ope)
??????? {
??????????? //實(shí)例化空父類,讓父類指向子類
??????????? Operation op = null;
??????????? switch (ope)
??????????? {
??????????????? case "+":
??????????????????? op = new OperationAdd();//父類指向OperationAdd這個(gè)子類,并調(diào)用子類中的加法
??????????????????? break;
??????????????? case "-":
??????????????????? op = new OperationSub();//父類指向OperationSub這個(gè)子類,并調(diào)用子類中的減法
??????????????????? break;
??????????? }
??????????? return op;
??????? } ?
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace FactoryModel
8 {
9 //====================使用C#,利用簡單工廠模式,實(shí)現(xiàn)簡單的計(jì)算器功能====================
10 //考察時(shí)知識(shí)點(diǎn):面向?qū)ο笕筇匦浴^承、封裝、多態(tài)
11 /// <summary>
12 /// 1.定義父類,同時(shí)也是一個(gè)封裝
13 /// </summary>
14 class Operation
15 {
16 //2.因?yàn)橐屪宇惸軌驅(qū)Ω割愡M(jìn)行訪問,故應(yīng)要將參數(shù)定義為受保護(hù)的變量類型
17 protected int numberA;
18 protected int numberB;
19 //定義屬性(必寫)
20 public int NumberA
21 {
22 get {
return numberA; }
23 set { numberA =
value; }
24 }
25 public int NumberB
26 {
27 get {
return numberB; }
28 set { numberB =
value; }
29 }
30 //3.封裝虛方法,以供子類進(jìn)行重寫
31 public virtual int getResule()
32 {
33 int result =
0;
34 return result;
35 }
36 }
37 /// <summary>
38 /// 4.定義子類,繼承父類,并對(duì)父類進(jìn)行重寫(加法)
39 /// </summary>
40 class OperationAdd : Operation
41 {
42 public override int getResule()
43 {
44 return numberA +
numberB;
45 }
46 }
47 //5.定義子類,繼承父類,并對(duì)父類進(jìn)行重寫(減法)
48 class OperationSub : Operation
49 {
50 public override int getResule()
51 {
52 return numberA -
numberB;
53 }
54 }
55 //6.創(chuàng)建簡單工廠模式
56 class Factory
57 {
58 /// <summary>
59 /// 封裝返回值類型為上面“父類型”——Operation類型的方法
60 /// </summary>
61 /// <param name="ope">ope是指運(yùn)算的類型,如+、-、*、/</param>
62 /// <returns></returns>
63 public static Operation CreateFactory(
string ope)
64 {
65 //實(shí)例化空父類,讓父類指向子類
66 Operation op =
null;
67 switch (ope)
68 {
69 case "+":
70 op =
new OperationAdd();
//父類指向OperationAdd這個(gè)子類,并調(diào)用子類中的加法
71 break;
72 case "-":
73 op =
new OperationSub();
//父類指向OperationSub這個(gè)子類,并調(diào)用子類中的減法
74 break;
75 }
76 return op;
77 }
78 }
79 //7.主函數(shù)中進(jìn)行調(diào)用
80 class Program
81 {
82 static void Main(
string[] args)
83 {
84 //要用哪種運(yùn)算,只需將參數(shù)傳入工廠中的方法中,工廠將會(huì)自動(dòng)調(diào)用相關(guān)的方法,
85 //即(父類指向相應(yīng)的子類,從而調(diào)用相應(yīng)的方法),進(jìn)行相應(yīng)的運(yùn)算
86 Operation op = Factory.CreateFactory(
"+");
87 op.NumberA =
10;
88 op.NumberB =
30;
89 //調(diào)用父類中的方法來獲取結(jié)果
90 int result =
op.getResule();
91 Console.WriteLine(result);
92 Console.ReadKey();
93 }
94 //如果在后續(xù)的編程中仍需要有其他的運(yùn)算,則只需要在子類中加上相應(yīng)的子類,
95 //并在工廠中加上相應(yīng)的case情況即可,這也就是簡單工廠的“利”所在
96 }
97 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/pang951189/p/7805073.html
總結(jié)
以上是生活随笔為你收集整理的Asp.Net 设计模式 之 “简单工厂”模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。