码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---工厂模式之工厂方法[转]

时间:2014-11-05 16:18:41      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   os   for   sp   

bubuko.com,布布扣
  1bubuko.com,布布扣
  2bubuko.com,布布扣{《HeadFirst设计模式》工厂模式之工厂方法 }
  3bubuko.com,布布扣{ 产品类                              }
  4bubuko.com,布布扣{ 编译工具 :Delphi2007 for win32      }
  5bubuko.com,布布扣{ 联系方式 :guzh-0417@163.com         }
  6bubuko.com,布布扣
  7bubuko.com,布布扣unit uProducts;
  8bubuko.com,布布扣
  9bubuko.com,布布扣interface
 10bubuko.com,布布扣
 11bubuko.com,布布扣type
 12bubuko.com,布布扣  { abstract Pizza }
 13bubuko.com,布布扣
 14bubuko.com,布布扣  TPizza = class abstract(TObject)
 15bubuko.com,布布扣  strict private
 16bubuko.com,布布扣    function GetName: string;
 17bubuko.com,布布扣  strict protected
 18bubuko.com,布布扣    FName : string;
 19bubuko.com,布布扣    FDough: string;
 20bubuko.com,布布扣    FSauce: string;
 21bubuko.com,布布扣  public
 22bubuko.com,布布扣    procedure Prepare;
 23bubuko.com,布布扣    procedure Bake;
 24bubuko.com,布布扣    procedure Cut; virtual;
 25bubuko.com,布布扣    procedure Box; 
 26bubuko.com,布布扣    property Name: string read GetName;
 27bubuko.com,布布扣  end;
 28bubuko.com,布布扣
 29bubuko.com,布布扣  { NY Style Pizza }
 30bubuko.com,布布扣
 31bubuko.com,布布扣  TNYStyleCheesePizza = class(TPizza)
 32bubuko.com,布布扣  public
 33bubuko.com,布布扣    constructor Create;
 34bubuko.com,布布扣  end;
 35bubuko.com,布布扣
 36bubuko.com,布布扣  TNYStyleClamPizza = class(TPizza)
 37bubuko.com,布布扣  public
 38bubuko.com,布布扣    constructor Create;
 39bubuko.com,布布扣  end;
 40bubuko.com,布布扣
 41bubuko.com,布布扣  TNYStylePepperoniPizza = class(TPizza)
 42bubuko.com,布布扣  public
 43bubuko.com,布布扣    constructor Create;
 44bubuko.com,布布扣  end;
 45bubuko.com,布布扣
 46bubuko.com,布布扣  TNYStyleVeggiePizza = class(TPizza)
 47bubuko.com,布布扣  public
 48bubuko.com,布布扣    constructor Create;
 49bubuko.com,布布扣  end;
 50bubuko.com,布布扣
 51bubuko.com,布布扣  { Chicago Style Pizza }
 52bubuko.com,布布扣
 53bubuko.com,布布扣  TChicagoStyleCheesePizza = class(TPizza)
 54bubuko.com,布布扣  public
 55bubuko.com,布布扣    constructor Create;
 56bubuko.com,布布扣    procedure Cut; override;
 57bubuko.com,布布扣  end;
 58bubuko.com,布布扣
 59bubuko.com,布布扣  TChicagoStyleClamPizza = class(TPizza)
 60bubuko.com,布布扣  public
 61bubuko.com,布布扣    constructor Create;
 62bubuko.com,布布扣    procedure Cut; override;
 63bubuko.com,布布扣  end;
 64bubuko.com,布布扣
 65bubuko.com,布布扣  TChicagoStylePepperoniPizza = class(TPizza)
 66bubuko.com,布布扣  public
 67bubuko.com,布布扣    constructor Create;
 68bubuko.com,布布扣    procedure Cut; override;
 69bubuko.com,布布扣  end;
 70bubuko.com,布布扣
 71bubuko.com,布布扣  TChicagoStyleVeggiePizza = class(TPizza)
 72bubuko.com,布布扣  public
 73bubuko.com,布布扣    constructor Create;
 74bubuko.com,布布扣    procedure Cut; override;
 75bubuko.com,布布扣  end;
 76bubuko.com,布布扣
 77bubuko.com,布布扣implementation
 78bubuko.com,布布扣
 79bubuko.com,布布扣{ TPizza }
 80bubuko.com,布布扣
 81bubuko.com,布布扣procedure TPizza.Bake;
 82bubuko.com,布布扣begin
 83bubuko.com,布布扣  Writeln(‘Bake for 25 minutes at 350‘);
 84bubuko.com,布布扣end;
 85bubuko.com,布布扣
 86bubuko.com,布布扣procedure TPizza.Box;
 87bubuko.com,布布扣begin
 88bubuko.com,布布扣  Writeln(‘Place pizza in official PizzaStore box‘);
 89bubuko.com,布布扣end;
 90bubuko.com,布布扣
 91bubuko.com,布布扣procedure TPizza.Cut;
 92bubuko.com,布布扣begin
 93bubuko.com,布布扣  Writeln(‘Cutting the pizza into diaginal slices‘);
 94bubuko.com,布布扣end;
 95bubuko.com,布布扣
 96bubuko.com,布布扣function TPizza.GetName: string;
 97bubuko.com,布布扣begin
 98bubuko.com,布布扣  Result := FName;
 99bubuko.com,布布扣end;
