问题需求与工厂模式一样,用到了抽象工厂模式----提供一个接口,用于创建相关或依赖对象的家族,而不需要制定具体的类。允许客户使用抽象的接口来创建一组相关的产品,而不需要知道或关心实际产出的具体产品是什么。实现客户和具体产品的解耦。即是披萨店和具体的披萨工厂解耦,用一个抽象类原料工厂实现产品的实例化。
抽象原料工厂类
/******************************/
/*原料工厂*/
/*****************************/
#ifndef INGREDIENTFACTORY_H_
#define INGREDIENTFACTORY_H_
#include<iostream>
#include<string>
#include<vector>
using namespace std;
//抽象工厂接口类
class IngredientFactory{
public:
virtual string createDough() = 0;
virtual string createSauce() = 0;
virtual string createCheese() = 0;
virtual vector<std::string> createVeggies() = 0;
virtual string createPepperoni() = 0;
virtual string createClam() = 0;
};
class NYIngredientFactory:public IngredientFactory{
public:
std::string createDough(){
cout << "制作薄的硬壳面团" << endl;
return "Thin Crust Dough";
}
std::string createSauce(){
cout << "制作Marinara酱汁" << endl;
return "Marinara Sauce";
}
std::string createCheese(){
cout << "制作Reggiano奶酪" << endl;
return "Reggiano Cheese";
}
std::vector<std::string> createVeggies(){
std::vector<std::string> temp;
temp.push_back("Garlic");
temp.push_back("Onion");
temp.push_back("Mushroom");
temp.push_back("Redpepper");
cout << "添加蔬菜-洋葱,蘑菇,红辣椒" << endl;
return temp;
}
std::string createPepperoni(){
cout << "制作辣椒丝" << endl;
return "Sliced Pepperoni";
}
std::string createClam(){
cout << "清洗蛤蜊" << endl;
return "Fresh Clams";
}
};
#endif
产品类
#ifndef PIZZA_H_
#define PIZZA_H_
#include<string>
#include<iostream>
#include"IngredientFactory.h"
using namespace std;
class Pizza{
private:
string name;
string dough;
string sauce;
vector<string> veggies;
string cheese;
string pepperoni;
string clam;
public:
Pizza(){}
virtual ~Pizza(){}
void setName(string& temp){
name = temp;
}
void setDough(string& temp){
dough = temp;
}
void setSauce(string& temp){
sauce = temp;
}
void setVeggies(vector<string>& temp){
veggies = temp;
}
void setCheese(string& temp){
cheese = temp;
}
void setPepperoni(string& temp){
pepperoni = temp;
}
void setClam(string& temp){
clam = temp;
}
virtual void prepare()=0;
virtual void bake(){
cout << "在350度烘烤25分钟" << endl;
}
virtual void cut(){
cout << "切成斜片" << endl;
}
virtual void box(){
cout << "用正规包装袋" << endl;
}
virtual string getName(){
return name;
}
};
class CheesePizza :public Pizza{
public:
CheesePizza(IngredientFactory* temp_factory){
factory = temp_factory;
}
void prepare(){
cout << "正在准备" << getName() << endl;
setDough(factory->createDough());
setSauce(factory->createSauce());
setCheese(factory->createCheese());
}
private:
IngredientFactory* factory;
};
class ClamPizza :public Pizza{
public:
ClamPizza(IngredientFactory* temp_factory){
factory = temp_factory;
}
void prepare(){
cout << "正在准备" << getName() << endl;
setDough(factory->createDough());
setSauce(factory->createSauce());
setCheese(factory->createCheese());
setClam(factory->createClam());
}
private:
IngredientFactory* factory;
};
#endif
客户类(披萨店)只写了一个纽约披萨店
#ifndef PIZZASTORE_H_
#define PIZZASTORE_H_
#include"pizza.h"
enum PizzaType{ cheese = 0, pepperoni, clam, veggie };//Pizaa类型
class PizzaStore{
public:
Pizza* orderPizza(PizzaType type){
Pizza* temp_pizza;
temp_pizza = createPizza(type);
temp_pizza->prepare();
temp_pizza->bake();
temp_pizza->cut();
temp_pizza->box();
return temp_pizza;
}
virtual Pizza* createPizza(PizzaType type) = 0;
};
class NYPizzaStore :public PizzaStore{
public:
Pizza* createPizza(PizzaType type){
Pizza *pizza = NULL;
IngredientFactory* InFactory = new NYIngredientFactory();
string str;
switch (type)
{
case cheese:
pizza = new CheesePizza(InFactory);
str = "纽约风味奶油披萨";
pizza->setName(str);
break;
case clam:
pizza = new ClamPizza(InFactory);
str = "纽约风味蛤蜊披萨";
pizza->setName(str);
break;
/*case cheese:
pizza = new NYStyleCheesePizza();
break;*/
default:
std::cout << "There is not this type of pizze" << std::endl;
}
return pizza;
}
};
//class ChicagoPizzaStore :public PizzaStore{
//public:
// Pizza* createPizza(PizzaType type){
// Pizza *pizza = new Pizza;
// switch (type)
// {
// case cheese:
// pizza = new ChicagoStyleCheesePizza();
// break;
// /*case cheese:
// pizza = new NYStyleCheesePizza();
// break;
// case cheese:
// pizza = new NYStyleCheesePizza();
// break;*/
// default:
// std::cout << "There is not this type of pizze" << std::endl;
// }
// return pizza;
// }
//};
#endif
主函数测试
#include"IngredientFactory.h"
#include"Pizza.h"
#include"PizzaStore.h"
int main()
{
cout << "-----小明的订单-----" << endl;
PizzaStore* nyPizzaStore = new NYPizzaStore();
nyPizzaStore->orderPizza(cheese);
nyPizzaStore->createPizza(cheese);
cout << endl;
cout << endl;
cout << "-----小红的订单-----" << endl;
//PizzaStore* nyPizzaStore = new NYPizzaStore();
nyPizzaStore->orderPizza(clam);
nyPizzaStore->createPizza(clam);
return 0;
}
运行结果
工厂方法和抽象工厂方法都是负责创建对象,工厂方法是利用继承创建,抽象工厂方法是利用的对象组合。
工厂方法通过子类创建对象,客户只需知道他们所使用的抽象类型就可以然后由子类负责决定具体类型,实现了客户和具体类型中解耦。
抽象工厂方法提供一个用来创建产品家族的抽象类型,这个类型的子类定义了产品被产生的方法。要想使用这个工厂,必须要先实例化它,然后将它传入一些针对抽象类型写的代码中,实现客户和具体产品的解耦。
最后说一下抽象工厂模式的设计原则:依赖抽象,不依赖具体类。
需要再看,虽然代码写完了,但依旧感觉糊里糊涂的,总结也没有写清楚。