1-【抽象工厂模式】
图解:
简单工厂,工厂方法模式 , 抽象工厂模式
共同点: 1- 封装对象的创建,2-减少应用程序与具体类之间的以来,松耦合
特点:
简单工厂(单个Pizza店)
工厂方法模式(Pizza 连锁店 )
使用 继承 ,把对象的创建委托给子类NewYorkPizzaStrore ,
子类实现工厂方法createPizza () 来创建具体的对象 NewYorkCheesePizza 。
抽象工厂模式(Pizza店原料工厂 )
抽象工厂创建相关的对象家族ChessePizza【以“原来工厂”为参数 】,而不是依赖他们的具体类NewYorkCheesPizza
使用对象 组合 【Pizza由Dough和Sauce组成 】, 对象的创建被实现在工厂接口
PizzaIngredientFactory 所暴露的方法中
pizza->Dough = createDough ();
pizza->Sauce = createSauce ();
C++代码实现:
PotatoPizza.h和ChessPizza.h类似,只写出ChessPizza.h
Sauce.h和Dough.h类似,ChicagoSauce.h和ChicagoDough.h类似,NewYorkDough.h和NewYorkSauce.h类似,只列出其一
ChicagoIngredientFactory.h和NewYorkIngredientFactory.h类似,只列出其一
ChicagoPizzaStore.h和NewYorkPizzaStore.h类似,只列出其一
输出:
ebug/GoF-AbstractFactory.exe
***********THERE IS A CLIENT ORDER PIZZA**********
=========== Chicago Ingredient Factory ==========
Ingredient:ChicagoDough being created......
Ingredient:ChicagoSauce being created......
=========== Now In Chicago Pizza Store ==========
ChessPizza:Doing 1-prepare......
:Doing 2-bake......
:Doing 3-cut......
:Doing 4-box......
*******************PIZZA PREPARED*****************
***********THERE IS A CLIENT ORDER PIZZA**********
=========== Chicago Ingredient Factory ==========
Ingredient:ChicagoDough being created......
Ingredient:ChicagoSauce being created......
=========== Now In Chicago Pizza Store ==========
PotatPizza:Doing 1-prepare......
:Doing 2-bake......
:Doing 3-cut......
:Doing 4-box......
*******************PIZZA PREPARED*****************
***********THERE IS A CLIENT ORDER PIZZA**********
=========== NewYork Ingredient Factory ==========
Ingredient:NewYorkDough being created......
Ingredient:NewYorkSauce being created......
=========== Now In NewYork Pizza Store ==========
ChessPizza:Doing 1-prepare......
:Doing 2-bake......
:Doing 3-cut......
:Doing 4-box......
*******************PIZZA PREPARED*****************
***********THERE IS A CLIENT ORDER PIZZA**********
=========== NewYork Ingredient Factory ==========
Ingredient:NewYorkDough being created......
Ingredient:NewYorkSauce being created......
=========== Now In NewYork Pizza Store ==========
PotatPizza:Doing 1-prepare......
:Doing 2-bake......
:Doing 3-cut......
:Doing 4-box......
*******************PIZZA PREPARED*****************
2-【Command模式】
目的是将“行为的请求者”和“行为的实现者”解耦。
图解:
输出:
ake -f "Makefile" CFG=Debug
g++ -c -g -o "Debug/GoF-Command.o" "GoF-Command.cpp"
g++ -g -o "Debug/GoF-Command.exe" Debug/GoF-Command.o
Debug/GoF-Command.exe
Now Set Command: TRUE
Light State: On!
Now Set Command: FALSE
Light State: Off!
Now On Button was pushed
Light State: On!
Now Off Button was pushed
Light State: Off!