Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂[转]

2020-12-13 15:42

阅读:242

标签:des   blog   http   io   ar   os   sp   div   on   

    简单工厂:工厂依据传进的参数创建相应的产品。

soscw.com,搜素材
  1soscw.com,搜素材
  2soscw.com,搜素材{《HeadFirst设计模式》工厂模式之简单工厂 }
  3soscw.com,搜素材{ 产品类                              }
  4soscw.com,搜素材{ 编译工具 :Delphi7.0                 }
  5soscw.com,搜素材{ 联系方式 :guzh-0417@163.com         }
  6soscw.com,搜素材
  7soscw.com,搜素材unit uProducts;
  8soscw.com,搜素材
  9soscw.com,搜素材interface
 10soscw.com,搜素材
 11soscw.com,搜素材type
 12soscw.com,搜素材  TPizza = class(TObject)
 13soscw.com,搜素材  public
 14soscw.com,搜素材    procedure Prepare; virtual; abstract;
 15soscw.com,搜素材    procedure Bake; virtual; abstract;
 16soscw.com,搜素材    procedure Cut; virtual; abstract;
 17soscw.com,搜素材    procedure Box; virtual; abstract;
 18soscw.com,搜素材  end;
 19soscw.com,搜素材
 20soscw.com,搜素材  TCheesePizza = class(TPizza)
 21soscw.com,搜素材  public
 22soscw.com,搜素材    procedure Prepare; override;
 23soscw.com,搜素材    procedure Bake; override;
 24soscw.com,搜素材    procedure Cut; override;
 25soscw.com,搜素材    procedure Box; override;
 26soscw.com,搜素材  end;
 27soscw.com,搜素材
 28soscw.com,搜素材  TVeggiePizza = class(TPizza)
 29soscw.com,搜素材  public
 30soscw.com,搜素材    procedure Prepare; override;
 31soscw.com,搜素材    procedure Bake; override;
 32soscw.com,搜素材    procedure Cut; override;
 33soscw.com,搜素材    procedure Box; override;
 34soscw.com,搜素材  end;
 35soscw.com,搜素材
 36soscw.com,搜素材  TClamPizza = class(TPizza)
 37soscw.com,搜素材  public
 38soscw.com,搜素材    procedure Prepare; override;
 39soscw.com,搜素材    procedure Bake; override;
 40soscw.com,搜素材    procedure Cut; override;
 41soscw.com,搜素材    procedure Box; override;
 42soscw.com,搜素材  end;
 43soscw.com,搜素材
 44soscw.com,搜素材  TPepperoniPizza = class(TPizza)
 45soscw.com,搜素材  public
 46soscw.com,搜素材    procedure Prepare; override;
 47soscw.com,搜素材    procedure Bake; override;
 48soscw.com,搜素材    procedure Cut; override;
 49soscw.com,搜素材    procedure Box; override;
 50soscw.com,搜素材  end;
 51soscw.com,搜素材
 52soscw.com,搜素材implementation
 53soscw.com,搜素材
 54soscw.com,搜素材{ TCheesePizza }
 55soscw.com,搜素材
 56soscw.com,搜素材procedure TCheesePizza.Bake;
 57soscw.com,搜素材begin
 58soscw.com,搜素材  Writeln(‘烘烤奶油比萨‘);
 59soscw.com,搜素材end;
 60soscw.com,搜素材
 61soscw.com,搜素材procedure TCheesePizza.Box;
 62soscw.com,搜素材begin
 63soscw.com,搜素材  Writeln(‘将奶油比萨装盒‘);
 64soscw.com,搜素材end;
 65soscw.com,搜素材
 66soscw.com,搜素材procedure TCheesePizza.Cut;
 67soscw.com,搜素材begin
 68soscw.com,搜素材  Writeln(‘将奶油比萨切成片‘);
 69soscw.com,搜素材end;
 70soscw.com,搜素材
 71soscw.com,搜素材procedure TCheesePizza.Prepare;
 72soscw.com,搜素材begin
 73soscw.com,搜素材  Writeln(‘准备奶油比萨‘);
 74soscw.com,搜素材end;
 75soscw.com,搜素材
 76soscw.com,搜素材{ TVeggiePizza }
 77soscw.com,搜素材
 78soscw.com,搜素材procedure TVeggiePizza.Bake;
 79soscw.com,搜素材begin
 80soscw.com,搜素材  Writeln(‘烘烤素比萨‘);
 81soscw.com,搜素材end;
 82soscw.com,搜素材
 83soscw.com,搜素材procedure TVeggiePizza.Box;
 84soscw.com,搜素材begin
 85soscw.com,搜素材  Writeln(‘将素比萨装盒‘);
 86soscw.com,搜素材end;
 87soscw.com,搜素材
 88soscw.com,搜素材procedure TVeggiePizza.Cut;
 89soscw.com,搜素材begin
 90soscw.com,搜素材   Writeln(‘将素比萨切成片‘);
 91soscw.com,搜素材end;
 92soscw.com,搜素材
 93soscw.com,搜素材procedure TVeggiePizza.Prepare;
 94soscw.com,搜素材begin
 95soscw.com,搜素材  Writeln(‘准备素比萨‘);
 96soscw.com,搜素材end;
 97soscw.com,搜素材
 98soscw.com,搜素材{ TClamPizza }
 99soscw.com,搜素材
