前言
抽象工厂模式可以认为是简单工厂模式的进阶版,不同的地方在于抽象工厂在工厂类层面还有一层继承关系,这种抽象化的方法,可以让原来一个
具体化的对象创建变成一类
对象的创建,最常见的就是不同操作系统下的程序的创建,可以采用抽象工厂模式,因为一个操作系统下的操作肯定是一类的操作,使用简单工厂,分支判断情况较多(当然,“反射机制“能够让工厂的创建过程更加简单明了)。
抽象工厂模式
1. 概要
2. 实现方法
/*************** 抽象工厂模式 *************/
#ifndef ABSTRACT_FACTORY_H
#define ABSTRACT_FACTORY_H
#include <iostream>
using namespace std;
//与简单工厂不同的地方是,简单工厂只是生产单一的产品
//抽象工厂的工厂生产多个产品(适用于产品种类多,而客户端只有一个产品接口)
//抽象工厂在工厂方面还有一次继承关系
//抽象产品类1
class Tank1
{
public:
virtual const string & type() = 0;
virtual ~Tank1(){ cout << "~Tank1" << endl;}
};
//具体产品类A1
class Tank1_A : public Tank1{
public:
Tank1_A():Tank1(),m_strType("Tank1_A"){ }
const string & type() override{
cout << m_strType.data() << endl;
return m_strType;
}
private:
string m_strType;
};
//具体产品类B1
class Tank1_B : public Tank1{
public:
Tank1_B():Tank1(),m_strType("Tank1_B"){ }
const string & type() override{
cout << m_strType.data() << endl;
return m_strType;
}
private:
string m_strType;
};
//抽象产品类2
class Tank2
{
public:
virtual const string & type() = 0;
virtual ~Tank2(){ cout << "~Tank2" << endl;}
};
//具体产品类A2
class Tank2_A : public Tank2{
public:
Tank2_A():Tank2(),m_strType("Tank2_A"){ }
const string & type() override{
cout << m_strType.data() << endl;
return m_strType;
}
private:
string m_strType;
};
//具体产品类B2
class Tank2_B : public Tank2{
public:
Tank2_B():Tank2(),m_strType("Tank2_B"){ }
const string & type() override{
cout << m_strType.data() << endl;
return m_strType;
}
private:
string m_strType;
};
//抽象工厂基类
class Tank_AbstrackFactory{
public:
virtual Tank1* createTank1() = 0;
virtual Tank2 *createTank2() = 0;
virtual ~Tank_AbstrackFactory(){}
};
//创建都是A类产品的工厂
class Tank_Factory_A : public Tank_AbstrackFactory{
virtual Tank1* createTank1(){
return new Tank1_A();
}
virtual Tank2 *createTank2(){
return new Tank2_A();
}
};
//创建都是B类产品的工厂
class Tank_Factory_B : public Tank_AbstrackFactory{
virtual Tank1* createTank1(){
return new Tank1_B();
}
virtual Tank2 *createTank2(){
return new Tank2_B();
}
};
//抽象工厂的应用实例
class Abstract_Factory
{
public:
Abstract_Factory();
//抽象工厂的例子
void run_example(){
cout << "****** Abstract_Factory ******" << endl;
Tank_AbstrackFactory *factory_A = new Tank_Factory_A();//创建A工厂
Tank_AbstrackFactory *factory_B = new Tank_Factory_B();//创建B工厂
Tank1 *tank_A1 = factory_A->createTank1();//实例化对象A1
Tank2 *tank_A2 = factory_A->createTank2();//实例化对象A2
Tank1 *tank_B1 = factory_B->createTank1();//实例化对象B1
Tank2 *tank_B2 = factory_B->createTank2();//实例化对象B2
tank_A1->type();
tank_B1->type();
tank_A2->type();
tank_B2->type();
delete tank_A1;
tank_A1 = nullptr;
delete tank_B1;
tank_B1 = nullptr;
delete tank_A2;
tank_A2 = nullptr;
delete tank_B2;
tank_B2 = nullptr;
delete factory_A;
factory_A = nullptr;
delete factory_B;
factory_B = nullptr;
}
};
#endif // ABSTRACT_FACTORY_H
3. 设计模式总结
总之,与简单工厂的模式一样,其实现是让创建过程推迟到子类中,不同的地方主要是简单工厂生产的是单一产品,抽象工厂可以是多个具有相同特性的产品。
另外,C++中的一些设计模式的总结导航,可以参考这里(设计模式总结)。
参考资料
[1] https://www.runoob.com/design-pattern/factory-pattern.html
[2] https://www.cnblogs.com/chengjundu/p/8473564.html
原文链接: https://www.cnblogs.com/treature/p/13198784.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/359511
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!