简单工厂模式是最简单的设计模式之一,其实它并不属于GOF的23种设计模式,但应用也十分频繁,同时也是其余创建型模式的基础,因此有必要先学习简单工厂模式。
animals类:动物类(AbstractProduct)
class animals_s {
public:
animals_s() {
}
virtual int get_leg_num() {
return m_leg_num;
}
virtual string get_color() {
return m_color;
}
virtual ~animals_s() {
};
protected:
int m_leg_num;
string m_color;
};
dog类:小狗动物类(Product1)
class dog_s : public animals_s {
public:
dog_s() : animals_s() {
m_leg_num = 4;
m_color = "write";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~dog_s() {
}
};
cock类:小鸡动物类(Product2)
class cock_s : public animals_s {
public:
cock_s() : animals_s() {
m_leg_num = 2;
m_color = "black";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~cock_s() {
}
};
animals_factory_s类:动物生产工厂类(Factory)
class animals_factory_s {
public:
animals_s *create_animals(const string &animals_type) {
animals_s *p_animals_obj = nullptr;
if (animals_type == "dog_s") {
p_animals_obj = new dog_s();
} else if (animals_type == "cock_s") {
p_animals_obj = new cock_s();
}
return p_animals_obj;
}
};
int main(int argc, char** argv) {
unique_ptr<animals_factory_s> m_animals_factory(new animals_factory_s());
unique_ptr<animals_s> dog(m_animals_factory->create_animals("dog_s"));
unique_ptr<animals_s> cock(m_animals_factory->create_animals("cock_s"));
int dog_leg_num;
string dog_color;
if (dog != NULL) {
dog_leg_num = dog->get_leg_num();
dog_color = dog->get_color();
}
int cock_leg_num;
string cock_color;
if (cock != NULL) {
cock_leg_num = cock->get_leg_num();
cock_color = cock->get_color();
}
return 0;
}
增加小猫动物。
cat类:小猫动物类(Product3)
class cat_s : public animals_s {
public:
cat_s() : animals_s() {
m_leg_num = 4;
m_color = "grey";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~cat_s() {
}
};
animals_factory_s类:动物生产工厂类(Factory)
class animals_factory_s {
public:
animals_s *create_animals(const string &animals_type) {
animals_s *p_animals_obj = nullptr;
if (animals_type == "dog_s") {
AINFO << "create dog_s class ";
p_animals_obj = new dog_s();
} else if (animals_type == "cock_s") {
AINFO << "create cock_s class ";
p_animals_obj = new cock_s();
} else if (animals_type == "cat_s") {
AINFO << "create cat_s class ";
p_animals_obj = new cat_s();
}
return p_animals_obj;
}
};
int main(int argc, char** argv) {
unique_ptr<animals_factory_s> m_animals_factory(new animals_factory_s());
unique_ptr<animals_s> dog(m_animals_factory->create_animals("dog_s"));
unique_ptr<animals_s> cock(m_animals_factory->create_animals("cock_s"));
unique_ptr<animals_s> cat(m_animals_factory->create_animals("cat_s"));
int dog_leg_num;
string dog_color;
if (dog != NULL) {
dog_leg_num = dog->get_leg_num();
dog_color = dog->get_color();
}
int cock_leg_num;
string cock_color;
if (cock != NULL) {
cock_leg_num = cock->get_leg_num();
cock_color = cock->get_color();
}
int cat_leg_num;
string cat_color;
if (cat != NULL) {
cat_leg_num = cat->get_leg_num();
cat_color = cat->get_color();
}
return 0;
}
工厂方法模式是使用频率最高的工厂模式。
定义一个用于创建对象的接口(virtual animals_m *create_animals() = 0;),但是让子类决定将哪一个类实例化(子类实现create_animals)。工厂方法模式让一个类的实例化延迟到其子类。
简单工厂模式中,每新增一个具体产品,就需要修改工厂类内部的判断逻辑。为了不修改工厂类,遵循开闭原则,工厂方法模式中不再使用工厂类统一创建所有的具体产品,而是针对不同的产品设计了不同的工厂,每一个工厂只生产特定的产品。
从工厂方法模式简介中,可以知道该模式有以下几种角色:
animals类:动物类(AbstractProduct)
class animals_m {
public:
animals_m() {
}
virtual int get_leg_num() {
return m_leg_num;
}
virtual string get_color() {
return m_color;
}
virtual ~animals_m(){
};
protected:
int m_leg_num;
string m_color;
};
dog类:小狗动物类(Product1)
class dog_m : public animals_m {
public:
dog_m() : animals_m() {
m_leg_num = 4;
m_color = "write";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~dog_m() {
}
};
cock类:小鸡动物类(Product2)
class cock_m : public animals_m {
public:
cock_m() : animals_m() {
m_leg_num = 2;
m_color = "black";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~cock_m() {
}
};
定义一个用于创建对象的接口(virtual animals_m *create_animals() = 0;),当需要扩展时,不需要更改此文件。
AbstractFactory类:生产不同产品的工厂的抽象类
class animals_factory_m {
public:
virtual animals_m *create_animals() = 0;
virtual ~animals_factory_m() {}
};
让子类决定将哪一个类实例化(子类实现create_animals)
dog_factory_m类:生产小狗动物的工厂类(ConcreteFactory1)
class dog_factory_m : public animals_factory_m
{
public:
animals_m *create_animals() {
return new dog_m();
}
};
cock_factory_m类:生产小鸡动物的工厂类(ConcreteFactory2)
class cock_factory_m : public animals_factory_m
{
public:
animals_m *create_animals() {
return new cock_m();
}
};
int main(int argc, char** argv) {
unique_ptr<animals_factory_m> m_dog_factory(new dog_factory_m());
unique_ptr<animals_factory_m> m_cock_factory(new cock_factory_m());
unique_ptr<animals_m> cock(m_cock_factory->create_animals());
unique_ptr<animals_m> dog(m_dog_factory->create_animals());
int dog_leg_num;
string dog_color;
if (dog != NULL) {
dog_leg_num = dog->get_leg_num();
dog_color = dog->get_color();
}
int cock_leg_num;
string cock_color;
if (cock != NULL) {
cock_leg_num = cock->get_leg_num();
cock_color = cock->get_color();
}
}
cat类:小猫动物类(Product3)
class cat_m : public animals_m {
public:
cat_m() : animals_m() {
m_leg_num = 4;
m_color = "grey";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~cat_m() {
}
};
cat_factory_m类:生产小猫动物的工厂类(ConcreteFactory3)
class cat_factory_m : public animals_factory_m
{
public:
animals_m *create_animals() {
return new cat_m();
}
};
int main(int argc, char** argv) {
unique_ptr<animals_factory_m> m_dog_factory(new dog_factory_m());
unique_ptr<animals_m> dog(m_dog_factory->create_animals());
int dog_leg_num;
string dog_color;
if (dog != NULL) {
dog_leg_num = dog->get_leg_num();
dog_color = dog->get_color();
}
unique_ptr<animals_factory_m> m_cock_factory(new cock_factory_m());
unique_ptr<animals_m> cock(m_cock_factory->create_animals());
int cock_leg_num;
string cock_color;
if (cock != NULL) {
cock_leg_num = cock->get_leg_num();
cock_color = cock->get_color();
}
unique_ptr<animals_factory_m> m_cat_factory(new cat_factory_m());
unique_ptr<animals_m> cat(m_cat_factory->create_animals());
int cat_leg_num;
string cat_color;
if (cat != NULL) {
cat_leg_num = cat->get_leg_num();
cat_color = cat->get_color();
}
}
我们每实现一个动物类,就需要实现一个对应的动物工厂类,有点麻烦,其实我们可以通过模板来优化这一步操作。
template <class T>
class factory_t_m : public animals_factory_m
{
public:
virtual animals_m *create_animals() {
return new T();
}
};
int main(int argc, char** argv) {
unique_ptr<animals_factory_m> m_dog_factory(new factory_t_m<dog_m>());
unique_ptr<animals_m> dog(m_dog_factory->create_animals());
int dog_leg_num;
string dog_color;
if (dog != NULL) {
dog_leg_num = dog->get_leg_num();
dog_color = dog->get_color();
}
unique_ptr<animals_factory_m> m_cock_factory(new factory_t_m<cock_m>());
unique_ptr<animals_m> cock(m_cock_factory->create_animals());
int cock_leg_num;
string cock_color;
if (cock != NULL) {
cock_leg_num = cock->get_leg_num();
cock_color = cock->get_color();
}
unique_ptr<animals_factory_m> m_cat_factory(new factory_t_m<cat_m>());
unique_ptr<animals_m> cat(m_cat_factory->create_animals());
int cat_leg_num;
string cat_color;
if (cat != NULL) {
cat_leg_num = cat->get_leg_num();
cat_color = cat->get_color();
}
}
使用模版,在增加动物类时,可以不增加动物工厂类,但是在应用中,需要知道动物类的类名(unique_ptr
提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类。
简言之,一个工厂可以提供创建多种相关产品的接口,而无需像工厂方法一样,为每一个产品都提供一个具体工厂。
抽象工厂模式结构与工厂方法模式结构类似,不同之处在于,一个具体工厂可以生产多种同类相关的产品:
在工厂方法模式基础上,增加系列,比如山地动物,河边动物,丛林动物,那么我们之前的例子就可以分为
山地 | 河边 | 丛林 |
---|---|---|
狗 | 狗 | 狗 |
鸡 | 鸡 | 鸡 |
猫 | 猫 | 猫 |
山地动物类:定义山地动物的接口(AbstractPhoneA)
class hill_animals {
public:
hill_animals() {
}
virtual int get_leg_num() {
return m_leg_num;
}
virtual string get_color() {
return m_color;
}
virtual ~hill_animals(){
};
protected:
int m_leg_num;
string m_color;
};
hill_dog类:山地小狗动物类(Product1)
class hill_dog : public hill_animals {
public:
hill_dog() : hill_animals() {
m_leg_num = 4;
m_color = "black";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~hill_dog() {
}
};
hill_cock类:山地小鸡动物类(Product2)
class hill_cock : public hill_animals {
public:
hill_cock() : hill_animals() {
m_leg_num = 2;
m_color = "black";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~hill_cock() {
}
};
hill_cat类:山地小猫动物类(Product3)
class hill_cat : public hill_animals {
public:
hill_cat() : hill_animals() {
m_leg_num = 4;
m_color = "black";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~hill_cat() {
}
};
河边动物类:定义河边动物的接口(AbstractPhoneB)
class river_animals {
public:
river_animals() {
}
virtual int get_leg_num() {
return m_leg_num;
}
virtual string get_color() {
return m_color;
}
virtual ~river_animals(){
};
protected:
int m_leg_num;
string m_color;
};
river_dog 类:河流小狗动物类(Product1)
class river_dog : public river_animals {
public:
river_dog() : river_animals() {
m_leg_num = 4;
m_color = "white";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~river_dog() {
}
};
river_cock 类:河流小鸡动物类(Product2)
class river_cock : public river_animals {
public:
river_cock() : river_animals() {
m_leg_num = 2;
m_color = "white";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~river_cock() {
}
};
river_cat 类:河流小猫动物类(Product3)
class river_cat : public river_animals {
public:
river_cat() : river_animals() {
m_leg_num = 4;
m_color = "white";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~river_cat() {
}
};
丛林动物类:定义丛林动物的接口(AbstractPhoneC)
class jungle_animals {
public:
jungle_animals() {
}
virtual int get_leg_num() {
return m_leg_num;
}
virtual string get_color() {
return m_color;
}
virtual ~jungle_animals(){
};
protected:
int m_leg_num;
string m_color;
};
jungle_dog类:丛林小狗动物类(Product1)
class jungle_dog : public jungle_animals {
public:
jungle_dog() : jungle_animals() {
m_leg_num = 4;
m_color = "green";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~jungle_dog() {
}
};
jungle_cock类:丛林小鸡动物类(Product2)
class jungle_cock : public jungle_animals {
public:
jungle_cock() : jungle_animals() {
m_leg_num = 2;
m_color = "green";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~jungle_cock() {
}
};
jungle_cat 类:丛林小猫动物类(Product3)
class jungle_cat : public jungle_animals {
public:
jungle_cat() : jungle_animals() {
m_leg_num = 4;
m_color = "green";
}
int get_leg_num() {
return m_leg_num;
}
string get_color() {
return m_color;
}
virtual ~jungle_cat() {
}
};
class animals_factory_a {
public:
virtual hill_animals *create_hill_animals() = 0;
virtual river_animals *create_river_animals() = 0;
virtual jungle_animals *create_jungle_animals() = 0;
virtual ~animals_factory_a() {}
};
小狗工厂类
class dog_factory : public animals_factory_a
{
public:
virtual hill_animals *create_hill_animals() {
return new hill_dog();
}
virtual river_animals *create_river_animals() {
return new river_dog();
}
virtual jungle_animals *create_jungle_animals() {
return new jungle_dog();
}
};
小鸡工厂类
class cock_factory : public animals_factory_a
{
public:
virtual hill_animals *create_hill_animals() {
return new hill_cock();
}
virtual river_animals *create_river_animals() {
return new river_cock();
}
virtual jungle_animals *create_jungle_animals() {
return new jungle_cock();
}
};
小猫工厂类
class cat_factory : public animals_factory_a
{
public:
virtual hill_animals *create_hill_animals() {
return new hill_cat();
}
virtual river_animals *create_river_animals() {
return new river_cat();
}
virtual jungle_animals *create_jungle_animals() {
return new jungle_cat();
}
};
int main(int argc, char** argv) {
unique_ptr<animals_factory_a> dog_bank(new dog_factory());
unique_ptr<hill_animals> hill_dog(dog_bank->create_hill_animals());
unique_ptr<river_animals> river_dog(dog_bank->create_river_animals());
unique_ptr<jungle_animals> jungle_dog(dog_bank->create_jungle_animals());
int dog_leg_num;
string dog_color;
if (hill_dog != NULL) {
dog_leg_num = hill_dog->get_leg_num();
dog_color = hill_dog->get_color();
}
if (river_dog != NULL) {
dog_leg_num = river_dog->get_leg_num();
dog_color = river_dog->get_color();
}
if (jungle_dog != NULL) {
dog_leg_num = jungle_dog->get_leg_num();
dog_color = jungle_dog->get_color();
}
unique_ptr<animals_factory_a> cock_bank(new cock_factory());
unique_ptr<hill_animals> hill_cock(cock_bank->create_hill_animals());
unique_ptr<river_animals> river_cock(cock_bank->create_river_animals());
unique_ptr<jungle_animals> jungle_cock(cock_bank->create_jungle_animals());
int cock_leg_num;
string cock_color;
if (hill_dog != NULL) {
cock_leg_num = hill_cock->get_leg_num();
cock_color = hill_cock->get_color();
}
if (river_dog != NULL) {
cock_leg_num = river_cock->get_leg_num();
cock_color = river_cock->get_color();
}
if (jungle_dog != NULL) {
cock_leg_num = jungle_cock->get_leg_num();
cock_color = jungle_cock->get_color();
}
unique_ptr<animals_factory_a> cat_bank(new cat_factory());
unique_ptr<hill_animals> hill_cat(cat_bank->create_hill_animals());
unique_ptr<river_animals> river_cat(cat_bank->create_river_animals());
unique_ptr<jungle_animals> jungle_cat(cat_bank->create_jungle_animals());
int cat_leg_num;
string cat_color;
if (hill_dog != NULL) {
cat_leg_num = hill_dog->get_leg_num();
cat_color = hill_dog->get_color();
}
if (river_dog != NULL) {
cat_leg_num = river_cat->get_leg_num();
cat_color = river_cat->get_color();
}
if (jungle_dog != NULL) {
cat_leg_num = jungle_cat->get_leg_num();
cat_color = jungle_cat->get_color();
}
}
抽象工厂方法用于创建客户所需产品,同时向客户隐藏某个具体产品类将被实例化的细节,用户只需关心所需产品对应的工厂。
新加入产品系列时,无需修改原有系统,增强了系统的可扩展性,符合开闭原则。
在已有产品系列中添加新产品时需要修改抽象层代码,对原有系统改动较大,违背开闭原则。
C++设计模式----工厂模式
C++设计模式之工厂模式(创建型模式)
C++工厂模式(简单工厂、工厂方法、抽象工厂)
工厂模式——简单工厂模式 && 智能指针的使用
C++创建型模式-工厂模式