100soscw.com,搜素材procedure TClamPizza.Bake;
101soscw.com,搜素材begin
102soscw.com,搜素材  Writeln(‘烘烤蛤蛎比萨‘);
103soscw.com,搜素材end;
104soscw.com,搜素材
105soscw.com,搜素材procedure TClamPizza.Box;
106soscw.com,搜素材begin
107soscw.com,搜素材  Writeln(‘将蛤蛎比萨装盒‘);
108soscw.com,搜素材end;
109soscw.com,搜素材
110soscw.com,搜素材procedure TClamPizza.Cut;
111soscw.com,搜素材begin
112soscw.com,搜素材   Writeln(‘将蛤蛎比萨切成片‘);
113soscw.com,搜素材end;
114soscw.com,搜素材
115soscw.com,搜素材procedure TClamPizza.Prepare;
116soscw.com,搜素材begin
117soscw.com,搜素材  Writeln(‘准备蛤蛎比萨‘);
118soscw.com,搜素材end;
119soscw.com,搜素材
120soscw.com,搜素材{ TPepperoniPizza }
121soscw.com,搜素材
122soscw.com,搜素材procedure TPepperoniPizza.Bake;
123soscw.com,搜素材begin
124soscw.com,搜素材  Writeln(‘烘烤Pepperoni比萨‘);
125soscw.com,搜素材end;
126soscw.com,搜素材
127soscw.com,搜素材procedure TPepperoniPizza.Box;
128soscw.com,搜素材begin
129soscw.com,搜素材  Writeln(‘将Pepperoni比萨装盒‘);
130soscw.com,搜素材end;
131soscw.com,搜素材
132soscw.com,搜素材procedure TPepperoniPizza.Cut;
133soscw.com,搜素材begin
134soscw.com,搜素材   Writeln(‘将Pepperoni比萨切成片‘);
135soscw.com,搜素材end;
136soscw.com,搜素材
137soscw.com,搜素材procedure TPepperoniPizza.Prepare;
138soscw.com,搜素材begin
139soscw.com,搜素材  Writeln(‘准备Pepperoni比萨‘);
140soscw.com,搜素材end;
141soscw.com,搜素材
142soscw.com,搜素材end.

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》工厂模式之简单工厂 }
 3soscw.com,搜素材{ 简单工厂类                           }
 4soscw.com,搜素材{ 编译工具 :Delphi7.0                 }
 5soscw.com,搜素材{ 联系方式 :guzh-0417@163.com         }
 6soscw.com,搜素材
 7soscw.com,搜素材unit uSimpleFactory;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  uProducts;
