简单工厂模式Factory Patten:将创建对象的代码放到一个单独的工厂类中管理。也有人说,简单工厂模式不是一个设计模式,反而比较像是一种编程习惯。
工厂方法模式:定义了一个创建对象的接口,但由子类决定实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
抽象工厂模式:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
OO设计原则:依赖倒置原则,要依赖抽象,不要依赖具体类。
1、提供Pizza原料,使用抽象工厂模式
class PizzaIngredientFactory
{
public:
virtual void CreateDough() = 0;
virtual void CreateSauce() = 0;
virtual void CreateToppings() = 0;
virtual void CreateCheese() = 0;
virtual void CreateClam() = 0;
};
class NYPizzaIngredientFactory : public PizzaIngredientFactory
{
public:
virtual void CreateDough() { std::cout<<" Creating NY dough... "<<std::endl; };
virtual void CreateSauce() { std::cout<<" Creating NY souce... "<<std::endl; };
virtual void CreateToppings(){ std::cout<<" Creating NY toppings... "<<std::endl; };
virtual void CreateCheese() {};
virtual void CreateClam() {};
};
class ChicagoPizzaIngredientFactory : public PizzaIngredientFactory
{
public:
virtual void CreateDough() { std::cout<<" Creating Chicago dough... "<<std::endl; };
virtual void CreateSauce() { std::cout<<" Creating Chicago souce... "<<std::endl; };
virtual void CreateToppings(){ std::cout<<" Creating Chicago toppings... "<<std::endl; };
virtual void CreateCheese() {};
virtual void CreateClam() {};
};
{
public:
virtual void CreateDough() = 0;
virtual void CreateSauce() = 0;
virtual void CreateToppings() = 0;
virtual void CreateCheese() = 0;
virtual void CreateClam() = 0;
};
class NYPizzaIngredientFactory : public PizzaIngredientFactory
{
public:
virtual void CreateDough() { std::cout<<" Creating NY dough... "<<std::endl; };
virtual void CreateSauce() { std::cout<<" Creating NY souce... "<<std::endl; };
virtual void CreateToppings(){ std::cout<<" Creating NY toppings... "<<std::endl; };
virtual void CreateCheese() {};
virtual void CreateClam() {};
};
class ChicagoPizzaIngredientFactory : public PizzaIngredientFactory
{
public:
virtual void CreateDough() { std::cout<<" Creating Chicago dough... "<<std::endl; };
virtual void CreateSauce() { std::cout<<" Creating Chicago souce... "<<std::endl; };
virtual void CreateToppings(){ std::cout<<" Creating Chicago toppings... "<<std::endl; };
virtual void CreateCheese() {};
virtual void CreateClam() {};
};
2、定义 NYStylePizza 和 ChicagoStylePizza
class Pizza
{
public:
void Prepare()
{
std::cout<<"1.Prepareing "<<_name.c_str()<<std::endl;
_ingredientFactory->CreateDough(); /*抽象工厂创建原料*/
_ingredientFactory->CreateSauce(); /*抽象工厂创建原料*/
_ingredientFactory->CreateToppings(); /*抽象工厂创建原料*/
}
void Bake()
{
std::cout<<"2.Bake for 25 minutes at 350 degress."<<std::endl;
}
void Cut()
{
std::cout<<"3.Cutting the pizza into diagonal slices."<<std::endl;
}
void Box()
{
std::cout<<"4.Place pizza in offical PizzaStore box."<<std::endl;
}
std::string GetName()
{
return _name;
}
protected:
std::string _name;
std::string _dough; /*面团*/
std::string _sauce; /*调味汁*/
std::string _toppings; /*糕点上的装饰配料*/
PizzaIngredientFactory *_ingredientFactory;
};
class NYStyleCheesePizza : public Pizza
{
public:
NYStyleCheesePizza(PizzaIngredientFactory *ingredientFactory)
{
_name = "NY Style Sauce and Cheese Pizza";
_ingredientFactory = ingredientFactory;
}
};
class NYStyleClamPizza : public Pizza /*捞蛤Pizza*/
{
public:
NYStyleClamPizza(PizzaIngredientFactory *ingredientFactory)
{
_name = "NY Style Sauce and Clam Pizza";
_ingredientFactory = ingredientFactory;
}
};
class ChicagoStyleCheesePizza : public Pizza
{
public:
ChicagoStyleCheesePizza(PizzaIngredientFactory *ingredientFactory)
{
_name = "Chicago Style Sauce and Cheese Pizza";
_ingredientFactory = ingredientFactory;
}
};
class ChicagoStyleClamPizza : public Pizza /*辣香肠Pizza*/
{
public:
ChicagoStyleClamPizza(PizzaIngredientFactory *ingredientFactory)
{
_name = "Chicago Style Sauce and Clam Pizza";
_ingredientFactory = ingredientFactory;
}
};
3、Pizza店,创建pizza制作pizza,使用工厂方法模式
class PizzaStore
{
public:
Pizza* OrderPizza(std::string type)
{
Pizza *pizza;
pizza = CreatePizza(type);
pizza->Prepare();
pizza->Bake();
pizza->Cut();
pizza->Box();
return pizza;
}
virtual Pizza* CreatePizza(std::string type) = 0; /*工厂方法:把创建对象交给子类,由子类决定创建什么Pizza*/
};
/*工厂方法类*/
class NYStylePizzaStore : public PizzaStore
{
public:
NYStylePizzaStore()
{
_ingredientFactory = new NYPizzaIngredientFactory();
}
virtual Pizza* CreatePizza(std::string type)
{
if (0 == type.compare("cheese"))
{
return new NYStyleCheesePizza(_ingredientFactory);
}
else if (0 == type.compare("clam"))
{
return new NYStyleClamPizza(_ingredientFactory);
}
//else if (0 == type.compare("veggie"))
//else if (0 == type.compare("pepperoni"))
else return NULL;
}
private:
PizzaIngredientFactory *_ingredientFactory;
};
/*工厂方法类*/
class ChicagoStylePizzaStore : public PizzaStore
{
public:
ChicagoStylePizzaStore()
{
_ingredientFactory = new ChicagoPizzaIngredientFactory();
}
virtual Pizza* CreatePizza(std::string type)
{
if (0 == type.compare("cheese"))
{
return new ChicagoStyleCheesePizza(_ingredientFactory);
}
else if (0 == type.compare("clam"))
{
return new ChicagoStyleClamPizza(_ingredientFactory);
}
//else if (0 == type.compare("veggie"))
//else if (0 == type.compare("pepperoni"))
else return NULL;
}
private:
PizzaIngredientFactory *_ingredientFactory;
};
{
public:
Pizza* OrderPizza(std::string type)
{
Pizza *pizza;
pizza = CreatePizza(type);
pizza->Prepare();
pizza->Bake();
pizza->Cut();
pizza->Box();
return pizza;
}
virtual Pizza* CreatePizza(std::string type) = 0; /*工厂方法:把创建对象交给子类,由子类决定创建什么Pizza*/
};
/*工厂方法类*/
class NYStylePizzaStore : public PizzaStore
{
public:
NYStylePizzaStore()
{
_ingredientFactory = new NYPizzaIngredientFactory();
}
virtual Pizza* CreatePizza(std::string type)
{
if (0 == type.compare("cheese"))
{
return new NYStyleCheesePizza(_ingredientFactory);
}
else if (0 == type.compare("clam"))
{
return new NYStyleClamPizza(_ingredientFactory);
}
//else if (0 == type.compare("veggie"))
//else if (0 == type.compare("pepperoni"))
else return NULL;
}
private:
PizzaIngredientFactory *_ingredientFactory;
};
/*工厂方法类*/
class ChicagoStylePizzaStore : public PizzaStore
{
public:
ChicagoStylePizzaStore()
{
_ingredientFactory = new ChicagoPizzaIngredientFactory();
}
virtual Pizza* CreatePizza(std::string type)
{
if (0 == type.compare("cheese"))
{
return new ChicagoStyleCheesePizza(_ingredientFactory);
}
else if (0 == type.compare("clam"))
{
return new ChicagoStyleClamPizza(_ingredientFactory);
}
//else if (0 == type.compare("veggie"))
//else if (0 == type.compare("pepperoni"))
else return NULL;
}
private:
PizzaIngredientFactory *_ingredientFactory;
};
4、测试代码
int main()
{
std::cout<<"Factory Patten"<<std::endl<<std::endl;
PizzaStore *nyStore = new NYStylePizzaStore();
Pizza *pizza = nyStore->OrderPizza("cheese");
std::cout<<"Jack ordered a "<<pizza->GetName().c_str()<<"."<<std::endl<<std::endl;
PizzaStore *chicagoStore = new ChicagoStylePizzaStore();
Pizza *pizza2 = chicagoStore->OrderPizza("clam");
std::cout<<"Joel ordered a "<<pizza2->GetName().c_str()<<"."<<std::endl<<std::endl;
return 0;
}