▷通过“对象创建” 模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定。它是接口抽象之后的第一步工作。
▷典型模式
▷在软件系统中,经常面临着创建对象的工作;由于需求的变化,需要创建的对象的具体类型经常变化。
▷如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种“封装机制”来避免客户程序和这种“具体对象创建工作”的紧耦合?
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使得一个类的实例化延迟(目的:解耦,手段:虚函数)到子类。
——《设计模式:可复用面向对象软件的基础》
场景:参照书籍-在建立披萨店之后,我们打算成立分店,建立不同风味的披萨店!
//PizzaStore.h
#ifndef FACTORY_PIZZASTORE_H
#define FACTORY_PIZZASTORE_H
#pragma once
#include
#include "Pizza.h"
using namespace std;
//抽象工厂类
class PizzaStore
{
public:
PizzaStore();
~PizzaStore();
virtual Pizza *orderPizza(string type);
//在PizzaStore里,"工厂方法"现在是抽象的。
virtual Pizza *createPizza(string type) = 0; //现在把工厂对象移到这个方法中。
};
#endif //FACTORY_PIZZASTORE_H
//PizzaStore.cpp
#include "PizzaStore.h"
#include
PizzaStore::PizzaStore()
= default;
PizzaStore::~PizzaStore()
= default;
Pizza* PizzaStore::orderPizza(string type)
{
Pizza* pizza = createPizza(std::move(type)); //现在createPizza()方法从工厂对象中移回PizzaStore
pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();
return pizza;
}
//Pizza.h
#ifndef FACTORY_PIZZA_H
#define FACTORY_PIZZA_H
#pragma once
#include
#include
#include
using namespace std;
//抽象产品类
class Pizza
{
protected:
//每个披萨都具有名称(name)\面团类型(dough)\酱料类型(sauce)\一套佐料(toppings)
string name;
string dough;
string sauce;
vector<string> toppings;
public:
Pizza();
~Pizza();
virtual void prepare();
virtual void bake();
virtual void cut();
virtual void box();
virtual string& getName();
};
#endif //FACTORY_PIZZA_H
//Pizza.cpp
#include "Pizza.h"
Pizza::Pizza()
= default;
Pizza::~Pizza()
= default;
void Pizza::prepare()
{
//此抽象类提供了某些默认的基本做法,用来进行烘焙\切片\装盒
cout << "Preparing " << name << endl;
cout << "Tossing dough..." << endl;
cout << "Adding sauce..." << endl;
cout << "Adding toppings: " << endl;
//准备工作需要以特定的顺序进行,有一连串的步骤
for(const auto & topping : toppings)
{
cout << " " << topping << endl;
}
}
//烘焙
void Pizza::bake()
{
cout << "Bake for 25 minutes at 350" << endl;
}
//切片
void Pizza::cut()
{
cout << "Cutting the pizza into diagonal slices" << endl;
}
//装盒
void Pizza::box()
{
cout << "Place pizza in official PizzaStore box" << endl;
}
string& Pizza::getName()
{
return name;
}
//NYPizzaStore.h
#ifndef FACTORY_NYPIZZASTORE_H
#define FACTORY_NYPIZZASTORE_H
#pragma once
#include "PizzaStore.h"
#include "NYStyleCheesePizza.h"
//具体工厂类
//NYPizzaStore扩展PizzaStore,所以拥有odrerPizza()方法(以及其他的方法);
class NYPizzaStore : public PizzaStore
{
public:
NYPizzaStore();
~NYPizzaStore();
//createPizza()返回一个Pizza对象,由子类全权负责该实例化哪一个具体Pizza
Pizza* createPizza(string item) override;
};
#endif //FACTORY_NYPIZZASTORE_H
//NYPizzaStore.cpp
#include "NYPizzaStore.h"
NYPizzaStore::NYPizzaStore()
= default;
NYPizzaStore::~NYPizzaStore()
= default;
Pizza* NYPizzaStore::createPizza(string item)
{
if(item == "cheese")
return new NYStyleCheesePizza;
}
//NYStyleCheesePizza.h
#ifndef FACTORY_NYSTYLECHEESEPIZZA_H
#define FACTORY_NYSTYLECHEESEPIZZA_H
#pragma once
#include "pizza.h"
//具体产品类1
class NYStyleCheesePizza : public Pizza
{
public:
NYStyleCheesePizza();
~NYStyleCheesePizza();
};
#endif //FACTORY_NYSTYLECHEESEPIZZA_H
//NYStyleCheesePizza.cpp
#include "NYStyleCheesePizza.h"
NYStyleCheesePizza::NYStyleCheesePizza()
{
//纽约披萨有自己的大葱番茄(Marinara)和薄饼。
name = "NY Style Sauce and Cheese Pizza";
dough = "Thin Crust Dough";
sauce = "Marinara Sauce";
//上面覆盖的是意大利Reggiano高级奶酪
toppings.emplace_back("Grated Reggiano Cheese");
}
NYStyleCheesePizza::~NYStyleCheesePizza()
= default;
//ChicagoPizzaStore.h
#ifndef FACTORY_CHICAGOPIZZASTORE_H
#define FACTORY_CHICAGOPIZZASTORE_H
#pragma once
#include "PizzaStore.h"
#include "ChicagoStyleCheesePizza.h"
class ChicagoPizzaStore : public PizzaStore
{
public:
ChicagoPizzaStore();
~ChicagoPizzaStore();
Pizza* createPizza(string type) override;
};
#endif //FACTORY_CHICAGOPIZZASTORE_H
//ChicagoPizzaStore.cpp
#include "ChicagoPizzaStore.h"
ChicagoPizzaStore::ChicagoPizzaStore()
= default;
ChicagoPizzaStore::~ChicagoPizzaStore()
= default;
Pizza* ChicagoPizzaStore::createPizza(std::string type)
{
if (type == "cheese")
return new ChicagoStyleCheesePizza;
return nullptr;
}
//ChicagoStyleCheesePizza.h
#ifndef FACTORY_CHICAGOSTYLECHEESEPIZZA_H
#define FACTORY_CHICAGOSTYLECHEESEPIZZA_H
#pragma once
#include "Pizza.h"
//具体产品类2
class ChicagoStyleCheesePizza : public Pizza
{
public:
ChicagoStyleCheesePizza();
~ChicagoStyleCheesePizza();
void cut() override; //覆盖了cut方法
};
#endif //FACTORY_CHICAGOSTYLECHEESEPIZZA_H
//ChicagoStyleCheesePizza.cpp
#include "ChicagoStyleCheesePizza.h"
ChicagoStyleCheesePizza::ChicagoStyleCheesePizza()
{
//芝加哥披萨使用小番茄作为酱料,并使用厚饼。
name = "Chicago Style Deep Fish Cheese Pizza";
dough = "Extra Thick Crust Dough";
sauce = "Plum Tomato Sauce";
//芝加哥风味的 深盘披萨使用许多意大利白干酪(mozzarella)
toppings.emplace_back("Shredded Mozzarella Cheese");
}
ChicagoStyleCheesePizza::~ChicagoStyleCheesePizza()
= default;
void ChicagoStyleCheesePizza::cut()
{
//这个芝加哥风味披萨覆盖了cut()方法,将披萨切成正方形
cout << "Cutting the pizza into square slices" << endl;
}
//main.cpp
//工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。
//工厂方法让类把实例化推迟到子类。
//设计原则:要依赖抽象,不要依赖具体类
#include
#include "NYPizzaStore.h"
#include "ChicagoPizzaStore.h"
using namespace std;
//测试
int main()
{
//建立两个不同的店
NYPizzaStore nyStore;
ChicagoPizzaStore chicagoStore;
Pizza *pizza = nyStore.orderPizza("cheese");
cout << "Ethan ordered a " << pizza->getName() << "\n" << endl;
delete pizza;
pizza = chicagoStore.orderPizza("cheese");
cout << "Joel ordered a " << pizza->getName() << "\n" << endl;
delete pizza;
return 0;
}
欢迎关注公众号:c_302888524
发送:“设计模式:可复用面向对象软件的基础” 获取电子书