超级简单的工厂模式-温度转换
1 double result = 0;
2
3 double value=Convert.ToDouble(textBox1.Text);
4
5 if (radioButton1.Checked)
6 {
7 result = 9 * value / 5 + 32;
8 }
9 else if (radioButton2.Checked)
10 {
11 result = 5 * (value - 32) / 9;
12 }
13
14 textBox1.Text=Convert.ToString(result);
上面的是原始代码,变成简单工厂模式是这样的:2
3 double value=Convert.ToDouble(textBox1.Text);
4
5 if (radioButton1.Checked)
6 {
7 result = 9 * value / 5 + 32;
8 }
9 else if (radioButton2.Checked)
10 {
11 result = 5 * (value - 32) / 9;
12 }
13
14 textBox1.Text=Convert.ToString(result);
1 public class TemperatureConvert
2 {//这是基类
3 virtual public double Convert(double d)
4 {//公共方法
5 double result = 0;
6 return result;
7 }
8 }
9
10 public class CelsConvert:TemperatureConvert
11 {//转摄氏度子类
12 public override double Convert(double d)
13 {//参数是原始数值
14 double result = 0;
15 result = 9 * d / 5 + 32;//公式
16 return result;
17 }
18 }
19
20 public class FahrConvert:TemperatureConvert
21 {//华氏温度子类
22 public override double Convert(double d)
23 {
24 double result = 0;
25 result = 5 * (d - 32) / 9;//公式
26 return result;
27 }
28 }
29
30 public class TemperatureConvertFactory
31 {//工厂类
32 public static TemperatureConvert GetConvert(string yourNeed)
33 {
34 TemperatureConvert result = null;
35 switch (yourNeed)
36 {//用字符串真是方便,C#真酷
37 case "toFahr"://这里用了字符串作型别码
38 result = new FahrConvert();
39 break;
40
41 case "toCels":
42 result = new CelsConvert();
43 break;
44
45 default:
46 break;
47 }
48 return result;
49 }
50 }
下面是使用方法,并不简单,但是如果添加新的温度子类很方便,呵呵2 {//这是基类
3 virtual public double Convert(double d)
4 {//公共方法
5 double result = 0;
6 return result;
7 }
8 }
9
10 public class CelsConvert:TemperatureConvert
11 {//转摄氏度子类
12 public override double Convert(double d)
13 {//参数是原始数值
14 double result = 0;
15 result = 9 * d / 5 + 32;//公式
16 return result;
17 }
18 }
19
20 public class FahrConvert:TemperatureConvert
21 {//华氏温度子类
22 public override double Convert(double d)
23 {
24 double result = 0;
25 result = 5 * (d - 32) / 9;//公式
26 return result;
27 }
28 }
29
30 public class TemperatureConvertFactory
31 {//工厂类
32 public static TemperatureConvert GetConvert(string yourNeed)
33 {
34 TemperatureConvert result = null;
35 switch (yourNeed)
36 {//用字符串真是方便,C#真酷
37 case "toFahr"://这里用了字符串作型别码
38 result = new FahrConvert();
39 break;
40
41 case "toCels":
42 result = new CelsConvert();
43 break;
44
45 default:
46 break;
47 }
48 return result;
49 }
50 }
1 string typeCodeStr = "toCels";
2 if (radioButton2.Checked)
3 {
4 typeCodeStr = "toFahr";
5 }
6 TemperatureConvert tc = TemperatureConvertFactory.GetConvert(typeCodeStr);
7 double originVal = Convert.ToDouble(textBox1.Text);
8 double resultVal = tc.Convert(originVal);
9 textBox1.Text = Convert.ToString(resultVal);
2 if (radioButton2.Checked)
3 {
4 typeCodeStr = "toFahr";
5 }
6 TemperatureConvert tc = TemperatureConvertFactory.GetConvert(typeCodeStr);
7 double originVal = Convert.ToDouble(textBox1.Text);
8 double resultVal = tc.Convert(originVal);
9 textBox1.Text = Convert.ToString(resultVal);
来一张UML类图