1.单例模式
概念
Singleton 模式是设计模式中最为简单、最为常见、最容易实现,也是最应该熟悉和掌握的模式。Singleton 模式就是一个类只创建一个唯一的对象,即一次创建多次使用。
实现步骤
- 构造函数私有
- 一个静态私有的当前类的指针
- 一个静态公有的获取单例的方法
- 分为饿汉式和懒汉式,前者是线程安全的,后者线程不安全,饿汉式在还没使用时就加载该单例对象,懒汉式等到首次使用时才加载该单例对象。
代码实现
#include<iostream>
using namespace std;
class SingletonLazy{
public:
static SingletonLazy* getInstance(){
if(pSingleton==NULL){
pSingleton=new SingletonLazy();
}
return pSingleton;
}
private:
SingletonLazy(){
cout<<"SingletonLazy"<<endl;
}
static SingletonLazy*pSingleton;
};
SingletonLazy* SingletonLazy::pSingleton=NULL;
class SingletonHungry{
public:
static SingletonHungry* getInstance(){
return pSingleton;
}
private:
SingletonHungry(){
cout<<"SingletonHungry"<<endl;
}
static SingletonHungry* pSingleton;
};
SingletonHungry* SingletonHungry::pSingleton=new SingletonHungry();
int main(){
SingletonLazy* sl=SingletonLazy::getInstance();
SingletonHungry* sh=SingletonHungry::getInstance();
return 0;
}
2.建造者模式
概念
当我们要创建的对象很复杂的时候(通常是由很多其他的对象组合而成),我们要要复杂对象的创建过程和这个对象的表示(展示)分离开来,这样做的好处就是通过一步步的进行复杂对象的构建,由于在每一步的构造过程中可以引入参数,使得经过相同的步骤创建最后得到的对象的展示不一样。
角色
- 一个抽象的建造者,是具体建造者的父类,提供产品各个构建步骤的实现方法。
- 若干具体的建造者,在构造函数中申请对应的产品,提供对应产品的具体各个构建步骤的是方法。
- 指导者,提供一个方法,在这个方法中以一个建造者作为参数,依次调用此建造者的各个建造步骤。关键在于它不直接返回对象而是一步步完成建造步骤。
代码实现
#include<iostream>
using namespace std;
class Car{
public:
string GetEngine(){return m_engine;}
string GetGearBox(){return m_gearbox;}
string GetChassis(){return m_chassis;}
void SetEnging(string engine){
m_engine = engine;
cout<<"created engine "<<m_engine<<endl;
}
void SetGearBox(string gearbox){
m_gearbox = gearbox;
cout<<"created gearbox "<<m_gearbox<<endl;
}
void SetChassis(string chassis){
m_chassis = chassis;
cout<<"created chassis "<<m_chassis<<endl;
}
private:
string m_engine;
string m_gearbox;
string m_chassis;
};
class BMWCar:public Car{
public:
BMWCar(){
cout<<"start building"<<endl;
}
};
class BenzCar:public Car{
public:
BenzCar(){
cout<<"start building"<<endl;
}
};
class CarBuilder{
public:
CarBuilder(){
cout<<"original carBuilder"<<endl;
}
virtual Car* BuildCar()=0;
virtual void BuildEngine(){
cout<<"orignal engine"<<endl;
}
virtual void BuildGearBox(){
cout<<"orignal gearbox"<<endl;
}
virtual void BuildChassis(){
cout<<"orignal chassis"<<endl;
}
};
class BenzCarBuilder:public CarBuilder{
public:
BenzCarBuilder(){
cout<<"BenzCarBuilder"<<endl;
m_car=new BenzCar();
}
void BuildEngine(){
m_car->SetEnging("Benz Engine");
}
void BuildGearBox(){
m_car->SetGearBox("Benz GearBox");
}
void BuildChassis(){
m_car->SetChassis("Benz Chassis");
}
Car* BuildCar(){
return m_car;
}
private:
Car* m_car;
};
class BMWCarBuilder:public CarBuilder{
public:
BMWCarBuilder(){
cout<<"BMWCarBuilder"<<endl;
m_car=new BMWCar();
}
void BuildEngine(){
m_car->SetEnging("BMW Engine");
}
void BuildGearBox(){
m_car->SetGearBox("BMW GearBox");
}
void BuildChassis(){
m_car->SetChassis("BMW Chassis");
}
Car* BuildCar(){
return m_car;
}
private:
Car* m_car;
};
class CarDirector{
public:
Car* ConstructCar(CarBuilder* carBuilder){
carBuilder->BuildEngine();
carBuilder->BuildGearBox();
carBuilder->BuildChassis();
cout<<"car build finished!"<<endl;
return carBuilder->BuildCar();
}
};
int main(){
CarDirector* director=new CarDirector();
Car* bmwCar=director->ConstructCar(new BMWCarBuilder());
Car* benzCar=director->ConstructCar(new BenzCarBuilder());
delete bmwCar;
delete benzCar;
delete director;
return 0;
}
3.原型模式
概念
Prototype 模式提供了自我复制的功能,就是说新对象的创建可以通过已有对象进行创建。
这里注意浅拷贝和深拷贝的区别,原型模式提供的clone都是深拷贝。
原型模式主要就是clone,解决了构建复杂对象时资源消耗的问题,提升某些场景中创建对象的效率,并且还提供一种只读的限制方法,即给出对象的拷贝版本而非本身。
角色
一个抽象的原型类,其中提供一个纯虚的clone方法,继承原型类的类都必须实现clone方法用来返回自己的拷贝版本。
代码实现
这里我使用一个静态类成员变量以区分不同的对象。
#include<iostream>
using namespace std;
class Prototype{
public:
virtual ~Prototype(){}
virtual Prototype* clone() const =0;
protected:
Prototype(){};
};
class ConcretePrototype:public Prototype{
public:
ConcretePrototype(){
cout<<"original machine "<<num<<" operation"<<endl;
num++;
};
ConcretePrototype(const ConcretePrototype& cp){
cout<<"cloned machine "<<num<<" operation"<<endl;
num++;
}
~ConcretePrototype(){}
Prototype* clone()const{
cout<<num<<" machine cloning..."<<endl;
return new ConcretePrototype(*this);
}
static int num;
};
int ConcretePrototype::num=1;
int main(){
Prototype *p=new ConcretePrototype();
Prototype *p1=p->clone();
Prototype *p2=p1->clone();
Prototype *p3=p2->clone();
return 0;
}
总结
抽象工厂模式、建造者模式、原型模式实际上都是通过一个类或实例来创建新实例的模式。他们的区别是,抽象工厂模式更注重产品类之间的联系,建造者更注重创建产品的每个步骤,原型模式更注重创建自身的拷贝。