简单工厂模式----计算器代码
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace SimpleFactory
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 double sum = 0;
12 Operation oper = null;
13 oper = OperationFactory.createOperate("+");
14 oper.NumberA = 1;
15 oper.NumberB = 2;
16 sum = oper.getResult();
17 Console.WriteLine(sum);
18 Console.ReadLine();
19 }
20 }
21 public abstract class Operation
22 {
23 private double _NumberA;
24
25 public double NumberA
26 {
27 get { return _NumberA; }
28 set { _NumberA = value; }
29 }
30 private double _NumberB;
31
32 public double NumberB
33 {
34 get { return _NumberB; }
35 set { _NumberB = value; }
36 }
37 public virtual double getResult()
38 {
39 double result = 0.0;
40 return result;
41 }
42 }
43 class OprationAdd : Operation
44 {
45 public override double getResult()
46 {
47 double result = 0;
48 result = NumberA + NumberB;
49 return result;
50 }
51 }
52 class OprationSub : Operation
53 {
54 public override double getResult()
55 {
56 double result = 0;
57 result = NumberA - NumberB;
58 return result;
59 }
60 }
61 class OprationMul : Operation
62 {
63 public override double getResult()
64 {
65 double result = 0;
66 result = NumberA * NumberB;
67 return result;
68 }
69 }
70 class OprationDiv : Operation
71 {
72 public override double getResult()
73 {
74 double result = 0;
75 if (NumberB == 0)
76 {
77 throw new Exception("除数不能为零。");
78 }
79 else
80 {
81 result = NumberA / NumberB;
82 }
83 return result;
84 }
85 }
86 public class OperationFactory
87 {
88 public static Operation createOperate(string operate)
89 {
90 Operation oper = null;
91 switch (operate)
92 {
93 case "+":
94 oper = new OprationAdd();
95 break;
96 case "-":
97 oper = new OprationSub();
98 break;
99 case "*":
100 oper = new OprationMul();
101 break;
102 case "/":
103 oper = new OprationDiv();
104 break;
105 }
106 return oper;
107 }
108 }
109
110 }
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace SimpleFactory
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 double sum = 0;
12 Operation oper = null;
13 oper = OperationFactory.createOperate("+");
14 oper.NumberA = 1;
15 oper.NumberB = 2;
16 sum = oper.getResult();
17 Console.WriteLine(sum);
18 Console.ReadLine();
19 }
20 }
21 public abstract class Operation
22 {
23 private double _NumberA;
24
25 public double NumberA
26 {
27 get { return _NumberA; }
28 set { _NumberA = value; }
29 }
30 private double _NumberB;
31
32 public double NumberB
33 {
34 get { return _NumberB; }
35 set { _NumberB = value; }
36 }
37 public virtual double getResult()
38 {
39 double result = 0.0;
40 return result;
41 }
42 }
43 class OprationAdd : Operation
44 {
45 public override double getResult()
46 {
47 double result = 0;
48 result = NumberA + NumberB;
49 return result;
50 }
51 }
52 class OprationSub : Operation
53 {
54 public override double getResult()
55 {
56 double result = 0;
57 result = NumberA - NumberB;
58 return result;
59 }
60 }
61 class OprationMul : Operation
62 {
63 public override double getResult()
64 {
65 double result = 0;
66 result = NumberA * NumberB;
67 return result;
68 }
69 }
70 class OprationDiv : Operation
71 {
72 public override double getResult()
73 {
74 double result = 0;
75 if (NumberB == 0)
76 {
77 throw new Exception("除数不能为零。");
78 }
79 else
80 {
81 result = NumberA / NumberB;
82 }
83 return result;
84 }
85 }
86 public class OperationFactory
87 {
88 public static Operation createOperate(string operate)
89 {
90 Operation oper = null;
91 switch (operate)
92 {
93 case "+":
94 oper = new OprationAdd();
95 break;
96 case "-":
97 oper = new OprationSub();
98 break;
99 case "*":
100 oper = new OprationMul();
101 break;
102 case "/":
103 oper = new OprationDiv();
104 break;
105 }
106 return oper;
107 }
108 }
109
110 }