c++两种设计模式 单例和工厂模式
一.单例
1.单例的概念
1.当前的类最多只能创建一个实例
2.当前这个唯一的实例,必须由当前类创建(自主创建),而不是调用者创建
3.必须向整个系统提供全局的访问点,来获取唯一的实例
2.单例的代码
c++
#include <iostream>
using namespace std;
class CSingleton {
CSingleton(){}
CSingleton(const CSingleton&) = delete;//弃用拷贝构造函数
static CSingleton* m_spring;
~CSingleton() {}
public:
static struct DeleteSingleton {//保证申请的堆空间一定被回收
~DeleteSingleton() {
if(m_spring)
delete m_spring;
m_spring = nullptr;
}
} del;//静态对象,在程序结束时静态对象会自动被回收,然后就会调用析构函数,就一定能保证申请的堆空间被回收
//有问题的代码,在多线程下,可能会创建多个对象
static CSingleton* CreatCSingleton() {
//加锁
if (!m_spring) {//如果指针为空,则创建对象
m_spring = new CSingleton;
}
//解锁
return m_spring;
}
static void DestoryCSingleton(CSingleton*& psin) {
if (m_spring)
delete m_spring;
m_spring = nullptr;
psin = nullptr;
}
/*~CSingleton() {//这里不能使用析构进行回收,这里回收的是对象,这样的话就会不断的调用析构
if (m_spring) {
delete m_spring;
m_spring = nullptr;
}
}*/
};
CSingleton::DeleteSingleton CSingleton::del;//类外定义初始化, 格式: 类型 类名::静态变量名
CSingleton* CSingleton:: m_spring(nullptr);
int main() {//主函数进行测试
CSingleton* p1 = CSingleton::CreatCSingleton();
CSingleton* p2 = CSingleton::CreatCSingleton();
cout << p1 << " " << p2 << endl;
CSingleton::DestoryCSingleton(p1);
return 0;
}
3.懒汉式和饿汉式
懒汉式:当第一次调用这个接口函数时,先创建单例 时间换空间的做法
饿汉式:无论是否调用获取单例接口函数,都会提前创建单例 空间换时间的做法
懒汉式代码就是上面的代码
饿汉式代码
c++
//饿汉式
class CSingleton {
CSingleton() {}
CSingleton(const CSingleton&) = delete;
static CSingleton sing;
~CSingleton() {}
public:
static CSingleton* CreatCSingleton() {
return &sing;
}
};
CSingleton CSingleton::sing;
4.单例模式的优点
1.单例模式提供了严格的对唯一实例的创建,访问和销毁,安全性高
2.单例模式的实现可以节省系统资源
二.工厂模式
1.概念
工厂模式主要用来集中创建对象的,如果在任何使用的地方创建对象那就造成了类或方法之间的耦合,如果要更换对象那么在所有使用到的地方都要修改一遍,不利于后期的维护,也违背了开闭设计原则,如果使用工厂来创建对象,那么就彻底解耦合了,如果要修改著需要修改工厂类即可,工厂模式最大的优势:解耦
2.简单工厂
c++
class CEngine {
public:
virtual void woring() = 0;
};
class CEngine2L: public CEngine {
void woring() {
cout<<"2.0自然吸气发动机正在工作" <<endl;
}
};
class CEngine2T : public CEngine {
void woring() {
cout<<"2.0涡轮增压发动机正在工作" << endl;
}
class CCar {
public:
CEngine* m_engine;
CCar():m_engine(new CEngine2L){}
CCar(const string&type) :m_engine(type=="2.0L"?(CEngine*)new CEngine2L: (CEngine*)new CEngine2T) {}//这里发生了耦合
void dirve() {
if (m_engine) {
m_engine->woring();
cout << "汽车正在行驶" << endl;
}
}
~CCar() {
if (m_engine) {
delete m_engine;
m_engine = nullptr;
}
}
};
int main() {
CCar tst1;
CCar tst2("2.0L");
tst1.dirve();
tst2.dirve();
}
简单工厂就是在上面代码的基础上进行解耦
耦合这里来看就是:如果发动机函数增加参数的话,那么汽车类中与发动机函数有交互的函数的内容也需要改,就是牵一发而动全身
进行解耦代码为
c++
class CEngine {
public:
virtual void woring() = 0;
};
class CEngine2L : public CEngine {
void woring() {
cout << "2.0自然吸气发动机正在工作" << endl;
}
};
class CEngine2T : public CEngine {
void woring() {
cout << "2.0涡轮增压发动机正在工作" << endl;
}
};
//创建发动机的具体代码写在这里
class CFactoryEngine {
public:
CEngine* CreatEngine(const string& type) {
if (type == "2.0L") {
return new CEngine2L;
}
else if (type == "2.0T") {
return new CEngine2T;
}
else {
return nullptr;
}
}
};
class CCar {
public:
CEngine* m_engine;
CCar() :m_engine(new CEngine2L) {}
//引入简单工厂后的写法
CCar(CFactoryEngine* pFac,const string& type):m_engine(pFac? pFac->CreatEngine(type):nullptr){}
void dirve() {
if (m_engine) {
m_engine->woring();
cout << "汽车正在行驶" << endl;
}
}
~CCar() {
if (m_engine) {
delete m_engine;
m_engine = nullptr;
}
}
};
int main(){
CFactoryEngine Fac;
CCar audi(&Fac, "2.0L");
audi.dirve();
CCar benzi(&Fac, "2.0L");
benzi.dirve();
}
3.工厂方法
工厂方法就是在简单工厂的基础上 把工厂进行细化 每个产品对应一个工厂(之前的简单工厂每有一个新产品就要将工厂的功能进行修改很麻烦,工厂进行细化之后,如果有新的产品我们只需要增加工厂的功能就行了)
代码如下
c++
#include <iostream>
using namespace std;
class CEngine {
public:
virtual void woring() = 0;
};
class CEngine2L : public CEngine {
void woring() {
cout << "2.0自然吸气发动机正在工作" << endl;
}
};
class CEngine2T : public CEngine {
void woring() {
cout << "2.0涡轮增压发动机正在工作" << endl;
}
};
class CFactoryEngine {
public:
virtual CEngine* CreatEngine() = 0;
};
class CFactoryEngine2L :public CFactoryEngine {
public:
virtual CEngine* CreatEngine() {
return new CEngine2L;
}
};
class CFactoryEngine2T :public CFactoryEngine {
public:
virtual CEngine* CreatEngine() {
return new CEngine2T;
}
};
class CCar {
public:
CEngine* m_engine;
CCar() :m_engine(new CEngine2L) {}
CCar(CFactoryEngine* pFac) :m_engine(pFac ? pFac->CreatEngine() : nullptr) {}
void dirve() {
if (m_engine) {
m_engine->woring();
cout << "汽车正在行驶" << endl;
}
}
~CCar() {
if (m_engine) {
delete m_engine;
m_engine = nullptr;
}
}
};
int main() {
CFactoryEngine* Fac2L = new CFactoryEngine2L;
CFactoryEngine* Fac2T = new CFactoryEngine2T;
CCar audi(Fac2L);
audi.dirve();
CCar benzi(Fac2T);
benzi.dirve();
return 0;
}
4.抽象工厂
抽象工厂就是在工厂方法的基础上将所有工厂统一管理
代码如下
c++
#include <iostream>
using namespace std;
class CEngine {
public:
virtual void woring() = 0;
};
class CEngine2L : public CEngine {
void woring() {
cout << "2.0自然吸气发动机正在工作" << endl;
}
};
class CEngine2T : public CEngine {
void woring() {
cout << "2.0涡轮增压发动机正在工作" << endl;
}
};
class CGearBox {
public:
virtual void woring() = 0;
};
class CGearBoxAuto : public CGearBox {
void woring() {
cout << "自动变速器正在工作" << endl;
}
};
class CGearBoxMaual : public CGearBox {
void woring() {
cout << "手动变速器正在工作" << endl;
}
};
//加速器
class CFactoryEngine {
public:
virtual CEngine* CreatEngine() = 0;
};
class CFactoryEngine2L :public CFactoryEngine {
public:
virtual CEngine* CreatEngine() {
return new CEngine2L;
}
};
class CFactoryEngine2T :public CFactoryEngine {
public:
virtual CEngine* CreatEngine() {
return new CEngine2T;
}
};
//变速器
class CFactoryEngineGearBox {
public:
virtual CGearBox* CreatBox() = 0;
};
class CFactoryEngineGearBoxAuto :public CFactoryEngineGearBox {
public:
virtual CGearBox* CreatBox() {
return new CGearBoxAuto;
}
};
class CFactoryEngineGearBoxMaual :public CFactoryEngineGearBox {
public:
virtual CGearBox* CreatBox() {
return new CGearBoxMaual;
}
};
//统一工厂
class CFactory {
public:
virtual CGearBox* CreatBox() = 0;
virtual void woring() = 0;
};
class CFactory2TAuto :public CFactory {
public:
virtual CGearBox* CreatBox() {
return new CGearBoxAuto;
}
virtual CEngine* CreatEngine() {
return new CEngine2T;
}
};
class CFactory2TMaual :public CFactory {
public:
virtual CGearBox* CreatBox() {
return new CGearBoxMaual;
}
virtual CEngine* CreatEngine() {
return new CEngine2T;
}
};
class CCar {
public:
CEngine* m_engine;
CGearBox* m_gearbox;
CCar() :m_engine(new CEngine2L) {}
CCar(CFactoryEngine* pFacEngine, CFactoryEngineGearBox* pFacGearBox) :
m_engine(pFacEngine ? pFacEngine->CreatEngine() : nullptr)
, m_gearbox(pFacGearBox ? pFacGearBox->CreatBox() : nullptr)
{}
void dirve() {
if (m_engine) {
m_engine->woring();
cout << "汽车正在行驶" << endl;
}
if (m_gearbox) {
m_gearbox->woring();
cout << "变速器正在工作" << endl;
}
}
~CCar() {
if (m_engine) {
delete m_engine;
m_engine = nullptr;
}
if (m_gearbox) {
delete m_gearbox;
m_gearbox = nullptr;
}
}
};
int main() {
CFactoryEngine* PFa1 = new CFactoryEngine2L;
CFactoryEngineGearBox* PFa2 = new CFactoryEngineGearBoxAuto;
CCar audi(PFa1,PFa2);
audi.dirve();
return 0;
}