设计模式_05 工厂模式
5 工厂模式
5.1 简单工厂模式
5.1.1 结构
抽象产品:定义了产品的规范,描述了产品的主要特性和性能。
具体产品:实现或继承抽象产品的子类。
具体工厂:提供了创建产品的方法,调用者通过该方法来获取产品。
5.1.2 实现
5.1.2.1 UML图
5.1.2.2 代码
class coffee {
public:
virtual string getName() {}
void addMilk() {
cout << "加奶" << endl;
}
void addSuger() {
cout << "加糖" << endl;
}
};
class AmericanCoffee : public coffee {
public:
string getName() {
return "美式咖啡";
}
};
class LatteCoffee : public coffee {
public:
string getName() {
return "拿铁咖啡";
}
};
class simpleCoffeeFactory {
public:
coffee* createCoffee(string type) {
coffee* coffeetype = NULL;
if (type == "american")
coffeetype = new AmericanCoffee();
else if (type == "latte")
coffeetype = new LatteCoffee();
return coffeetype;
}
};
class coffeeStore {
public:
coffee* getCoffee(string type) {
simpleCoffeeFactory* factory = new simpleCoffeeFactory();
coffee* Coffee = factory->createCoffee(type);
Coffee->addMilk();
Coffee->addSuger();
cout <<Coffee->getName() <<endl;
return Coffee;
}
};
5.1.3 静态工厂
将工厂类中创建对象的方法设置为静态;
class simpleCoffeeFactory {
public:
static coffee* createCoffee(string type) { //将创建对象的方法设置为静态
coffee* coffeetype = NULL;
if (type == "american")
coffeetype = new AmericanCoffee();
else if (type == "latte")
coffeetype = new LatteCoffee();
return coffeetype;
}
};
class coffeeStore {
public:
coffee* getCoffee(string type) {
coffee* Coffee = simpleCoffeeFactory::createCoffee(type); //直接调用方法,无需实例化工厂类
Coffee->addMilk();
Coffee->addSuger();
cout <<Coffee->getName() <<endl;
return Coffee;
}
};
5.1.4 缺点
违背了开闭原则。
5.2 工厂方法模式
完全遵循了开闭原则。
5.2.1 概念
定义一个用于创建对象的接口,让子类决定实例化哪个产品类的对象。使一个产品的实例化延迟到其子类中。
5.2.2 结构
抽象工厂:提供创建产品的接口,调用者通过它访问具体工厂的工厂方法来创建产品。
具体工厂:实现抽象工厂中的方法,完成具体产品的创建。
抽象产品:定义了产品的规范,描述了产品的主要特性和性能。
具体产品:实现抽象产品角色所定义的接口,由具体工厂来创建,同具体工厂之间一一对应。
5.2.3 实现
5.2.3.1 UML图
5.2.3.2 代码
#include<iostream>
using namespace std;
//咖啡类
class coffee {
public:
virtual string getName() {
return NULL;
}
void addMilk() {
cout << "加奶" << endl;
}
void addSuger() {
cout << "加糖" << endl;
}
};
class AmericanCoffee : public coffee {
public:
string getName() {
return "美式咖啡";
}
};
class LatteCoffee : public coffee {
public:
string getName() {
return "拿铁咖啡";
}
};
//咖啡工厂类
class CoffeeFactory {
public:
virtual coffee* createCoffee() = 0;
};
class AmericanCoffeeFactory : public CoffeeFactory {
public:
coffee* createCoffee() {
return new AmericanCoffee();
}
};
class LatteCoffeeFactory : public CoffeeFactory {
public:
coffee* createCoffee() {
return new LatteCoffee();
}
};
//咖啡店类
class coffeeStore {
private:
CoffeeFactory* factory = NULL;
public:
void setFactory(CoffeeFactory* factory) {
this->factory = factory;
}
coffee* getCoffee() {
coffee* Coffee = factory->createCoffee();
Coffee->addMilk();
Coffee->addSuger();
cout << Coffee->getName() << endl;
return Coffee;
}
};
int main() {
coffeeStore store;
store.setFactory(new AmericanCoffeeFactory());
store.getCoffee();
store.setFactory(new LatteCoffeeFactory());
store.getCoffee();
return 0;
}
5.2.4 优缺点
优点:满足开闭原则。
缺点:每一新增产品都需要增加对应的具体工厂类和产品类,增加复杂度。
5.3 抽象工厂模式
工厂方法模式只考虑生产同级别产品,现实生活中许多工厂是综合性工厂。
工厂方法只生产一个等级的产品,抽象工厂生产多个等级的产品。
5.3.1 结构
抽象工厂:提供创建产品的接口,包含多个创建产品的方法,可以创建多个不同等级的产品。。
具体工厂:实现抽象工厂中的多个抽象方法,完成具体产品的创建。
抽象产品:定义了产品的规范,描述了产品的主要特性和性能,抽象产品模式有多个抽象产品。。
具体产品:实现抽象产品角色所定义的接口,由具体工厂来创建,同具体工厂之间是多对一关系。
5.3.2 实现
5.3.2.1 UML图
5.3.2.2 代码
#include<iostream>
using namespace std;
//咖啡类
class Coffee {
public:
virtual void getName() {}
void addMilk() {
cout << "加奶" << endl;
}
void addSuger() {
cout << "加糖" << endl;
}
};
class AmericanCoffee : public Coffee {
public:
void getName() {
cout <<"美式咖啡" <<endl;
}
};
class LatteCoffee : public Coffee {
public:
void getName() {
cout << "拿铁咖啡" << endl;
}
};
//甜品类
class Dessert {
public:
virtual void getName() {}
};
class AmericanDessert : public Dessert {
public:
void getName() {
cout<< "美式甜点" <<endl;
}
};
class LatteDessert : public Dessert {
public:
void getName() {
cout << "拿铁甜点" << endl;
}
};
//工厂类
class Factory {
public:
virtual Coffee* createCoffee() = 0;
virtual Dessert* createDessert() = 0;
};
class AmericanFactory : public Factory {
public:
Coffee* createCoffee() {
return new AmericanCoffee();
}
Dessert* createDessert() {
return new AmericanDessert();
}
};
class LatteFactory : public Factory {
public:
Coffee* createCoffee() {
return new LatteCoffee();
}
Dessert* createDessert() {
return new LatteDessert();
}
};
//咖啡店类
class coffeeStore {
private:
Factory* factory = NULL;
public:
void setFactory(Factory* factory) {
this->factory = factory;
}
Coffee* getCoffee() {
Coffee* coffee = factory->createCoffee();
coffee->getName();
coffee->addMilk();
coffee->addSuger();
return coffee;
}
Dessert* getDessert() {
Dessert* dessert = factory->createDessert();
dessert->getName();
return dessert;
}
};
int main() {
coffeeStore store;
store.setFactory(new AmericanFactory());
store.getCoffee();
cout << endl;
store.getDessert();
cout << endl;
store.setFactory(new LatteFactory());
store.getCoffee();
cout << endl;
store.getDessert();
cout << endl;
return 0;
}
5.3.3 优缺点
5.3.3.1 优点
当一个产品族的多个对象设计成一起工作时,保证客户端始终只使用同一个产品族中的对象。
5.3.3.2 缺点
当产品族需要增加一个新产品时,所有的工厂类都需要进行修改。
不同产品级别产品族的种类必须相同。
5.3.4 使用场景
输入法换皮肤。生产不同操作系统的程序。
5.4 模式扩展
简单工厂+模式扩展用于解耦合。
在工厂类中加载配置文件中的全类名,并通过反射创建对象进行存储。客户端如果需要对象,直接进行获取即可。