Thoughts on Singleton Design Pattern – Good Bad Ugly

Posted by admin on August 17, 2009 under Software Architect | Be the First to Comment




Singleton patternis one of the best-known patterns in software engineering. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance. Most commonly, singletons don’t allow any parameters to be specified when creating the instance – as otherwise a second request for an instance but with a different parameter could be problematic. (if the same instance should be accessed for all requests with the same parameter, the factory pattern is more appropriate. )

  1. Why Singletons are Evil – While I did not write this, I totally agree with it. Brian Button is probably one of the most talented people I know, and I am sure he would love your feedback.
  2. We Don’t Need No Stinking Singletons: Why to Avoid the Singleton Pattern – OOP Techniques for Flash and Flex Developers
  3. Singletons vs. Static Classes – My last post seemed to elicit a good number of interesting responses. Today, I’m musing about Singletons vs. Static Classes — two constructs which appear to accomplish the same task. Or do they?
  4. Design Patterns – Singleton Pattern – A quick look at Singleton Pattern
  5. Chill out on the Singleton Fetish – ALT.NET dotnet .NET C# Agile BizTalk ASP.NET
  6. Know your design tools – The Singleton case – A professional software designer is one whose next paycheck depends on the quality of her software – looks for an ever increasing acquaintance with his design tools. One of the most important design tools in software is the actual computing machine is abstract,
  7. Exploring the Singleton Design Pattern – Exploring the Singleton Design Pattern
  8. Singleton – the most overused pattern. – Do you wonder why singleton is overused?
  9. The Singleton Pattern Revisited – A fresh look at Singleton Design Pattern
  10. Singletons – we are better off without them – Why we should avaoid it.
  11. Double-checked locking and the Singleton pattern – All programming languages have their share of idioms. Many are useful to know and use, and programmers spend valuable time creating, learning, and implementing them. The problem is that some idioms are later proven not to be all that they were purported, or to simply not work as described. Double-checked locking is one such idiom in the Java programming language that should never be used. In this article, Peter Haggar examines the roots of the double-checked locking idiom, why it was developed, and why it doesn’t work.
  12. Singleton pattern – At Wikipedia, the free encyclopedia
  13. Singleton pattern in MultiThreaded Environment – A Journey Through the Minds of EBW Thought Leaders for dealing with issues caused due to Singleton in Multithreaded implementations

Singleton pattern is: make all the methods as static, if for any reason you cannot make all the methods as static, then go for static instance approach. I feel keeping all methods as static methods is better design and developer friendly.

Share This Post

GoF Design Patterns Template

Posted by admin on August 1, 2009 under Software Architect | Be the First to Comment




The GoF design patterns are in the middle of these levels of abstraction

A design pattern names, abstracts, and identifies key aspects of a common design structure that makes it useful for creating a reusable object-oriented design.

The GoF design patterns are

descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.

GoF Classification Of Design Patterns

Purpose- what a pattern does

  1. Behavioral Patterns – Deal with the interaction of classes and objects
  2. Creational Patterns – Concern the process of object creation
  3. Structural Patterns – Deal with the composition of classes and objects

Scope- what the pattern applies to

Class Patterns

  • Focus on the relationships between classes and their subclasses
  • Involve inheritance reuse

Object Patterns

  • Focus on the relationships between objects
  • Involve composition reuse

GoF Essential Elements Of Design Patterns

Pattern Name

  • Having a concise, meaningful name for a pattern improves communication among developers

Problem

  • What is the problem and context where we would use this pattern?
  • What are the conditions that must be met before this pattern should be used?

Solution

  • A description of the elements that make up the design pattern
  • Emphasizes their relationships, responsibilities and collaborations
  • Not a concrete design or implementation; rather an abstract description

Consequences

  • The pros and cons of using the pattern
  • Includes impacts on reusability, portability, extensibility

GoF Pattern Template

Pattern Name and Classification A good , concise name for the pattern and the pattern’s type
Intent Short statement about what the pattern does
Also Known As Other names for the pattern
Motivation A scenario that illustrates where the pattern would be useful
Applicability Situations where the pattern can be used
Structure A graphical representation of the pattern
Participants The classes and objects participating in the pattern
Collaborations How to do the participants interact to carry out their responsibilities?
Consequences What are the pros and cons of using the pattern?
Implementation Hints and techniques for implementing the pattern
Sample Code Code fragments for a sample implementation
Known Uses Examples of the pattern in real systems
Related Patterns Other patterns that are closely related to the pattern

References: Prof. Bob Tarr's Notes

Share This Post