C++11 设计模式6. 建造者模式,也叫做生成器模式

news/2024/5/18 18:30:36

一 什么是建造者模式?

// 小木公司对于SQL有配置,因此要输入sql url,然后输入 sql username,然后是 sql ps
//因此小木就想到了使用 SystemConfig这个类来完成上述的三个初始化

//5.1号小木公司加入了redis的缓存机制,redis也有 url,username,password,三个字符串

//10.1之后,小木公司加入了kafka机制,让log都存储到kafka中,kafka也有url,username,password,三个字符串

//说不定过年后,还有加上其他配置项。这时候小木意识到问题的严重性了,
//每次改动都一堆参数,且容易出现问题

// 小木公司对于SQL有配置,因此要输入sql url,然后输入 sql username,然后是 sql ps
//因此小木就想到了使用 SystemConfig这个类来完成上述的三个初始化//5.1号小木公司加入了redis的缓存机制,redis也有 url,username,password,三个字符串//10.1之后,小木公司加入了kafka机制,让log都存储到kafka中,kafka也有url,username,password,三个字符串//说不定过年后,还有加上其他配置项。这时候小木意识到问题的严重性了,
//每次改动都一堆参数,且容易出现问题
#include <iostream>
#include <string>
using namespace std;class SystemConfig {public:SystemConfig(string sqlurl,string sqlusername,string sqlpassword,string redisurl, string redisusername, string redispassword,string kafkaurl, string kafkausername, string kafkapassword): MySQL_URL(sqlurl), MySQL_username(sqlusername), MySQL_password(sqlpassword),REDIS_URL(redisurl), REDIS_username(redisusername), REDIS_password(redispassword),KAFKA_URL(kafkaurl), KAFKA_username(kafkausername), KAFKA_password(kafkapassword){}void printfSysConfig() {cout << "MySQL_URL = " << MySQL_URL<< "   MySQL_username = " << MySQL_username<< "   MySQL_password = " << MySQL_password<< "   REDIS_URL = " << REDIS_URL<< "   REDIS_username = " << REDIS_username<< "   REDIS_password = " << REDIS_password<< "   KAFKA_URL = " << KAFKA_URL<< "   KAFKA_username = " << KAFKA_username<< "   KAFKA_password = " << KAFKA_password<< endl;}
private:string MySQL_URL;string MySQL_username;string MySQL_password;//5.1之后string REDIS_URL;string REDIS_username;string REDIS_password;//10.1之后string KAFKA_URL;string KAFKA_username;string KAFKA_password;
};//想到的第一解决方案是:我们将这些属性都弄成函数,class SystemConfig1 {public:SystemConfig1() {}void setMySQLConfig(string sqlurl, string sqlusername, string sqlpassword) {this->MySQL_URL = sqlurl;this->MySQL_username = sqlusername;this->MySQL_password = sqlpassword;}void setRedisConfig(string redisurl, string redisusername, string redispassword) {this->REDIS_URL = redisurl;this->REDIS_username = redisusername;this->REDIS_password = redispassword;}void setKAFKAConfig(string kafkaurl, string kafkausername, string kafkapassword) {this->KAFKA_URL = kafkaurl;this->KAFKA_username = kafkausername;this->KAFKA_password = kafkapassword;}void printfSysConfig() {cout << "MySQL_URL = " << MySQL_URL<< "   MySQL_username = " << MySQL_username<< "   MySQL_password = " << MySQL_password<< "   REDIS_URL = " << REDIS_URL<< "   REDIS_username = " << REDIS_username<< "   REDIS_password = " << REDIS_password<< "   KAFKA_URL = " << KAFKA_URL<< "   KAFKA_username = " << KAFKA_username<< "   KAFKA_password = " << KAFKA_password<< endl;}
private:string MySQL_URL;string MySQL_username;string MySQL_password;//5.1之后string REDIS_URL;string REDIS_username;string REDIS_password;//10.1之后string KAFKA_URL;string KAFKA_username;string KAFKA_password;
};//构造者模式
//创建一个sustemconfig的build类,创建systemconfig的过程全都交给这个systemconfig类来实现class SystemConfig2 {
public:SystemConfig2() {}void printfSysConfig() {cout << "MySQL_URL = " << MySQL_URL<< "   MySQL_username = " << MySQL_username<< "   MySQL_password = " << MySQL_password<< "   REDIS_URL = " << REDIS_URL<< "   REDIS_username = " << REDIS_username<< "   REDIS_password = " << REDIS_password<< "   KAFKA_URL = " << KAFKA_URL<< "   KAFKA_username = " << KAFKA_username<< "   KAFKA_password = " << KAFKA_password<< endl;}
public:string MySQL_URL;string MySQL_username;string MySQL_password;//5.1之后string REDIS_URL;string REDIS_username;string REDIS_password;//10.1之后string KAFKA_URL;string KAFKA_username;string KAFKA_password;
};class SystemConfig2Build {public:SystemConfig2Build() {}SystemConfig2 & getSystemConfig() {return systemconfig;}void setMySQLConfig(string sqlurl, string sqlusername, string sqlpassword) {systemconfig.MySQL_URL = sqlurl;systemconfig.MySQL_username = sqlusername;systemconfig.MySQL_password = sqlpassword;}void setRedisConfig(string redisurl, string redisusername, string redispassword) {systemconfig.REDIS_URL = redisurl;systemconfig.REDIS_username = redisusername;systemconfig.REDIS_password = redispassword;}void setKAFKAConfig(string kafkaurl, string kafkausername, string kafkapassword) {systemconfig.KAFKA_URL = kafkaurl;systemconfig.KAFKA_username = kafkausername;systemconfig.KAFKA_password = kafkapassword;}private:SystemConfig2  systemconfig;
};class CompanyA {
public:virtual SystemConfig2 & buildSystemConfig() {scbuild2.setMySQLConfig("11a", "22a", "33a");scbuild2.setRedisConfig("44a", "55a", "66a");scbuild2.setKAFKAConfig("77a", "88a", "99a");return scbuild2.getSystemConfig();}private:SystemConfig2Build scbuild2;
};class CompanyB {
public:SystemConfig2 & buildSystemConfig() {scbuild2.setMySQLConfig("11B", "22B", "33B");scbuild2.setRedisConfig("44B", "55B", "66B");scbuild2.setKAFKAConfig("77B", "88B", "99B");return scbuild2.getSystemConfig();}private:SystemConfig2Build scbuild2;
};class CompanyDiect {
public:virtual SystemConfig2 & buildSystemConfig() = 0;virtual ~CompanyDiect() {//注意父类的 析构函数 要写成 virtual形式的。}
private:SystemConfig2Build scbuild2;
};//c公司
class CompanyDiectc:public CompanyDiect {
public:virtual SystemConfig2 & buildSystemConfig() {scbuild2.setMySQLConfig("11c", "22c", "33c");scbuild2.setRedisConfig("44c", "55c", "66c");scbuild2.setKAFKAConfig("77c", "88c", "99c");return scbuild2.getSystemConfig();}private:SystemConfig2Build scbuild2;
};class CompanyDiectd :public CompanyDiect {
public:virtual SystemConfig2 & buildSystemConfig() {scbuild2.setMySQLConfig("11d", "22d", "33d");scbuild2.setRedisConfig("44d", "55d", "66d");scbuild2.setKAFKAConfig("77d", "88d", "99d");return scbuild2.getSystemConfig();}private:SystemConfig2Build scbuild2;
};int main()
{_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);//程序退出时检测内存泄漏并显示到“输出”窗口std::cout << "Hello World!\n";SystemConfig sc = SystemConfig("aa","bb","cc","dd","ee","ff","11","22","33");sc.printfSysConfig();//首先想到的fix方案,但是有个问题,就是开出来了public接口,让user可以调用,这不怎么好。//也就是说破坏了原有的systemConfig的封装性,让user可以有机会再次改动。虽然不得不这么干。SystemConfig1 sc1 = SystemConfig1();sc1.setMySQLConfig("SQL11", "SQL22", "SQL33");sc1.setRedisConfig("redis1", "redis2", "redis3");sc1.setKAFKAConfig("kafka1", "kafka2", "kafka3");sc1.printfSysConfig();//构造者模式的调用SystemConfig2Build scbuild2;scbuild2.setMySQLConfig("11", "22", "33");scbuild2.setRedisConfig("44", "55", "66");scbuild2.setKAFKAConfig("77", "88", "99");SystemConfig2 sc2 = scbuild2.getSystemConfig();sc2.printfSysConfig();//我们看到上述构造者模式的调用,每次都要传递参数进入,实际开发中,我们可能对于A,B公司的配置基本是不变的,也就是说,我们需要一些预选项CompanyA ca;SystemConfig2 sca = ca.buildSystemConfig();sca.printfSysConfig();//B公司CompanyB cB;SystemConfig2 scB = cB.buildSystemConfig();scB.printfSysConfig();//我们看代码知道 CompanyA 和 CompanyB 的代码是完全一样的,因此可以抽取成父类,这样CompanyDiect * comabs;comabs = new CompanyDiectc(); //多态完成SystemConfig2 scconn = comabs->buildSystemConfig();scconn.printfSysConfig();delete comabs;}

        建造者模式(也被成为⽣成器模式),是⼀种创建型设计模式,软件开发过程中有的时候需要创建很复杂的对象,⽽建造者模式的主要思想是将对象的构建过程分为多个步骤,并为每个步骤定义⼀个抽象的接⼝。具体的构建过程由实现了这些接⼝的具体建造者类来完成。同时有⼀个指导者类负责协调建造者的⼯作,按照⼀定的顺序或逻辑来执⾏构建步骤,最终⽣成产品。
        举个例⼦,假如我们要创建⼀个计算机对象,计算机由很多组件组成,例如 CPU、内存、硬盘、显卡等每个组件可能有不同的型号、配置和制造,这个时候计算机就可以被视为⼀个复杂对象,构建过程相对复杂,⽽我们使⽤建造者模式将计算机的构建过程封装在⼀个具体的建造者类中,⽽指导者类则负责指导构建的步骤和顺序。每个具体的建造者类可以负责构建不同型号或配置的计算机,客户端代码可以通过选择不同的建造者来创建不同类型的计算机,这样就可以根据需要构建不同表示的复杂对象,更加灵活。

假设我们要创建一个计算机对象,普通的写法如下:

// 004构建者模式.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <string>
using namespace std;//cpu 
class CPU {
public:CPU(string cpuname) :_cpuname(cpuname) {cout << "CPU 构造方法被调用 _cpuname = " << _cpuname << endl;}
public:string _cpuname;};class Memory {
public:Memory(string memname) :_memname(memname) {cout << "Memory 构造方法被调用 _memname = " << memname << endl;}
public:string _memname;
};class Harddisk {
public:Harddisk(string diskname) :_diskname(diskname) {cout << "Harddisk 构造方法被调用 diskname = " << diskname << endl;}
public:string _diskname;
};class GraphicsCard {
public:GraphicsCard(string gcname) :_gcname(gcname) {cout << "GraphicsCard 构造方法被调用 _gcname = " << _gcname << endl;}
public:string _gcname;
};//如果是普通的写法
//假如我们要创建⼀个计算机对象,计算机由很多组件组成,
//例如 CPU、内存、硬盘、显卡等。
//每个组件可能有不同的型号、配置和制造,
//这个时候计算机就可以被视为⼀个复杂对象
class Computer {
public:Computer(CPU *cpu, Memory *mem, Harddisk *hdisk, GraphicsCard *gc):_cpu(cpu),_mem(mem),_hdisk(hdisk),_gc(gc) {cout << "Computer 构造方法被调用" << endl;}~Computer() {cout << "Computer 析构方法被调用" << endl;}void printfCom() {cout << "cpu = " << this->_cpu->_cpuname<< "   mem = " << this->_mem->_memname<< "   hdisk = " << this->_hdisk->_diskname<< "   gc = " << this->_gc->_gcname<<endl;}
protected:CPU *_cpu;Memory *_mem;Harddisk *_hdisk;GraphicsCard *_gc;
};int main()
{std::cout << "Hello World!\n";CPU cpu("intelcpu");Memory mem("sanxingmem");Harddisk disk("xindisk");GraphicsCard gc("huaweixianka");Computer comA(&cpu,&mem,&disk,&gc);comA.printfCom();//这里有个问题,如果我们创建的cpmputer比较多,又有不同的型号,那么很容易写错。//创建者模型就是解决这个问题
}

和继承的写法结合在一起

// 004构建者模式正确的写法.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <string>
using namespace std;//使用构造者模式创建
class CPUBase {
public:CPUBase(string cpuname) :_cpuname(cpuname) {cout << "CPUBase 构造方法被调用 _cpuname = " << _cpuname << endl;}public:string _cpuname;
};class HuaweiCpu : public CPUBase {
public:HuaweiCpu(string cpuname) :CPUBase(cpuname) {cout << "HuaweiCpu 构造方法被调用 _cpuname = " << _cpuname << endl;}
};
class xiaomiCpu : public CPUBase {
public:xiaomiCpu(string cpuname) :CPUBase(cpuname) {cout << "xiaomiCpu 构造方法被调用 _cpuname = " << _cpuname << endl;}
};
class lianxiangCpu : public CPUBase {
public:lianxiangCpu(string cpuname) :CPUBase(cpuname) {cout << "lianxiangCpu 构造方法被调用 _cpuname = " << _cpuname << endl;}
};class MemoryBase {
public:MemoryBase(string memname) :_memname(memname) {cout << "MemoryBase 构造方法被调用 _memname = " << memname << endl;}
public:string _memname;
};
class huaweiMemory :public MemoryBase {
public:huaweiMemory(string memname) :MemoryBase(memname) {cout << "huaweiMemory 构造方法被调用 _memname = " << memname << endl;}
};
class xiaomiMemory :public MemoryBase {
public:xiaomiMemory(string memname) :MemoryBase(memname) {cout << "xiaomiMemory 构造方法被调用 _memname = " << memname << endl;}
};
class lianxiangMemory :public MemoryBase {
public:lianxiangMemory(string memname) :MemoryBase(memname) {cout << "lianxiangMemory 构造方法被调用 _memname = " << memname << endl;}
};class HarddiskBase {
public:HarddiskBase(string diskname) :_diskname(diskname) {cout << "HarddiskBase 构造方法被调用 diskname = " << diskname << endl;}
public:string _diskname;
};
class huaweiHarddisk :public HarddiskBase {
public:huaweiHarddisk(string diskname) :HarddiskBase(diskname) {cout << "huaweiHarddisk 构造方法被调用 _diskname = " << diskname << endl;}
};
class xiaomiHarddisk :public HarddiskBase {
public:xiaomiHarddisk(string diskname) :HarddiskBase(diskname) {cout << "xiaomiHarddisk 构造方法被调用 diskname = " << diskname << endl;}
};
class lianxiangHarddisk :public HarddiskBase {
public:lianxiangHarddisk(string diskname) :HarddiskBase(diskname) {cout << "lianxiangHarddisk 构造方法被调用 diskname = " << diskname << endl;}
};class GraphicsCardBase {
public:GraphicsCardBase(string gcname) :_gcname(gcname) {cout << "GraphicsCardBase 构造方法被调用 _gcname = " << _gcname << endl;}
public:string _gcname;
};
class huaweiGraphicsCard :public GraphicsCardBase {
public:huaweiGraphicsCard(string gcname) :GraphicsCardBase(gcname) {cout << "huaweiHarddisk 构造方法被调用 gcname = " << gcname << endl;}
};
class xiaomiGraphicsCard :public GraphicsCardBase {
public:xiaomiGraphicsCard(string gcname) :GraphicsCardBase(gcname) {cout << "xiaomiGraphicsCard 构造方法被调用 gcname = " << gcname << endl;}
};
class lianxiangGraphicsCard :public GraphicsCardBase {
public:lianxiangGraphicsCard(string gcname) :GraphicsCardBase(gcname) {cout << "lianxiangGraphicsCard 构造方法被调用 gcname = " << gcname << endl;}
};class ComputerBase {public:virtual ~ComputerBase() {};void printComputerBase() {cout << "_cpuBase = " << this->_cpuBase<< "   _memBase = " << this->_memBase<< "   _hdiskBase = " << this->_hdiskBase<< "   _gcBase = " << this->_gcBase<< endl;}// 一般需要给私有的成员属性提供读写接口CPUBase* GetCPUBase() {return this->_cpuBase;}void SetCPUBase(CPUBase* cpubase) {this->_cpuBase = cpubase;}MemoryBase* GetMemoryBase() {return this->_memBase;}void SetMemoryBase(MemoryBase* membase) {this->_memBase = membase;}HarddiskBase* GetHarddiskBase() {return this->_hdiskBase;}void SetHarddiskBase(HarddiskBase* hdiskBase) {this->_hdiskBase = hdiskBase;}GraphicsCardBase* GetGraphicsCardBase() {return this->_gcBase;}void SetGraphicsCardBase(GraphicsCardBase* gcBase) {this->_gcBase = gcBase;}protected:CPUBase *_cpuBase;MemoryBase *_memBase;HarddiskBase *_hdiskBase;GraphicsCardBase *_gcBase;
};class huaweiComputer :public ComputerBase {};class lianxiangComputer :public ComputerBase {};class xiaomiComputer :public ComputerBase {};//构建器父类
class ComputerBaseBuilder {
public:virtual ~ComputerBaseBuilder() {};virtual void loadCPU(string cpubase) = 0;virtual void loadMem(string membase) = 0;virtual void loaddisk(string diskbase) = 0;virtual void loadGraphicsCard(string graphicsCardbase) = 0;ComputerBase * getCompBase() {return m_compBase;}
protected:ComputerBase *m_compBase;
};class huaweiComputerBuild :public ComputerBaseBuilder {
public:huaweiComputerBuild() {m_compBase = new huaweiComputer();}virtual void loadCPU(string strmodelno) {CPUBase *cpu = nullptr;if (strmodelno == "001") {cpu = new HuaweiCpu("huaweicpu");}if (strmodelno == "002") {cpu = new xiaomiCpu("xiaomicpu");}if (strmodelno == "003") {cpu = new lianxiangCpu("lianxiangcpu");}cout << "huaweiComputerBuild loadCPU" << endl;m_compBase->SetCPUBase(cpu);}virtual void loadMem(string strmodelno) {MemoryBase *memBase = nullptr;if (strmodelno == "001") {memBase = new huaweiMemory("huaweimemory");}if (strmodelno == "002") {memBase = new xiaomiMemory("xiaomimemory");}if (strmodelno == "003") {memBase = new lianxiangMemory("lianxiangmemory");}cout << "huaweiComputerBuild loadmemory" << endl;m_compBase->SetMemoryBase(memBase);}virtual void loaddisk(string strmodelno) {HarddiskBase *hdiskBase = nullptr;if (strmodelno == "001") {hdiskBase = new huaweiHarddisk("huaweidisk");}if (strmodelno == "002") {hdiskBase = new xiaomiHarddisk("xiaomidisk");}if (strmodelno == "003") {hdiskBase = new lianxiangHarddisk("lianxiangdisk");}cout << "huaweiComputerBuild loaddisk" << endl;m_compBase->SetHarddiskBase(hdiskBase);}virtual void loadGraphicsCard(string strmodelno) {GraphicsCardBase *_gcBase = nullptr;if (strmodelno == "001") {_gcBase = new huaweiGraphicsCard("huaweigc");}if (strmodelno == "002") {_gcBase = new xiaomiGraphicsCard("huaweigc");}if (strmodelno == "003") {_gcBase = new lianxiangGraphicsCard("huaweigc");}cout << "huaweiComputerBuild loadgcbase" << endl;m_compBase->SetGraphicsCardBase(_gcBase);}~huaweiComputerBuild() {}
};class lianxiangComputerBuild :public ComputerBaseBuilder {
public:lianxiangComputerBuild() {m_compBase = new lianxiangComputer();}virtual void loadCPU(string strmodelno) {CPUBase *cpu = nullptr;if (strmodelno == "001") {cpu = new HuaweiCpu("huaweicpu");}if (strmodelno == "002") {cpu = new xiaomiCpu("xiaomicpu");}if (strmodelno == "003") {cpu = new lianxiangCpu("lianxiangcpu");}cout << "huaweiComputerBuild loadCPU" << endl;m_compBase->SetCPUBase(cpu);}virtual void loadMem(string strmodelno) {MemoryBase *memBase = nullptr;if (strmodelno == "001") {memBase = new huaweiMemory("huaweimemory");}if (strmodelno == "002") {memBase = new xiaomiMemory("xiaomimemory");}if (strmodelno == "003") {memBase = new lianxiangMemory("lianxiangmemory");}cout << "huaweiComputerBuild loadmemory" << endl;m_compBase->SetMemoryBase(memBase);}virtual void loaddisk(string strmodelno) {HarddiskBase *hdiskBase = nullptr;if (strmodelno == "001") {hdiskBase = new huaweiHarddisk("huaweidisk");}if (strmodelno == "002") {hdiskBase = new xiaomiHarddisk("xiaomidisk");}if (strmodelno == "003") {hdiskBase = new lianxiangHarddisk("lianxiangdisk");}cout << "huaweiComputerBuild loaddisk" << endl;m_compBase->SetHarddiskBase(hdiskBase);}virtual void loadGraphicsCard(string strmodelno) {GraphicsCardBase *_gcBase = nullptr;if (strmodelno == "001") {_gcBase = new huaweiGraphicsCard("huaweigc");}if (strmodelno == "002") {_gcBase = new xiaomiGraphicsCard("huaweigc");}if (strmodelno == "003") {_gcBase = new lianxiangGraphicsCard("huaweigc");}cout << "huaweiComputerBuild loadgcbase" << endl;m_compBase->SetGraphicsCardBase(_gcBase);}~lianxiangComputerBuild() {}
};class xiaomiComputerBuild :public ComputerBaseBuilder {
public:xiaomiComputerBuild() {m_compBase = new xiaomiComputer();}virtual void loadCPU(string strmodelno) {CPUBase *cpu = nullptr;if (strmodelno == "001") {cpu = new HuaweiCpu("huaweicpu");}if (strmodelno == "002") {cpu = new xiaomiCpu("xiaomicpu");}if (strmodelno == "003") {cpu = new lianxiangCpu("lianxiangcpu");}cout << "huaweiComputerBuild loadCPU" << endl;m_compBase->SetCPUBase(cpu);}virtual void loadMem(string strmodelno) {MemoryBase *memBase = nullptr;if (strmodelno == "001") {memBase = new huaweiMemory("huaweimemory");}if (strmodelno == "002") {memBase = new xiaomiMemory("xiaomimemory");}if (strmodelno == "003") {memBase = new lianxiangMemory("lianxiangmemory");}cout << "huaweiComputerBuild loadmemory" << endl;m_compBase->SetMemoryBase(memBase);}virtual void loaddisk(string strmodelno) {HarddiskBase *hdiskBase = nullptr;if (strmodelno == "001") {hdiskBase = new huaweiHarddisk("huaweidisk");}if (strmodelno == "002") {hdiskBase = new xiaomiHarddisk("xiaomidisk");}if (strmodelno == "003") {hdiskBase = new lianxiangHarddisk("lianxiangdisk");}cout << "huaweiComputerBuild loaddisk" << endl;m_compBase->SetHarddiskBase(hdiskBase);}virtual void loadGraphicsCard(string strmodelno) {GraphicsCardBase *_gcBase = nullptr;if (strmodelno == "001") {_gcBase = new huaweiGraphicsCard("huaweigc");}if (strmodelno == "002") {_gcBase = new xiaomiGraphicsCard("xiaomigc");}if (strmodelno == "003") {_gcBase = new lianxiangGraphicsCard("lianxianggc");}cout << "huaweiComputerBuild loadgcbase" << endl;m_compBase->SetGraphicsCardBase(_gcBase);}~xiaomiComputerBuild() {}
};//指挥者类
class ComputerDirector {public:ComputerDirector(ComputerBaseBuilder *computerBaseBuilder) {this->m_pComputerBaseBuilder = computerBaseBuilder;}//指定新的构建器void SetBuilder(ComputerBaseBuilder* ptmpBuilder){m_pComputerBaseBuilder = ptmpBuilder;}//原MonsterBuilder类中的Assemble成员函数ComputerBase *Construct(string strmodelno) //参数:模型编号,形如:“002001003001”等。每些位的组合都有一些特别的含义,这里不需要探究。{m_pComputerBaseBuilder->loadCPU(strmodelno.substr(0, 3));m_pComputerBaseBuilder->loadMem(strmodelno.substr(3, 3));m_pComputerBaseBuilder->loaddisk(strmodelno.substr(6, 3));m_pComputerBaseBuilder->loadGraphicsCard(strmodelno.substr(9, 3));return m_pComputerBaseBuilder->getCompBase();}private:ComputerBaseBuilder* m_pComputerBaseBuilder; //指向所有构建器类的父类
};int main()
{_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);//程序退出时检测内存泄漏并显示到“输出”窗口xiaomiComputerBuild* xiaocomp = new xiaomiComputerBuild();ComputerDirector *cd = new ComputerDirector(xiaocomp);ComputerBase *combase = cd->Construct("001001001001");delete(combase->GetCPUBase());delete(combase->GetMemoryBase());delete(combase->GetHarddiskBase());delete(combase->GetGraphicsCardBase());combase->printComputerBase();delete combase;delete cd;delete xiaocomp;
}