100bubuko.com,布布扣
101bubuko.com,布布扣procedure TPizza.Prepare;
102bubuko.com,布布扣begin
103bubuko.com,布布扣  Writeln(‘Preparing ‘ + FName);
104bubuko.com,布布扣  Writeln(‘Tossing doughbubuko.com,布布扣‘);
105bubuko.com,布布扣  Writeln(‘Adding saucebubuko.com,布布扣‘);
106bubuko.com,布布扣  Writeln(‘Adding topping:‘);
107bubuko.com,布布扣end;
108bubuko.com,布布扣
109bubuko.com,布布扣{ TNYStyleCheesePizza }
110bubuko.com,布布扣
111bubuko.com,布布扣constructor TNYStyleCheesePizza.Create;
112bubuko.com,布布扣begin
113bubuko.com,布布扣  FName  := ‘NY Style Sauce and Cheese Pizza‘;
114bubuko.com,布布扣  FDough := ‘Thin Crust Dough‘;
115bubuko.com,布布扣  FSauce := ‘Marinara Sauce‘;
116bubuko.com,布布扣end;
117bubuko.com,布布扣
118bubuko.com,布布扣{ TNYStyleClamPizza }
119bubuko.com,布布扣
120bubuko.com,布布扣constructor TNYStyleClamPizza.Create;
121bubuko.com,布布扣begin
122bubuko.com,布布扣  FName  := ‘NY Style Clam Pizza‘;
123bubuko.com,布布扣  FDough := ‘Thin Crust Dough‘;
124bubuko.com,布布扣  FSauce := ‘Marinara Sauce‘;
125bubuko.com,布布扣end;
126bubuko.com,布布扣
127bubuko.com,布布扣{ TNYStylePepperoniPizza }
128bubuko.com,布布扣
129bubuko.com,布布扣constructor TNYStylePepperoniPizza.Create;
130bubuko.com,布布扣begin
131bubuko.com,布布扣  FName  := ‘NY Style Pepperoni Pizza‘;
132bubuko.com,布布扣  FDough := ‘Thin Crust Dough‘;
133bubuko.com,布布扣  FSauce := ‘Marinara Sauce‘;
134bubuko.com,布布扣end;
135bubuko.com,布布扣
136bubuko.com,布布扣{ TNYStyleVeggieizza }
137bubuko.com,布布扣
138bubuko.com,布布扣constructor TNYStyleVeggiePizza.Create;
139bubuko.com,布布扣begin
140bubuko.com,布布扣  FName  := ‘NY Style Veggie Pizza‘;
141bubuko.com,布布扣  FDough := ‘Thin Crust Dough‘;
142bubuko.com,布布扣  FSauce := ‘Marinara Sauce‘;
143bubuko.com,布布扣end;
144bubuko.com,布布扣
145bubuko.com,布布扣{ TChicagoStyleCheesePizza }
146bubuko.com,布布扣
147bubuko.com,布布扣constructor TChicagoStyleCheesePizza.Create;
148bubuko.com,布布扣begin
149bubuko.com,布布扣  FName  := ‘Chicago Style Deep Dish Cheese Pizza‘;
150bubuko.com,布布扣  FDough := ‘Extra Thick Crust Dough‘;
151bubuko.com,布布扣  FSauce := ‘Plum Tomato Sauce‘;
152bubuko.com,布布扣end;
153bubuko.com,布布扣
154bubuko.com,布布扣procedure TChicagoStyleCheesePizza.Cut;
155bubuko.com,布布扣begin
156bubuko.com,布布扣  Writeln(‘Cutting the pizza into square slices‘);
157bubuko.com,布布扣end;
158bubuko.com,布布扣
159bubuko.com,布布扣{ TChicagoStyleClamPizza }
160bubuko.com,布布扣
161bubuko.com,布布扣constructor TChicagoStyleClamPizza.Create;
162bubuko.com,布布扣begin
163bubuko.com,布布扣  FName  := ‘Chicago Style Clam Pizza‘;
164bubuko.com,布布扣  FDough := ‘Extra Thick Crust Dough‘;
165bubuko.com,布布扣  FSauce := ‘Plum Tomato Sauce‘;
166bubuko.com,布布扣end;
167bubuko.com,布布扣
168bubuko.com,布布扣procedure TChicagoStyleClamPizza.Cut;
169bubuko.com,布布扣begin
170bubuko.com,布布扣  Writeln(‘Cutting the pizza into square slices‘);
171bubuko.com,布布扣end;
172bubuko.com,布布扣
173bubuko.com,布布扣{ TChicagoStylePepperoniPizza }
174bubuko.com,布布扣
175bubuko.com,布布扣constructor TChicagoStylePepperoniPizza.Create;
176bubuko.com,布布扣begin
177bubuko.com,布布扣  FName  := ‘Chicago Style Pepperoni Pizza‘;
178bubuko.com,布布扣  FDough := ‘Extra Thick Crust Dough‘;
179bubuko.com,布布扣  FSauce := ‘Plum Tomato Sauce‘;
180bubuko.com,布布扣end;
181bubuko.com,布布扣
182bubuko.com,布布扣procedure TChicagoStylePepperoniPizza.Cut;
183bubuko.com,布布扣begin
184bubuko.com,布布扣  Writeln(‘Cutting the pizza into square slices‘);
185bubuko.com,布布扣end;
186bubuko.com,布布扣
187bubuko.com,布布扣{ TChicagoStyleVeggiePizza }
188bubuko.com,布布扣
189bubuko.com,布布扣constructor TChicagoStyleVeggiePizza.Create;
190bubuko.com,布布扣begin
191bubuko.com,布布扣  FName  := ‘Chicago Style Veggie Pizza‘;
192bubuko.com,布布扣  FDough := ‘Extra Thick Crust Dough‘;
193bubuko.com,布布扣  FSauce := ‘Plum Tomato Sauce‘;
194bubuko.com,布布扣end;
195bubuko.com,布布扣
196bubuko.com,布布扣procedure TChicagoStyleVeggiePizza.Cut;
197bubuko.com,布布扣begin
198bubuko.com,布布扣  Writeln(‘Cutting the pizza into square slices‘);
199bubuko.com,布布扣end;
200bubuko.com,布布扣
201bubuko.com,布布扣end.
202bubuko.com,布布扣