13soscw.com,搜素材
14soscw.com,搜素材type
15soscw.com,搜素材  { 静态工厂 }
16soscw.com,搜素材  TSimplePizzaFactory = class(TObject)
17soscw.com,搜素材  public
18soscw.com,搜素材    class function CreatePizza(Category: Integer): TPizza;
19soscw.com,搜素材  end;
20soscw.com,搜素材
21soscw.com,搜素材implementation
22soscw.com,搜素材
23soscw.com,搜素材{ TSimplePizzaFactory }
24soscw.com,搜素材
25soscw.com,搜素材class function TSimplePizzaFactory.CreatePizza(Category: Integer): TPizza;
26soscw.com,搜素材begin
27soscw.com,搜素材  case Category of
28soscw.com,搜素材    0: Result := TCheesePizza.Create;    {创建奶油比萨      }
29soscw.com,搜素材    1: Result := TVeggiePizza.Create;    {创建素比萨        }
30soscw.com,搜素材    2: Result := TClamPizza.Create;      {创建蛤蛎比萨      }
31soscw.com,搜素材    3: Result := TPepperoniPizza.Create; {创建Pepperoni比萨 }
32soscw.com,搜素材  else
33soscw.com,搜素材    Result := nil;
34soscw.com,搜素材  end; // end case.
35soscw.com,搜素材end;
36soscw.com,搜素材
37soscw.com,搜素材end.

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》工厂模式之简单工厂 }
 3soscw.com,搜素材{ 简单工厂的用户类                      }
 4soscw.com,搜素材{ 编译工具 :Delphi7.0                 }
 5soscw.com,搜素材{ 联系方式 :guzh-0417@163.com         }
 6soscw.com,搜素材
 7soscw.com,搜素材unit uUserOfSimpleFactory;
 8soscw.com,搜素材
 9soscw.com,搜素材interface
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  uProducts, uSimpleFactory;
13soscw.com,搜素材
14soscw.com,搜素材type
15soscw.com,搜素材  { 工厂的用户 }
16soscw.com,搜素材  TPizzaStore = class(TObject)
17soscw.com,搜素材  public
18soscw.com,搜素材    procedure OrderPizza;
19soscw.com,搜素材  end;
20soscw.com,搜素材
21soscw.com,搜素材implementation
22soscw.com,搜素材
23soscw.com,搜素材{ TOrderPizza }
24soscw.com,搜素材
25soscw.com,搜素材procedure TPizzaStore.OrderPizza;
26soscw.com,搜素材var
27soscw.com,搜素材  aPizza: TPizza;
28soscw.com,搜素材begin
29soscw.com,搜素材  aPizza := TSimplePizzaFactory.CreatePizza(0);
30soscw.com,搜素材
31soscw.com,搜素材  aPizza.Prepare();
32soscw.com,搜素材  aPizza.Bake();
33soscw.com,搜素材  aPizza.Cut();
34soscw.com,搜素材  aPizza.Box();
35soscw.com,搜素材
36soscw.com,搜素材  aPizza.Free;
37soscw.com,搜素材end;
38soscw.com,搜素材
39soscw.com,搜素材end.

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》工厂模式之简单工厂 }
 3soscw.com,搜素材{ 客户端                              }
 4soscw.com,搜素材{ 编译工具 :Delphi7.0                 }
 5soscw.com,搜素材{ 联系方式 :guzh-0417@163.com         }
 6soscw.com,搜素材
 7soscw.com,搜素材program pPizzaStoreTest;
 8soscw.com,搜素材
 9soscw.com,搜素材{$APPTYPE CONSOLE}
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  uProducts in ‘uProducts.pas‘,
13soscw.com,搜素材  uSimpleFactory in ‘uSimpleFactory.pas‘,
14soscw.com,搜素材  uUserOfSimpleFactory in ‘uUserOfSimpleFactory.pas‘;
15soscw.com,搜素材
16soscw.com,搜素材var
17soscw.com,搜素材  aPizzaStore: TPizzaStore;
18soscw.com,搜素材
19soscw.com,搜素材begin
20soscw.com,搜素材  aPizzaStore := TPizzaStore.Create;
21soscw.com,搜素材  aPizzaStore.OrderPizza;
22soscw.com,搜素材  aPizzaStore.Free;
23soscw.com,搜素材
24soscw.com,搜素材  Readln;
25soscw.com,搜素材end.

运行结果:

soscw.com,搜素材

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂[转]

标签:des   blog   http   io   ar   os   sp   div   on   

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


评论


亲,登录后才可以留言!