1 / 6

Creational Pattern: Singleton

Creational Pattern: Singleton. Chapter 3 – Page 45. When an application needs one, and only one, instance of a particular object, proper enforcement of that object’s uniqueness and appropriate global access to the object can be problematic.

cynara
Download Presentation

Creational Pattern: Singleton

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creational Pattern: Singleton Chapter 3 – Page 45 When an application needs one, and only one, instance of a particular object, proper enforcement of that object’s uniqueness and appropriate global access to the object can be problematic. This is particularly useful when one object is needed to coordinate actions across a software system. One problem associated with this pattern is the fact that it complicates unit testing, since it introduces a global state into the software system.

  2. The Singleton Pattern Chapter 3 – Page 46 The class of the single instance is responsible for access and "initialization on first use“. The single instance is a protected static attribute in order to guarantee that a new instance is created if one doesn’t already exist, and if one does exist, a reference to that instance is accessible. The constructor is private in order to ensure that the object can only be instantiated via the constructor.

  3. C++ Code for a Logger Singleton Chapter 3 – Page 47 #include <string> #include <iostream> #include <fstream> #include <vector> using namespace std; class Logger { public: static const string kLogLevelDebug; static const string kLogLevelInfo; static const string kLogLevelError; // Returns a reference to the singleton Logger object static Logger& instance(); // Logs a single message at the given log level void log(const string &inMessage, const string &inLogLevel); // Logs a vector of messages at the given log level void log(const vector<string> &inMessages, const string& inLogLevel); protected: // Static variable for the one-and-only instance static Logger sInstance; // Constant for the file name static const char* constkLogFileName; // Data member for the output stream ofstreammOutputStream; private: Logger(); ~Logger(); };

  4. const string Logger::kLogLevelDebug = "DEBUG"; const string Logger::kLogLevelInfo = "INFO"; const string Logger::kLogLevelError = "ERROR"; constchar* const Logger::kLogFileName = "log.txt"; // The static instance will be constructed when // the program starts and destroyed when it ends. Logger Logger::sInstance; Logger& Logger::instance() { returnsInstance; } Logger::~Logger() { mOutputStream.close(); } Logger::Logger() { mOutputStream.open(kLogFileName); if (!mOutputStream.good()) cerr << "Unable to initialize the Logger!" << endl; } void Logger::log(const string &inMessage, const string &inLogLevel) { mOutputStream << inLogLevel << ": " << inMessage << endl; } void Logger::log(const vector<string> &inMessages, const string &inLogLevel) { for (inti = 0; i < (int)inMessages.size(); i++) log(inMessages[i], inLogLevel); } Chapter 3 – Page 48

  5. Chapter 3 – Page 49 void main() { Logger::instance().log("test message", Logger::kLogLevelDebug); vector<string> items; items.push_back("item1"); items.push_back("item2"); Logger::instance().log(items, Logger::kLogLevelError); }

  6. Singleton Design Advantages Chapter 3 – Page 50 • The Singleton pattern ensures that the class (not the programmer) is responsible for the number of instances created. • Unlike static variables, the Singleton pattern is extensible, so if the software design later changes so a larger (but still controlled) number of instances is needed, only the instance() operator needs to be changed. • Unlike global variables, singletons don’t pollute the namespace with unnecessary variables.

More Related