bubuko.com,布布扣
 1bubuko.com,布布扣
 2bubuko.com,布布扣{《HeadFirst设计模式》工厂模式之工厂方法 }
 3bubuko.com,布布扣{ 工厂类                              }
 4bubuko.com,布布扣{ 编译工具 :Delphi2007 for win32     }
 5bubuko.com,布布扣{ 联系方式 :guzh-0417@163.com        }
 6bubuko.com,布布扣
 7bubuko.com,布布扣unit uFactory;
 8bubuko.com,布布扣
 9bubuko.com,布布扣interface
10bubuko.com,布布扣
11bubuko.com,布布扣uses
12bubuko.com,布布扣  uProducts;
13bubuko.com,布布扣
14bubuko.com,布布扣type
15bubuko.com,布布扣  TPizzaStore = class abstract(TObject)
16bubuko.com,布布扣  strict protected
17bubuko.com,布布扣    FPizza: TPizza;
18bubuko.com,布布扣  public
19bubuko.com,布布扣    function OrderPizza (Category: Integer): TPizza;
20bubuko.com,布布扣    function CreatePizza(Category: Integer): TPizza; virtual; abstract;
21bubuko.com,布布扣  end;
22bubuko.com,布布扣
23bubuko.com,布布扣  TNYPizzaStore = class(TPizzaStore)
24bubuko.com,布布扣  public
25bubuko.com,布布扣    function CreatePizza(Category: Integer): TPizza; override;
26bubuko.com,布布扣    destructor Destroy; override;
27bubuko.com,布布扣  end;
28bubuko.com,布布扣
29bubuko.com,布布扣  TChicagoPizzaStore = class(TPizzaStore)
30bubuko.com,布布扣  public
31bubuko.com,布布扣    function CreatePizza(Category: Integer): TPizza; override;
32bubuko.com,布布扣    destructor Destroy; override;
33bubuko.com,布布扣  end;
34bubuko.com,布布扣
35bubuko.com,布布扣implementation
36bubuko.com,布布扣
37bubuko.com,布布扣{ TPizzaStore }
38bubuko.com,布布扣
39bubuko.com,布布扣function TPizzaStore.OrderPizza(Category: Integer): TPizza;
40bubuko.com,布布扣begin
41bubuko.com,布布扣  FPizza := CreatePizza(Category);
42bubuko.com,布布扣  
43bubuko.com,布布扣  FPizza.Prepare;
44bubuko.com,布布扣  FPizza.Bake;
45bubuko.com,布布扣  FPizza.Cut;
46bubuko.com,布布扣  FPizza.Box;
47bubuko.com,布布扣
48bubuko.com,布布扣  Result := FPizza;
49bubuko.com,布布扣end;
50bubuko.com,布布扣
51bubuko.com,布布扣{ TNYPizzaStore }
52bubuko.com,布布扣
53bubuko.com,布布扣function TNYPizzaStore.CreatePizza(Category: Integer): TPizza;
54bubuko.com,布布扣begin
55bubuko.com,布布扣  case Category of
56bubuko.com,布布扣    0: Result := TNYStyleCheesePizza.Create;
57bubuko.com,布布扣    1: Result := TNYStyleClamPizza.Create;
58bubuko.com,布布扣    2: Result := TNyStylePepperoniPizza.Create;
59bubuko.com,布布扣    3: Result := TNYStyleVeggiePizza.Create;
60bubuko.com,布布扣  else
61bubuko.com,布布扣    Result := nil;
62bubuko.com,布布扣  end;
63bubuko.com,布布扣end;
64bubuko.com,布布扣
65bubuko.com,布布扣destructor TNYPizzaStore.Destroy;
66bubuko.com,布布扣begin
67bubuko.com,布布扣  FPizza.Free;
68bubuko.com,布布扣  inherited Destroy;
69bubuko.com,布布扣end;
70bubuko.com,布布扣
71bubuko.com,布布扣{ TChicagoPizzaStore }
72bubuko.com,布布扣
73bubuko.com,布布扣function TChicagoPizzaStore.CreatePizza(Category: Integer): TPizza;
74bubuko.com,布布扣begin
75bubuko.com,布布扣  case Category of
76bubuko.com,布布扣    0: Result := TChicagoStyleCheesePizza.Create;
77bubuko.com,布布扣    1: Result := TChicagoStyleClamPizza.Create;
78bubuko.com,布布扣    2: Result := TChicagoStylePepperoniPizza.Create;
79bubuko.com,布布扣    3: Result := TChicagoStyleVeggiePizza.Create;
80bubuko.com,布布扣  else
81bubuko.com,布布扣    Result := nil;
82bubuko.com,布布扣  end;
83bubuko.com,布布扣end;
84bubuko.com,布布扣
85bubuko.com,布布扣destructor TChicagoPizzaStore.Destroy;
86bubuko.com,布布扣begin
87bubuko.com,布布扣  FPizza.Free;
88bubuko.com,布布扣  inherited Destroy;
89bubuko.com,布布扣end;
90bubuko.com,布布扣
91bubuko.com,布布扣end.
92bubuko.com,布布扣

