单例模式的意图是保证一个类只有一个实例,并提供一个访问它的全局访问点.
适用场景:
1.当类只有一个实例而且客户可以从一个众所周知的访问点访问它时;
2.当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。
设计步骤:
1. 在“CSingleton”类中定义一个static的私有属性。
2. 在类中定义static公共访问器函数。
3. 在访问器功能中执行“延迟初始化”(首次创建时使用)。
4. 将类的构造函数定义为protected或private。
5. 使用时只能通过访问器函数来操作CSingleton。
示例:Singleton (VS2010,win32模式)
适用场景:
1.当类只有一个实例而且客户可以从一个众所周知的访问点访问它时;
2.当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。
设计步骤:
1. 在“CSingleton”类中定义一个static的私有属性。
2. 在类中定义static公共访问器函数。
3. 在访问器功能中执行“延迟初始化”(首次创建时使用)。
4. 将类的构造函数定义为protected或private。
5. 使用时只能通过访问器函数来操作CSingleton。
示例:Singleton (VS2010,win32模式)
Singleton.h文件
#pragma once
class CSingleton
{
public:
~CSingleton(void);
// 2. 在类中定义static公共访问器函数。
static CSingleton* _initInstance(int _val = 0);
void inValue(int _val);
void outValue();
protected:
// 3. 在访问器功能中执行“延迟初始化”(首次创建时使用)。
CSingleton(int _val = 0);
private:
// 1. 在“CSingleton”类中定义一个static的私有属性。
static CSingleton* m_instance;
int m_value;
};
Singleton.cpp文件
#include <iostream>
#include "Singleton.h"
CSingleton* CSingleton::m_instance = 0;
CSingleton::CSingleton(int _val)
{
m_value = _val;
}
CSingleton::~CSingleton(void)
{
}
CSingleton* CSingleton::_initInstance(int _val)
{
if (m_instance == 0)
m_instance = new CSingleton(_val);
return m_instance;
}
void CSingleton::inValue(int _val)
{
m_value = _val;
}
void CSingleton::outValue()
{
std::cout << m_value << std::endl;
}
#include <iostream>
using namespace std;
/*
单例模式的意图是保证一个类只有一个实例,并提供一个访问它的全局访问点.
适用场景:
1.当类只有一个实例而且客户可以从一个众所周知的访问点访问它时;
2.当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。
设计步骤:
1. 在“CSingleton”类中定义一个static的私有属性。
2. 在类中定义static公共访问器函数。
3. 在访问器功能中执行“延迟初始化”(首次创建时使用)。
4. 将类的构造函数定义为protected或private。
5. 使用时只能通过访问器函数来操作CSingleton。
*/
#include "Singleton.h"
CSingleton* singleton;
int main()
{
// 5. 使用时只能通过访问器函数来操作CSingleton。
CSingleton* s1 = singleton->_initInstance(100);
s1->outValue();
CSingleton* s2 = singleton->_initInstance(222);
s2->outValue();
system("pause");
return 0;
}包含一个子类的情况示例:WithSonClass(未细分,直接上main.cpp)
main.cpp文件
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
// 基类
class Number
{
public:
// 2. Define a public static accessor func
// 定义公共静态访问器方法
static Number *instance();
static void setType(string t)
{
type = t;
delete inst;
inst = 0;
}
virtual void setValue(int in)
{
value = in;
}
virtual int getValue()
{
return value;
}
protected:
int value;
// 4. Define all ctors to be protected
// 定义所有要保护的构造函数
Number()
{
cout << "我是基类: ";
}
// 1. Define a private static attribute
// 定义一个私有静态属性
private:
static string type;
static Number *inst;
};
// 全局实例
string Number::type = "decimal";
Number *Number::inst = 0;
// 子类(个人理解:继承单例的子类是对单例基类的一种功能扩充)
class Octal: public Number
{
// 6. Inheritance can be supported
// 继承可以支持
public:
friend class Number;
void setValue(int in)
{
char buf[10];
sprintf(buf, "%o", in);// 以8进制写入buf
sscanf(buf, "%d", &value);
}
protected:
// 4
Octal(){
cout << "我是子类:";
}
};
Number *Number::instance()
{
if (!inst)
// 3. Do "lazy initialization" in the accessor function
// 在访问器函数中执行“延迟初始化”
if (type == "octal")
inst = new Octal();
else
inst = new Number();
return inst;
}
int main()
{
// Number myInstance; - error: cannot access protected constructor
// 5. Clients may only use the accessor function to manipulate the Singleton
// 客户端可能只使用访问器函数来操纵Singleton
Number::instance()->setValue(42);
cout << "value is " << Number::instance()->getValue() << endl;
Number::setType("octal");
Number::instance()->setValue(64);
cout << "value is " << Number::instance()->getValue() << endl;
system("pause");
return 0;
}
*个人理解:继承单例的子类是对单例基类的一种功能扩充。
示例代码参考自:https://sourcemaking.com/design_patterns/object_pool
1424

被折叠的 条评论
为什么被折叠?



