design_pattern_singleton
来源:互联网 发布:熟shou 知乎 编辑:程序博客网 时间:2024/05/16 07:02
The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.
What is our motivation to use it? In nearly every application, there is a need to have an area from which to globally access and maintain some type of data. There are also cases in object-oriented (OO) systems where there should be only one class, or a predefined number of instances of a class, running at any given time.
For example, when a class is being used to maintain an incremental counter, the simple counter class needs to keep track of an integer value that is being used in multiple areas of an application. The class needs to be able to increment this counter as well as return the current value. For this situation, the desired class behavior would be to have exactly one instance of a class that maintains the integer and nothing more.
The basic structure is as follows, there is a good discussion here
// a lot of methods are omitted hereclass Singleton{ public: static Singleton* getInstance( ); ~Singleton( ); private: Singleton( ); static Singleton* instance;};
class ChocolateBoiler {private: static ChocolateBoiler* uniqueInstance;bool empty;bool boiled;ChocolateBoiler(const ChocolateBoiler& ); // Disable copy constructorvoid operator=(const ChocolateBoiler& ); // Disable assignment operatorChocolateBoiler(){empty = true;boiled = false;}~ChocolateBoiler() {uniqueInstance = NULL;}public: static ChocolateBoiler* getInstance() {if(uniqueInstance == NULL ) {std::cout << "Creating unique instance of Chocolate Boiler" << std::endl;uniqueInstance = new ChocolateBoiler();}std::cout << "Returning instance of Chocolate Boiler"<< std::endl;return uniqueInstance;}void fill() {// fill the boiler with a milk/chocolate mixtureif(isEmpty()){empty = false;boiled = false;}}void drain() {// drain the boiled milk and chocolateif(!isEmpty() && isBoiled()){empty = true;}}void boil() {// bring the contents to a boilif(!isEmpty() && !isBoiled()) {boiled = true;}}bool isEmpty() const { return empty; }bool isBoiled() const { return boiled; }};The main function is
#include <iostream>#include <./chocolatefactory.hpp>ChocolateBoiler* ChocolateBoiler::uniqueInstance = NULL;int main(){ ChocolateBoiler* boiler = ChocolateBoiler::getInstance();boiler->fill();boiler->boil();boiler->drain();// will return the existing instanceChocolateBoiler* boiler2 = ChocolateBoiler::getInstance();if( boiler == boiler2 )std::cout << "Got same boiler" << std::endl;elsestd::cout << "Oh oh! got a different boiler" << std::endl; return 0;}The result is
- design_pattern_singleton
- Animation
- Win8.1无法安装msi软件提示2502、2503错误怎么解决?
- 多态总结
- KMP算法/字符串匹配算法
- JAVA单例模式编程详解
- design_pattern_singleton
- 【C++ Primer】【学习笔记】【第九章】顺序容器之:顺序容器的操作
- Linux下面如何安装Django
- Machine Learning PCA
- [Hb-XVII] 计算机的抽象层次-简 使用寄存器 使用内存空间 程序执行过程 使用main函数规定 不定参数函数机制 C
- URAL 1885. Passenger Comfort(数学啊 )
- 会议印象
- pat 1032. Sharing (25)
- TCP Socket 简单编程 in java