bubuko.com,布布扣
 1bubuko.com,布布扣
 2bubuko.com,布布扣{《HeadFirst设计模式》工厂模式之工厂方法 }
 3bubuko.com,布布扣{ 客户端                              }
 4bubuko.com,布布扣{ 编译工具 :Delphi2007 for win32     }
 5bubuko.com,布布扣{ 联系方式 :guzh-0417@163.com        }
 6bubuko.com,布布扣
 7bubuko.com,布布扣program pPizzaStoreTest;
 8bubuko.com,布布扣
 9bubuko.com,布布扣{$APPTYPE CONSOLE}
10bubuko.com,布布扣
11bubuko.com,布布扣uses
12bubuko.com,布布扣  uFactory in ‘uFactory.pas‘,
13bubuko.com,布布扣  uProducts in ‘uProducts.pas‘;
14bubuko.com,布布扣
15bubuko.com,布布扣var
16bubuko.com,布布扣  aNYStore: TPizzaStore;
17bubuko.com,布布扣  aChicagoStore: TPizzaStore;
18bubuko.com,布布扣  aPizza: TPizza;
19bubuko.com,布布扣
20bubuko.com,布布扣begin
21bubuko.com,布布扣  aNYStore := TNYPizzaStore.Create;
22bubuko.com,布布扣  aPizza := aNYStore.OrderPizza(0);
23bubuko.com,布布扣  Writeln(‘Ethan ordered a ‘ + aPizza.Name);
24bubuko.com,布布扣  aNYStore.Free;
25bubuko.com,布布扣
26bubuko.com,布布扣  Writeln;
27bubuko.com,布布扣
28bubuko.com,布布扣  aChicagoStore := TChicagoPizzaStore.Create;
29bubuko.com,布布扣  aPizza := aChicagoStore.OrderPizza(0);
30bubuko.com,布布扣  Writeln(‘Joel ordered a ‘ + aPizza.Name);
31bubuko.com,布布扣  aChicagoStore.Free;
32bubuko.com,布布扣
33bubuko.com,布布扣  Readln;
34bubuko.com,布布扣end.
35bubuko.com,布布扣

运行结果:

bubuko.com,布布扣

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---工厂模式之工厂方法[转]

标签:des   style   blog   http   io   ar   os   for   sp   

原文地址:http://www.cnblogs.com/0x2D-0x22/p/4076344.html

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