http://www.mrgr.cn/p/30504855

相关文章

对2023年图灵奖揭晓看法

2023年图灵奖揭晓&#xff0c;你怎么看&#xff1f; 2023年图灵奖&#xff0c;最近刚刚颁给普林斯顿数学教授 Avi Wigderson&#xff01;作为理论计算机科学领域的领军人物&#xff0c;他对于理解计算中的随机性和伪随机性的作用&#xff0c;作出了开创性贡献。这些贡献不仅推…

Visual studio调试技巧

Visual studio调试技巧 bug是什么&#xff1f;Debug和ReleaseDebugRelease 如何调试VS调试快捷键调试过程中查看程序信息查看临时变量的值查看内存信息查看调用堆栈查看汇编信息查看寄存器信息 编译常见错误编译型错误链接型错误运行时错误 bug是什么&#xff1f; bug的英文释…

改进需求分析书

需求规格说明书 该项目是一个基于TCP用QT开发的五子棋对战系统。 项目仓库地址 1.1面向用户分析 五子棋是一种简单的休闲游戏,主要面向不需要高强度消耗、高耗时的网络用户。 对于本项目,目标人群精确至需要在游戏的同时进行社交的用户,他们需要在保证游戏正常运行的同时,直…

ThreeJS:坐标辅助器与轨道控制器

