Delphi 设计模式:《HeadFirst设计模式》Delphi代码---工厂模式之抽象工厂
2009-11-10 09:29
716 查看
1. 抽象工厂的产品
2. 抽象工厂
3.抽象工厂的用户
4.Pizza的用户
5.客户端
运行结果:
{《HeadFirst设计模式》工厂模式之抽象工厂 } { 抽象工厂的产品 } { 编译工具:Delphi7.0 } { E-Mail :xshlife@163.com } unit uPizzaIngredient; interface type TDough = class(TObject) end; TThinCrustDough = class(TDough) end; TThickCrustDough = class(TDough) end; //////////////////////////////// TSauce = class(TObject) end; TMarinaraSauce = class(TSauce) end; TPlumTomatoSauce = class(TSauce) end; //////////////////////////////// TCheese = class(TObject) end; TReggianoCheese = class(TCheese) end; TMozzarellaCheese = class(TCheese) end; ////////////////////////////////// TPepperoni = class(TObject) end; TSlicedPepperoni = class(TPepperoni) end; //////////////////////////////////// TClams = class(TObject) end; TFreshClams = class(TClams) end; TFrozenClams = class(TClams) end; implementation end.
2. 抽象工厂
{《HeadFirst设计模式》工厂模式之抽象工厂 } { 抽象工厂 } { 编译工具:Delphi7.0 } { E-Mail :xshlife@163.com } unit uPizzaIngredientFactory; interface uses uPizzaIngredient; type TPizzaIngredientFactory = class(TObject) public function CreateDough: TDough; virtual; abstract; function CreateSauce: TSauce; virtual; abstract; function CreateCheese: TCheese; virtual; abstract; function CreatePepperoni: TPepperoni; virtual; abstract; function CreateClams: TClams; virtual; abstract; end; TNYPizzaIngredientFactory = class(TPizzaIngredientFactory) public function CreateDough: TDough; override; function CreateSauce: TSauce; override; function CreateCheese: TCheese; override; function CreatePepperoni: TPepperoni; override; function CreateClams: TClams; override; end; TChicagoPizzaIngredientFactory = class(TPizzaIngredientFactory) public function CreateDough: TDough; override; function CreateSauce: TSauce; override; function CreateCheese: TCheese; override; function CreatePepperoni: TPepperoni; override; function CreateClams: TClams; override; end; implementation { TNYPizzaIngredientFactory } function TNYPizzaIngredientFactory.CreateCheese: TCheese; begin Result := TReggianoCheese.Create; end; function TNYPizzaIngredientFactory.CreateClams: TClams; begin Result := TFreshClams.Create; end; function TNYPizzaIngredientFactory.CreateDough: TDough; begin Result := TThinCrustDough.Create; end; function TNYPizzaIngredientFactory.CreatePepperoni: TPepperoni; begin Result := TSlicedPepperoni.Create; end; function TNYPizzaIngredientFactory.CreateSauce: TSauce; begin Result := TMarinaraSauce.Create; end; { TChicagoPizzaIngredientFactory } function TChicagoPizzaIngredientFactory.CreateCheese: TCheese; begin Result := TMozzarellaCheese.Create; end; function TChicagoPizzaIngredientFactory.CreateClams: TClams; begin Result := TFrozenClams.Create; end; function TChicagoPizzaIngredientFactory.CreateDough: TDough; begin Result := TThickCrustDough.Create; end; function TChicagoPizzaIngredientFactory.CreatePepperoni: TPepperoni; begin Result := TSlicedPepperoni.Create; end; function TChicagoPizzaIngredientFactory.CreateSauce: TSauce; begin Result := TPlumTomatoSauce.Create; end; end.
3.抽象工厂的用户
{《HeadFirst设计模式》工厂模式之抽象工厂 } { 抽象工厂的用户 } { 编译工具:Delphi7.0 } { E-Mail :guzh-0417@163.com } unit uPizza; interface uses uPizzaIngredient, uPizzaIngredientFactory; type TPizza = class(TObject) private procedure SetName(const Value: string); protected FName: string; FDough: TDough; FSauce: TSauce; FCheese: TCheese; FPepperoni: TPepperoni; FClams: TClams; FIngredientFactory: TPizzaIngredientFactory; public constructor Create(aIngredientFactory: TPizzaIngredientFactory); procedure Prepare; virtual; abstract; procedure Bake; procedure Cut; procedure Box; property Name: string read FName write SetName; end; TCheesePizza = class(TPizza) public destructor Destroy; override; procedure Prepare; override; end; TClamsPizza = class(TPizza) public destructor Destroy; override; procedure Prepare; override; end; implementation { TPizza } procedure TPizza.Bake; begin Writeln('Bake for 25 minutes at 350'); end; procedure TPizza.Box; begin Writeln('Place pizza in official PizzaStore box'); end; constructor TPizza.Create(aIngredientFactory: TPizzaIngredientFactory); begin Self.FIngredientFactory := aIngredientFactory; end; procedure TPizza.Cut; begin Writeln('Cutting the pizza into diagonal slices'); end; procedure TPizza.SetName(const Value: string); begin FName := Value; end; { TCheesePizza } destructor TCheesePizza.Destroy; begin FDough.Free; FSauce.Free; FCheese.Free; inherited; end; procedure TCheesePizza.Prepare; begin Writeln('Preparing ' + Name); FDough := FIngredientFactory.CreateDough; FSauce := FIngredientFactory.CreateSauce; FCheese := FIngredientFactory.CreateCheese; end; { TClamsPizza } destructor TClamsPizza.Destroy; begin FDough.Free; FSauce.Free; FCheese.Free; FClams.Free; inherited; end; procedure TClamsPizza.Prepare; begin Writeln('Preparing ' + Name); FDough := FIngredientFactory.CreateDough; FSauce := FIngredientFactory.CreateSauce; FCheese := FIngredientFactory.CreateCheese; FClams := FIngredientFactory.CreateClams; end; end.
4.Pizza的用户
{《HeadFirst设计模式》工厂模式之抽象工厂 } { Pizza的用户 } { 编译工具:Delphi7.0 } { E-Mail :xshlife@163.com } unit uPizzaStore; interface uses uPizza, uPizzaIngredientFactory; type TPizzaStore = class(TObject) protected FPizza: TPizza; FIngredientFactory: TPizzaIngredientFactory; public destructor Destroy; override; function OrderPizza(Category: Integer): TPizza; function CreatePizza(Style: Integer): TPizza; virtual; abstract; end; TNYPizzaStore = class(TPizzaStore) public function CreatePizza(Style: Integer): TPizza; override; end; TChicagoPizzaStore = class(TPizzaStore) public function CreatePizza(Style: Integer): TPizza; override; end; implementation { TPizzaStore } destructor TPizzaStore.Destroy; begin FPizza.Free; FIngredientFactory.Free; inherited; end; function TPizzaStore.OrderPizza(Category: Integer): TPizza; begin FPizza := CreatePizza(Category); FPizza.Prepare; FPizza.Bake; FPizza.Cut; FPizza.Box; Result := FPizza; end; { TNYPizzaStore } function TNYPizzaStore.CreatePizza(Style: Integer): TPizza; begin FIngredientFactory := TNYPizzaIngredientFactory.Create; FPizza := nil; case Style of 0: begin FPizza := TCheesePizza.Create(FIngredientFactory); FPizza.Name := 'New York Style Cheese Pizza'; end; 1: begin FPizza := TClamsPizza.Create(FIngredientFactory); FPizza.Name := 'New York Style Clams Pizza'; end; //2: 其它风味的 New York Pizza // end; Result := FPizza; end; { TChicagoPizzaStore } function TChicagoPizzaStore.CreatePizza(Style: Integer): TPizza; begin FIngredientFactory := TChicagoPizzaIngredientFactory.Create; case Style of 0: begin Result := TCheesePizza.Create(FIngredientFactory); Result.Name := 'Chicago Style Cheese Pizza'; end; 1: begin Result := TClamsPizza.Create(FIngredientFactory); Result.Name := 'Chicago Style Clams Pizza'; end; //2: 其它风味的 Chicago Pizza // else Result := nil; end; end; end.
5.客户端
{《HeadFirst设计模式》工厂模式之抽象工厂 } { 客户端 } { 编译工具:Delphi7.0 } { E-Mail :xshlife@163.com } program pPizzaStore; {$APPTYPE CONSOLE} uses uPizzaIngredient in 'uPizzaIngredient.pas', uPizzaIngredientFactory in 'uPizzaIngredientFactory.pas', uPizza in 'uPizza.pas', uPizzaStore in 'uPizzaStore.pas'; var PizzaStore: TPizzaStore; Pizza: TPizza; begin PizzaStore := TNYPizzaStore.Create; Pizza := PizzaStore.OrderPizza(0); Writeln('Ethan ordered a ' + Pizza.Name); Writeln; Pizza := PizzaStore.OrderPizza(1); Writeln('Ethan ordered a ' + Pizza.Name); PizzaStore.Free; Writeln; Writeln(' ********************************** '); Writeln; PizzaStore := TChicagoPizzaStore.Create; Pizza := PizzaStore.OrderPizza(0); Writeln('Joel ordered a ' + Pizza.Name); Writeln; Pizza := PizzaStore.OrderPizza(1); Writeln('Joel ordered a ' + Pizza.Name); PizzaStore.Free; Readln; end.
运行结果:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/djCode/EntryImages/20091110/picAbstractPizzaFactory.jpg)
相关文章推荐
- Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---工厂模式之工厂方法
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂
- 请用代码简单描述一下Singleton、抽象工厂、Bridge、Composite(任选三个)的设计模式的概念
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---策略模式之MiniDuckSimulator
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteControlTest
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---状态模式
- Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---单例模式之ChocolateBoiler
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---装饰模式之StarBuzzCoffee
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---门面模式之HomeTheater
- 设计模式之抽象工厂 代码示例
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---观察者模式之WeatherReport
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之RemoteControlTest
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook
- Java设计模式笔记之抽象工厂代码示例
- Delphi 设计模式:《HeadFirst设计模式》Delphi代码---模式小结之一个叫声接口和几只鸭子
- Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteWithUndoTest
- 简单抽象工厂设计模式代码
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---适配器模式之TurkeyAdapter