设计模式(C++实现):simple factory pattern + singleton pattern
2013-09-17 15:30
393 查看
1简单工厂模式
简单工厂模式按照设计模式类型分的话是属于创建型模式,他是工厂模式中最简单的一种,他不属于GoF 23中设计模式之一,但是是简单实用的。他是有一个工厂对象来决定创建哪一种对象的实例。
实现比较简单,这里我只贴出类图和调用方法:
[cpp] view
plaincopy
IProduct * simpleFactory::Create(std::string className)
{
if( className == "Ipad")
return new Ipad();
if(className == "IPhone")
return new IPhone();
}
优点:外界只要传入类的名字或者识别符即可,不需要关心具体创建过程,只顾“消费”就可以了。各有各的指责和权力,分工明确,利于整个软件架构优化。
缺点:违反里高内聚责任分配原则,将全部逻辑放到一个工厂类中,如果需要支持新的类,那就必须改变工厂类了。
这里只有一种工厂,一个系列的产品,那如果有多个系列的产品呢?问题如何解决?
应用场景:
1. 我的asp.net程序需要根据客户实际所用数据库来配置,那么我的程序支持所有的数据库,具体用到哪个,配置文件配置一下即可。这个C#/Java里的反射就可以实现。
2. MFC里的Seralize功能。
2.单例模式,顾名思义,就是只能由一个实例,那么我们就必须保证
该类不能被复制。
该类不能被公开的创造。
那么对于C++来说,他的构造函数,拷贝构造函数和他的赋值函数都不能被公开调用。
但对于该私有的构造函数的构造时机上来说也可以分两种情况来构造:
只有当需要该类的时候去构造(即为懒汉模式)
在程序开始之前我就先构造好,你到时候直接用就可(即为饿汉模式)
那么我分别来说说这两种模式:
懒汉模式,静态局部变量只会被初始化一次即第一次执行的时候,其生命周期与程序的生命周期是相同的。这个同样适用于多线程的情况。
[cpp] view
plaincopy
class single1
{
public:
static single1 &getInstance()
{
static single1 instance;
return instance;
}
void helloSingle() {cout<<"Hello Single1!"<<endl;}
private:
single1(){};
single1(const single1&);
single1& operator=(const single1&);
};
饿汉模式,但是在main函数外面必须先调用其实例的构造,这个是静态实例的初始化,C/C++会保证其在进入main函数之前进行。
[cpp] view
plaincopy
class single2
{
public:
static single2* getInstance()
{
return mInstance;
}
void helloSingle() {cout<<"Hello Single2!"<<endl;}
private:
static single2* mInstance;
private:
single2(){};
single2(const single2&);
single2& operator=(const single2&);
};
single2* single2::mInstance = new single2;
使用:
[cpp] view
plaincopy
#include<iostream>
using namespace std;
class single1
{
public:
static single1 &getInstance()
{
static single1 instance;
return instance;
}
void helloSingle() {cout<<"Hello Single1!"<<endl;}
private:
single1(){};
single1(const single1&);
single1& operator=(const single1&);
};
class single2
{
public:
static single2* getInstance()
{
return mInstance;
}
void helloSingle() {cout<<"Hello Single2!"<<endl;}
private:
static single2* mInstance;
private:
single2(){};
single2(const single2&);
single2& operator=(const single2&);
};
single2* single2::mInstance = new single2;
int main()
{
single1 *a=&single1::getInstance();
single2 *b=single2::getInstance();
a->helloSingle();
b->helloSingle();
return 0;
}
我们运用静态局部变量和静态实例的特性,使得我们的这两种模式都适用于多线程。
简单工厂模式按照设计模式类型分的话是属于创建型模式,他是工厂模式中最简单的一种,他不属于GoF 23中设计模式之一,但是是简单实用的。他是有一个工厂对象来决定创建哪一种对象的实例。
实现比较简单,这里我只贴出类图和调用方法:
[cpp] view
plaincopy
IProduct * simpleFactory::Create(std::string className)
{
if( className == "Ipad")
return new Ipad();
if(className == "IPhone")
return new IPhone();
}
优点:外界只要传入类的名字或者识别符即可,不需要关心具体创建过程,只顾“消费”就可以了。各有各的指责和权力,分工明确,利于整个软件架构优化。
缺点:违反里高内聚责任分配原则,将全部逻辑放到一个工厂类中,如果需要支持新的类,那就必须改变工厂类了。
这里只有一种工厂,一个系列的产品,那如果有多个系列的产品呢?问题如何解决?
应用场景:
1. 我的asp.net程序需要根据客户实际所用数据库来配置,那么我的程序支持所有的数据库,具体用到哪个,配置文件配置一下即可。这个C#/Java里的反射就可以实现。
2. MFC里的Seralize功能。
2.单例模式,顾名思义,就是只能由一个实例,那么我们就必须保证
该类不能被复制。
该类不能被公开的创造。
那么对于C++来说,他的构造函数,拷贝构造函数和他的赋值函数都不能被公开调用。
但对于该私有的构造函数的构造时机上来说也可以分两种情况来构造:
只有当需要该类的时候去构造(即为懒汉模式)
在程序开始之前我就先构造好,你到时候直接用就可(即为饿汉模式)
那么我分别来说说这两种模式:
懒汉模式,静态局部变量只会被初始化一次即第一次执行的时候,其生命周期与程序的生命周期是相同的。这个同样适用于多线程的情况。
[cpp] view
plaincopy
class single1
{
public:
static single1 &getInstance()
{
static single1 instance;
return instance;
}
void helloSingle() {cout<<"Hello Single1!"<<endl;}
private:
single1(){};
single1(const single1&);
single1& operator=(const single1&);
};
饿汉模式,但是在main函数外面必须先调用其实例的构造,这个是静态实例的初始化,C/C++会保证其在进入main函数之前进行。
[cpp] view
plaincopy
class single2
{
public:
static single2* getInstance()
{
return mInstance;
}
void helloSingle() {cout<<"Hello Single2!"<<endl;}
private:
static single2* mInstance;
private:
single2(){};
single2(const single2&);
single2& operator=(const single2&);
};
single2* single2::mInstance = new single2;
使用:
[cpp] view
plaincopy
#include<iostream>
using namespace std;
class single1
{
public:
static single1 &getInstance()
{
static single1 instance;
return instance;
}
void helloSingle() {cout<<"Hello Single1!"<<endl;}
private:
single1(){};
single1(const single1&);
single1& operator=(const single1&);
};
class single2
{
public:
static single2* getInstance()
{
return mInstance;
}
void helloSingle() {cout<<"Hello Single2!"<<endl;}
private:
static single2* mInstance;
private:
single2(){};
single2(const single2&);
single2& operator=(const single2&);
};
single2* single2::mInstance = new single2;
int main()
{
single1 *a=&single1::getInstance();
single2 *b=single2::getInstance();
a->helloSingle();
b->helloSingle();
return 0;
}
我们运用静态局部变量和静态实例的特性,使得我们的这两种模式都适用于多线程。
相关文章推荐
- (C++实现)——简单工厂模式(Simple Factory Pattern)
- 大话设计模式c++实现1——简单工厂模式 Simple Factory Pattern
- 我所理解的设计模式(C++实现)——简单工厂模式(Simple Factory Pattern)
- 创建型模式之工厂模式(Factory Pattern)C++实现******
- c++设计模式:简单工厂模式(Simple Factory Pattern)
- C++ 设计模式之——简单工厂模式(SimpleFactoryPattern)
- 设计模式C++ 笔记一 创建型模式(Factory、abstract factory、singleton)
- 创建型模式之抽象工厂模式(AbstractFactory Pattern)C++实现******
- c++设计模式:简单工厂模式(Simple Factory Pattern)
- (C++实现)——抽象工厂模式(Abstract Factory Pattern)
- 设计模式之工厂模式(Factory Pattern)用C++实现
- 设计模式factory的C++实现源码
- 设计模式(一)之工厂模式(Factory) C++实现
- 设计模式之抽象工程模式Abstract Factory Pattern()C++实现
- Head First 设计模式 (四) 工厂模式(factory pattern) C++实现
- 设计模式C++实现(1)——工厂模式(Factory)
- 设计模式(Design Pattern) - 创建型模式(Creational Pattern) - 简单工厂模式(Simple Factory Pattern) - Java实现
- 我所理解的设计模式(C++实现)——抽象工厂模式(Abstract Factory Pattern)
- 设计模式(4)-Simple Factory Pattern
- 我所理解的设计模式(C++实现)——抽象工厂模式(Abstract Factory Pattern)