ThreeJS与右手坐标系 使用ThreeJS创建3D场景时&#xff0c;需要使用一个坐标系来定位和控制对象的位置和方向。 ThreeJS使用的坐标系是右手坐标系&#xff0c;即&#xff1a;X轴向右、Y轴向上、Z轴向前&#xff0c;如下图所示&#xff0c; ThreeJS-右手坐标系 Tips&#xff1a;…

code-server容器webpack的ws无法连接解决方法

TLDR 通过指定client的wsrul去连接ws devServer.client.webSocketURL ‘wss://<Forwarded uri>/ws’ 拓扑 1、code-server: 用于编写代码、启动webpack dev-server 服务&#xff1b;[https://<domain>:8001] 2、webpack: 用于浏览dev-server服务&#xff1b;[ht…

Picks Theorem 学习笔记

求顶点都是整点的简单多边形内部的整点个数。Picks Theorem 学习笔记 UVA10088 题目传送门 题意:顺时针或逆时针地给出一个 \(n\) 个顶点(顶点都是整点)的简单多边形,求这个多边形内部的整点数量(位于多边形形上的整点不算)。 Picks Theorem 对于一个顶点都是整点的简单多…

DC-9-sudo提权

标签:SQL注入、本地文件包含LFI、端口敲门、hydra爆破、linux提权 0x00 环境准备 下载地址:https://www.vulnhub.com/entry/dc-9,412/ 靶机描述: DC-9 is another purposely built vulnerable lab with the intent of gaining experience in the world of penetration testi…

DC-8-Drupal-exim4提权

Vulnhub简介 Vulnhub是一个提供了很多漏洞环境的靶场平台,其中的环境基本上都是做好的虚拟机镜像文件,需要使用VMware或者是VirtualBox运行。每个镜像会有破解的目标,大多是Boot2root,从启动虚拟机到获取操作系统的root权限和查看flag。 靶场部署 Vulnhub官网:https://www…

C#调用skiasharp实现绘制并拖拽图形

SkiaSharp是基于.net的跨平台二维图形库&#xff0c;封装的谷歌的Skia库&#xff0c;SkiaSharp支持在以下平台或运行时中使用&#xff0c;能够在图片中绘图&#xff0c;也提供控件在Winform、WPF等使用。本文学习skiasharp在Winform的基本用法&#xff0c;并参照参考文献5实现绘…

DC-5-screen提权

Vulnhub简介 Vulnhub是一个提供了很多漏洞环境的靶场平台,其中的环境基本上都是做好的虚拟机镜像文件,需要使用VMware或者是VirtualBox运行。每个镜像会有破解的目标,大多是Boot2root,从启动虚拟机到获取操作系统的root权限和查看flag。 靶场部署 vulnhub官网:https://www…

Kubernetes 弃用Docker后 Kubelet切换到Containerd

