Home arrow C++ arrow Page 3 - C++ In Theory: The Singleton Pattern, Part I

C++ In Theory: The Singleton Pattern, Part I

Have you ever wondered how to implement a class with simple logging functionality? J. Nakamura explains how to do it in a way that makes use of the Singleton pattern.

Author Info:
By: J. Nakamura
Rating: 4 stars4 stars4 stars4 stars4 stars / 87
January 18, 2005
  1. · C++ In Theory: The Singleton Pattern, Part I
  2. · A Logging Class
  3. · Statics are not Singletons
  4. · The Gamma Singleton
  5. · The Meyers Singleton

print this article

C++ In Theory: The Singleton Pattern, Part I - Statics are not Singletons
(Page 3 of 5 )

Forcing the existence of only one instance of the Log class seems easily implemented when we use only static variables and functions:

class Log {
  static void Write(char const *logline);
  static bool SaveTo(char const *filename);
  static std::list<std::string> m_data;

In log.cpp we need to add

std::list<std::string> Log::m_data;

Though it looks like it fulfills our goal, there are some downsides to these kinds of singleton implementations. First of all, the construction and destruction of static member variables are difficult to localize. They are instantiated in any sequence by grace of the compiler, outside any constructors. This can cause tremendous problems when singletons depend on the right construction/destruction sequence

Second, it is not possible to implement polymorphic behavior this way because static functions cannot be virtual. This means that a singleton implemented with static variables/functions cannot be accessed through its baseclass. For example, when our application runs standalone on a machine we probably want to save the log to a file, but when it is part of a distributed application we might want to save the log to a logging server. Instead of bundling all functionality into one big fat "does-everything" class, we split the behavior up in to separate classes, all implementing the interface of the abstract baseclass:

// LogBase.h
class LogBase {
  virtual ~LogBase() {}
  virtual void Write(char const *logline)=0;
  virtual bool SaveTo(char const *filename)=0;

The destructor of our baseclass has to be declared virtual, otherwise the destructor of a derived class will not be called when it is destroyed through the baseclass.

// LogFile.h
class LogFile : public LogBase {
  virtual ~LogFile();
  virtual void Write(char const *logline);
  virtual void SaveTo(char const *filename);
  std::list<std::string> m_data;

// LogNetwork.h
class LogNetwork : public LogNetwork {
  virtual ~LogNetwork();
  virtual void Write(char const *logline);
  virtual void SaveTo(char const *filename);
  virtual void SetServer(TCPConnection const &server);
  TCPConnection m_logServer;
  std::list<std::string> m_data;

It is good to give each class its own responsibility, and to choose which Log class you will use during runtime. This prevents us from having to instantiate code we will not need:

int main(int argc, char *argv[])
  LogBase *myLog = NULL;
if (argc == 1)
    return –1;

  if (strcmp(argv[1], ”-StandAlone”)==0)
    myLog = reinterpret_cast<LogBase*>(new LogFile);
  else if (strcmp(argv[1], “-Networked”)==0)
  TCPConnection fictServer(“”);
  LogNetwork *logPtr = new LogNetwork;
    myLog = reinterpret_cast<LogBase*>(logPtr); 
    return –1;
  /* more code here */
  // logging example
  myLog->Write(“test line going to either a file or network log.”);

This is of course a fictional example, but I hope you get the idea of what polymorphic behavior can mean for your application. In the example above, application code can log through the LogBase class, but depending on the arguments given during start-up, the logs will either be written to file or to a network server.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials