design_pattern_singleton

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 here
class 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 constructor
	void operator=(const ChocolateBoiler& ); // Disable assignment operator

	ChocolateBoiler(){
		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 mixture
		if(isEmpty()){
			empty = false;
			boiled = false;
		}
	}

	void drain() {	// drain the boiled milk and chocolate
		if(!isEmpty() && isBoiled()){
			empty = true;
		}
	}

	void boil() {	// bring the contents to a boil
		if(!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 instance
	ChocolateBoiler* boiler2 = ChocolateBoiler::getInstance();

	if( boiler == boiler2 )
		std::cout << "Got same boiler" << std::endl;
	else
		std::cout << "Oh oh! got a different boiler" << std::endl;

    return 0;
}
The result is






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值