C++
  Home arrow C++ arrow C++ In Theory: The Singleton Pattern, Part...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

C++ In Theory: The Singleton Pattern, Part I
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 63
    2005-01-18

    Table of Contents:
  • C++ In Theory: The Singleton Pattern, Part I
  • A Logging Class
  • Statics are not Singletons
  • The Gamma Singleton
  • The Meyers Singleton

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


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


    (Page 1 of 5 )

    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.

    There is a great book called “Design Patterns” [Gamma] that encapsulates knowledge trapped inside the heads of experienced programmers, presenting it in understandable and useable chunks. Solutions to a lot of design problems often turn out to be the same, even when programmers discover them independently. Instead of providing these solutions to you in code format, the “Design Patterns” book presents the thoughts and reasoning behind the solutions along with example code.

    Design Patterns are a great way to get a head start on solving that problem you have encountered or might yet have to encounter. In this article we are going to implement a class with simple logging functionality that will make use of the Singleton pattern. My intention is not to present you with a class that will end all your logging woes, but rather to use it to demonstrate why you need this pattern.

    Writing to a log is an action where it makes sense to have only one single log object active at any time you want to register data with it. It doesn’t matter which part of your application is writing to the log, as long as all parts are using the same log.

    In an older programming language such as C you might choose global variables and global functions to implement such facilities, but as a project grows larger the risk of adding subtle bugs grows as well. These global variables and global functions all end up in the global namespace, and the introduction of a foreign library (or any new code for that matter) might cause naming collisions (meaning there are multiple variables/functions sharing the same name/identifier). Such collisions can only be resolved by changing the names of your own global variables and/or functions; these changes are bug prone and quite a chore in large projects with a lot of source files.

    The benefit of the Singleton pattern (which can be implemented in C++) is that it prevents you from filling up the global namespace. You can keep the variables and functions locked up inside a class and restrict access to it by implementing a single point of access, which prevents you from creating multiple instances of the class. Other great features of the C++ language such as polymorphism (changing the behavior of a class through virtual functions) remain available to you as well.

    More C++ Articles
    More By J. Nakamura


       · The article isw very good for a beginner showing him how the solution...
       · class LogNetwork : public LogNetwork {};I don't think this is right.a class...
       · You are very right it should beclass LogNetwork : public LogBasethx for...
       · "...which can be written to a single file when Log::Write is called."That should...
       · You discussed using polymorphism to have different logging behaviors, and using the...
       · There is implicit conversion from derived class to base class, no reason to use a...
     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek