零基础网站建设视频,流量网站怎么做,沈阳男科医院哪家口碑好,网站设计文档抽象工厂模式
定义#xff1a;将有关联关系的系列产品放到一个工厂里#xff0c;通过该工厂生产一系列产品。
设计模式有三大分类#xff1a;创建型模式、结构型模式、行为型模式 抽象工厂模式属于创建型模式 上篇 工厂方法模式 提到工厂方法模式中每个工厂只生产一种特定…抽象工厂模式
定义将有关联关系的系列产品放到一个工厂里通过该工厂生产一系列产品。
设计模式有三大分类创建型模式、结构型模式、行为型模式 抽象工厂模式属于创建型模式 上篇 工厂方法模式 提到工厂方法模式中每个工厂只生产一种特定的产品这样会有工厂类太多的问题不容易维护。现实生活中小米工厂除了生产手机还可能生产有关联的平板电脑即一个工厂会生产多个有关联关系的系列产品这种方式即抽象工厂模式。 比如 小米工厂生产 小米手机和小米平板电脑 华为工厂生产 华为手机和华为平板电脑
故事铁蛋所在的富士康公司接到了为小米和华为代工手机和平板的任务这个重任交给了打工人铁蛋铁蛋是超级厉害的铁蛋使用了抽象工厂模式解决了该问题。
铁蛋是怎么做的呢他是这样做的 一、定义手机相关的类手机基类Phone、XiaomiPhone、HuaweiPhone 二、定义平板相关的类平板基类Computer 、XiaomiComputer 、HuaweiComputer 三、定义工厂相关的类工厂基类AbstractFactory、XiaomiFactory、HuaweiFactory 如下
#include iostream
#include string
using namespace std;// 手机基类
class Phone {
public:Phone(string name) : name_(name) {}virtual void InstallPhoSystemOperation() 0;protected:string name_;
};
// 小米手机类
class XiaomiPhone : public Phone {
public:XiaomiPhone(string name) : Phone(name){cout 获得一部小米手机: name endl;}void InstallPhoSystemOperation() {cout 为小米手机安装系统 endl;}
};
// 华为手机类
class HuaweiPhone : public Phone {
public:HuaweiPhone(string name) : Phone(name){cout 获得一部华为手机: name endl;}void InstallPhoSystemOperation() {cout 为华为手机安装系统 endl;}
};// 平板基类
class Computer {
public:Computer(string name) : name_(name) {}virtual void InstallComSystemOperation() 0;protected:string name_;
};
// 小米平板类
class XiaomiComputer : public Computer {
public:XiaomiComputer(string name) : Computer(name){cout 获得一个小米平板: name endl;}void InstallComSystemOperation() {cout 为小米《平板》安装系统 endl;}
};
// 华为平板类
class HuaweiComputer : public Computer {
public:HuaweiComputer(string name) : Computer(name){cout 获得一个华为平板: name endl;}void InstallComSystemOperation() {cout 为华为《平板》安装系统 endl;}
};// 抽象工厂接口
class AbstractFactory {
public:virtual Phone* CreatePhone(string name) 0;virtual Computer* CreateComputer(string name) 0;
};
// 小米工厂
class XiaomiFactory : public AbstractFactory {
public:Phone* CreatePhone(string name){return new XiaomiPhone(name);}Computer* CreateComputer(string name){return new XiaomiComputer(name);}
};
// 华为工厂
class HuaweiFactory : public AbstractFactory {
public:Phone* CreatePhone(string name){return new HuaweiPhone(name);}Computer* CreateComputer(string name){return new HuaweiComputer(name);}
};int main()
{cout ------抽象工厂模式------ endl;// 创建小米工厂AbstractFactory* xiaomiFactory new XiaomiFactory();// 创建华为工厂AbstractFactory* huaweiFactory new HuaweiFactory();//生产小米手机和小米平板Phone* xiaomiPhone xiaomiFactory-CreatePhone(小米11);Computer* xiaomiComputer xiaomiFactory-CreateComputer(平板《MI》);//为小米手机和小米平板安装操作系统xiaomiPhone-InstallPhoSystemOperation();xiaomiComputer-InstallComSystemOperation();//生产华为手机和华为平板Phone* huaweiPhone huaweiFactory-CreatePhone(华为mate60);Computer* huaweiComputer huaweiFactory-CreateComputer(平板《huawei》);//为华为手机和华为平板安装操作系统huaweiPhone-InstallPhoSystemOperation();huaweiComputer-InstallComSystemOperation();delete xiaomiPhone;delete xiaomiComputer;delete huaweiPhone;delete huaweiComputer;delete xiaomiFactory;delete huaweiFactory;return 0;
}运行结果
利用抽象工厂模式成功创建两个工厂成功分别生产小米手机平板 和 华为手机平板 谢谢观看祝顺利 https://shusheng007.top/2020/02/16/abstract-factory-pattern/