//Chocolate.hpp
#ifndef _HFDP_CPP_SINGLETON_CHOCOLATE_HPP_
#define _HFDP_CPP_SINGLETON_CHOCOLATE_HPP_
#include "../../Standard.h"
#include "chocolateboiler.hpp"
#endif
//ChocolateBoiler.hpp
#ifndef _HFDP_CPP_SINGLETON_CHOCOLATE_BOILER_HPP_
#define _HFDP_CPP_SINGLETON_CHOCOLATE_BOILER_HPP_
#include "Chocolate.hpp"
namespace HeadFirstDesignPatterns {
namespace Factory {
namespace Singleton {
class ChocolateBoiler {
private: static ChocolateBoiler* _uniqueInstance;
private: bool _empty;
private: bool _boiled;
private: ChocolateBoiler( const ChocolateBoiler& ); // Disable copy constructor
private: void operator=( const ChocolateBoiler& ); // Disable assignment operator
private: ChocolateBoiler() {
_empty = true;
_boiled = false;
}
private: ~ChocolateBoiler() {
_uniqueInstance = 0;
}
public: static ChocolateBoiler* getInstance() {
if( _uniqueInstance == 0 ) {
std::cout << "Creating unique instance of Chocolate Boiler" << std::endl;
_uniqueInstance = new ChocolateBoiler();
}
std::cout << "Returning instance of Chocolate Boiler"<< std::endl;
return _uniqueInstance;
}
public: void fill() { // fill the boiler with a milk/chocolate mixture
if( isEmpty() ) {
_empty = false;
_boiled = false;
}
}
public: void drain() { // drain the boiled milk and chocolate
if( !isEmpty() && isBoiled() ) {
_empty = true;
}
}
public: void boil() { // bring the contents to a boil
if( !isEmpty() && !isBoiled() ) {
_boiled = true;
}
}
public: bool isEmpty() const {
return _empty;
}
public: bool isBoiled() const {
return _boiled;
}
};
} // namespace Singleton
} // namespace Factory
} // namespace HeadFirstDesignPatterns
#endif
//chocolate.cpp
#include "Chocolate.hpp"
using namespace HeadFirstDesignPatterns::Factory::Singleton;
ChocolateBoiler* ChocolateBoiler::_uniqueInstance = 0;
int main( int argc, char* argv[] ) {
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;
}
以上程序中只包含一个类,这就是孤独者模式的基本原理,输出如下: