简单工厂其实不是一个设计模式,而是一个编程习惯:
简单工厂其实不是一个设计模式,而是一个编程习惯:
![]()
SimpleFactory_Patterns
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Collections;
5![]()
6
namespace SimpleFactory_Patterns
7![]()
![]()
{
8
class Program
9![]()
{
10
static void Main(string[] args)
11![]()
{
12
SimplePizzaFactory factory = new SimplePizzaFactory();
13
PizzaStore pizzaStore = new PizzaStore(factory);
14
pizzaStore.orderPizza("Cheese");
15
}
16
}
17![]()
18
public abstract class Pizza
19![]()
{
20
protected string name;
21
protected string dough;
22
protected string sauce;
23![]()
24
protected ArrayList toppings = new ArrayList();
25![]()
26
public void prepare()
27![]()
{
28
Console.WriteLine("Preparing "+ name);
29
Console.WriteLine("Tossing dough
");
30
Console.WriteLine("Adding Sauce
.");
31
Console.WriteLine("Adding toppings:");
32
for (int i = 0; i < toppings.Count; i++)
33![]()
{
34
Console.WriteLine(" "+toppings[i].ToString() );
35
}
36
}
37![]()
38
public void bake()
39![]()
{
40
Console.WriteLine("Bake for 25 minutes at 350!");
41
}
42![]()
43
public void cut()
44![]()
{
45
Console.WriteLine("Cutting the pizza into diagonal slices");
46
}
47![]()
48
public void box()
49![]()
{
50
Console.WriteLine("Place pizza in official PizzaStore box");
51
}
52![]()
53
public string getName()
54![]()
{
55
return name;
56
}
57
}
58![]()
59
public class CheesePizza:Pizza
60![]()
{
61
public CheesePizza()
62![]()
{
63
name = "Cheese";
64
toppings.Add("A Of Cheese ;");
65
}
66
}
67![]()
68
public class PepperoniPizza : Pizza
69![]()
{
70
public PepperoniPizza()
71![]()
{
72
name = "Pepperoni";
73
toppings.Add("A of Pepperoni ;");
74
}
75![]()
76![]()
77
}
78![]()
79
public class ClamPizza : Pizza
80![]()
{
81
public ClamPizza()
82![]()
{
83
name = "Clam";
84
toppings.Add("A of Clam");
85
toppings.Add("B of Clam");
86
}
87![]()
88![]()
89
}
90![]()
91
public class VeggiePizza : Pizza
92![]()
{
93
public VeggiePizza()
94![]()
{
95
name = "Veggie";
96
toppings.Add("A of Veggie");
97
}
98
}
99![]()
100![]()
101
public class SimplePizzaFactory
102![]()
{
103
public Pizza CreatePizza(string type)
104![]()
{
105
Pizza pizza = null;
106
if (type.Equals("Cheese"))
107![]()
{
108
pizza = new CheesePizza();
109
}
110
else if (type.Equals("Pepperoni"))
111![]()
{
112
pizza = new PepperoniPizza();
113
}
114
else if (type.Equals("Clam"))
115![]()
{
116
pizza = new ClamPizza();
117
}
118
else if (type.Equals("Veggie"))
119![]()
{
120
pizza = new VeggiePizza();
121
}
122
123
return pizza;
124
}
125
}
126![]()
127
public class PizzaStore
128![]()
{
129
SimplePizzaFactory factory;
130
public PizzaStore(SimplePizzaFactory factory)
131![]()
{
132
this.factory = factory;
133
}
134![]()
135
public Pizza orderPizza(string type)
136![]()
{
137
Pizza pizza;
138
pizza = factory.CreatePizza(type);
139
pizza.prepare();
140
pizza.bake();
141
pizza.cut();
142
pizza.box();
143![]()
144
return pizza;
145
}
146
}
147![]()
148![]()
149![]()
150
}
151![]()
1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Collections;5

6
namespace SimpleFactory_Patterns7


{8
class Program9

{10
static void Main(string[] args)11

{12
SimplePizzaFactory factory = new SimplePizzaFactory();13
PizzaStore pizzaStore = new PizzaStore(factory);14
pizzaStore.orderPizza("Cheese");15
}16
}17

18
public abstract class Pizza19

{20
protected string name;21
protected string dough;22
protected string sauce;23

24
protected ArrayList toppings = new ArrayList();25

26
public void prepare()27

{28
Console.WriteLine("Preparing "+ name);29
Console.WriteLine("Tossing dough
");30
Console.WriteLine("Adding Sauce
.");31
Console.WriteLine("Adding toppings:");32
for (int i = 0; i < toppings.Count; i++)33

{34
Console.WriteLine(" "+toppings[i].ToString() );35
}36
}37

38
public void bake()39

{40
Console.WriteLine("Bake for 25 minutes at 350!");41
}42

43
public void cut()44

{45
Console.WriteLine("Cutting the pizza into diagonal slices");46
}47

48
public void box()49

{50
Console.WriteLine("Place pizza in official PizzaStore box");51
}52

53
public string getName()54

{55
return name;56
}57
}58

59
public class CheesePizza:Pizza60

{61
public CheesePizza()62

{63
name = "Cheese";64
toppings.Add("A Of Cheese ;");65
}66
}67

68
public class PepperoniPizza : Pizza69

{70
public PepperoniPizza()71

{72
name = "Pepperoni";73
toppings.Add("A of Pepperoni ;");74
}75

76

77
}78

79
public class ClamPizza : Pizza80

{81
public ClamPizza()82

{83
name = "Clam";84
toppings.Add("A of Clam");85
toppings.Add("B of Clam");86
}87

88

89
}90

91
public class VeggiePizza : Pizza92

{93
public VeggiePizza()94

{95
name = "Veggie";96
toppings.Add("A of Veggie");97
}98
}99

100

101
public class SimplePizzaFactory102

{103
public Pizza CreatePizza(string type)104

{105
Pizza pizza = null;106
if (type.Equals("Cheese"))107

{108
pizza = new CheesePizza();109
}110
else if (type.Equals("Pepperoni"))111

{112
pizza = new PepperoniPizza();113
}114
else if (type.Equals("Clam"))115

{116
pizza = new ClamPizza();117
}118
else if (type.Equals("Veggie"))119

{120
pizza = new VeggiePizza();121
}122
123
return pizza;124
}125
}126

127
public class PizzaStore128

{129
SimplePizzaFactory factory;130
public PizzaStore(SimplePizzaFactory factory)131

{132
this.factory = factory;133
}134

135
public Pizza orderPizza(string type)136

{137
Pizza pizza;138
pizza = factory.CreatePizza(type);139
pizza.prepare();140
pizza.bake();141
pizza.cut();142
pizza.box();143

144
return pizza;145
}146
}147

148

149

150
}151

浙公网安备 33010602011771号