码迷,mamicode.com
首页 > 其他好文 > 详细

简单工厂模式

时间:2014-10-21 01:00:36      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   strong   sp   div   

1. 简介

从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。

2. 代码示例

 1 //运算类
 2 public class Operation
 3     {
 4         private double numberA = 0;
 5         private double numberB = 0;
 6 
 7         public double NumberA
 8         {
 9             get { return numberA; }
10             set { numberA = value; }
11         }        
12 
13         public double NumberB
14         {
15             get { return numberB; }
16             set { numberB = value; }
17         }
18 
19         public virtual double GetResult()
20         {
21             double result = 0;
22             return result;
23         }
24 }
25 //加减乘除类
26 class OperationAdd:Operation
27     {
28         public override double GetResult()
29         {
30             double result = 0;
31             result = NumberA + NumberB;
32             return result;
33         }
34 }
35 class OperationSub:Operation
36     {
37         public override double GetResult()
38         {
39             double result = 0;
40             result = NumberA - NumberB;
41             return result;
42         }
43 }
44 class OperationMul:Operation
45     {
46         public override double GetResult()
47         {
48             double result = 0;
49             result = NumberA * NumberB;
50             return result;
51         }
52 }
53 class OperationDiv:Operation
54     {
55         public override double GetResult()
56         {
57             double result = 0;
58             if (NumberB == 0)
59                 throw new Exception("除数不能为零");
60             result = NumberA / NumberB;
61             return result;
62         }
63 }
64 //简单运算工厂类
65 public class OperationFactory
66     {
67         public static Operation createOprate(string operate)
68         {
69             Operation oper = null;
70             switch (operate)
71             {
72                 case "+":
73                     oper = new OperationAdd();
74                     break;
75                 case "-":
76                     oper = new OperationSub();
77                     break;
78                 case "*":
79                     oper = new OperationMul();
80                     break;
81                 case "/":
82                     oper = new OperationDiv();
83                     break;
84             }
85             return oper;
86         }
87     }
88 //Main
89 static void Main(string[] args)
90         {
91             Operation oper;
92             oper = OperationFactory.createOprate("+");
93             oper.NumberA = 1;
94             oper.NumberB = 2;
95             double result = oper.GetResult();
96 
97             Console.ReadKey();
98         }

 

简单工厂模式

标签:style   blog   http   color   io   ar   strong   sp   div   

原文地址:http://www.cnblogs.com/HuoAA/p/4039277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!