containerd 是一个高级容器运行时&#xff0c;又名 容器管理器。简单来说&#xff0c;它是一个守护进程&#xff0c;在单个主机上管理完整的容器生命周期&#xff1a;创建、启动、停止容器、拉取和存储镜像、配置挂载、网络等。 containerd 旨在轻松嵌入到更大的系统中。Docke…

【多模态大模型】AI对视频内容解析问答

文章目录 1. 项目背景2. 直接对视频进行解析进行AI问答&#xff1a;MiniGPT4-Video2.1 MiniGPT4-Video效果 3. 对视频抽帧为图片再进行AI问答3.1 视频抽帧3.2 图片AI问答3.2.1 阿里通义千问大模型 Qwen-vl-plus3.2.2 Moonshot 1. 项目背景 最近在做一个项目,需要使用AI技术对视…

洛谷P2375 [NOI2014] 动物园

动物园 题目描述输入格式输出格式输入输出样例 输入 3 aaaaa ab abcababc 输出 36 1 32 开始时都没看出来这是kmp板子题 先看看AC代码吧 #include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=1e6+10; const int mod=1e9+7; char a[maxn];…

List的remove()方法详解

https://blog.csdn.net/anxin_hw/article/details/128312846 一、错误使用场景 1、普通for循环遍历List删除指定元素,list.remove(index) 示例:将姓张的名字移除掉List<String> nameList = new ArrayList<>(Arrays.asList("张三", "李四", &…

本地大语言模型LLM的高效运行专家 | Ollama

Ollama简介 Ollama是一个开源的大型语言模型服务工具&#xff0c;它帮助用户快速在本地运行大模型。通过简单的安装指令&#xff0c;用户可以执行一条命令就在本地运行开源大型语言模型&#xff0c;如Llama 2。Ollama极大地简化了在Docker容器内部署和管理LLM的过程&#xff0…

软考备考1

【BV1Qc411G7fB】考试形式 考45分就行上午-计算机与软件工程知识-150分钟,笔试,选择题-75分还有5分时专业英语,,一篇文章挖5个空下午-软件设计-150分钟-笔试-简答题-75分三个复习阶段考点理论学习——建立理论框架 题型全覆盖——考试全部题型了然于胸 真题强化训练——适应…

AWVS

工具说明 Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的Web网络漏洞扫描工具,他通过网络爬虫测试你的网站安全,检测流行安全漏洞。 AWVS可以通过SQL注入攻击、XSS(跨站脚本攻击)、目录遍历、代码执行等漏洞来审核Web应用程序的安全性并输出扫描报告。相对于…

需求改进系统设计

这个作业属于哪个课程 <班级的链接>这个作业要求在哪里 <作业要求的链接>这个作业的目标 <体验项目的流程>1、需求改进 描述改进2、系统设计3、任务分配 由于团队只有一人,所以灵活分配时间 4、测试计划 测试是否可以正常登录 测试是否可以修改密码 测试图书…

低代码技术在构建质量管理系统中的应用与优势

引言 在当今快节奏的商业环境中&#xff0c;高效的质量管理系统对于组织的成功至关重要。质量管理系统帮助组织确保产品或服务符合客户的期望、符合法规标准&#xff0c;并持续改进以满足不断变化的需求。与此同时&#xff0c;随着技术的不断进步&#xff0c;低代码技术作为一…

免费开源语音克隆-GPT-SoVITS-WebUI只需 5 秒的声音样本

语音克隆-GPT-SoVITS-WebUI 强大的少样本语音转换与语音合成Web用户界面。 功能&#xff1a; 零样本文本到语音&#xff08;TTS&#xff09;&#xff1a; 输入 5 秒的声音样本&#xff0c;即刻体验文本到语音转换。 少样本 TTS&#xff1a; 仅需 1 分钟的训练数据即可微调模型…