Showing posts with label Design Pattern. Show all posts
Showing posts with label Design Pattern. Show all posts

Friday, September 23, 2011

Singleton pattern


Sometimes it’s important for classes to have exactly one instance.
For example: Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager.

In such scenario Singleton Design Pattern comes to help.

Singleton pattern ensures a class only has one instance, and provides a global point of access to it.

Implementing Singleton

To implement singleton we need do following two things.
1.    Ensure a class only has one instance:
 This can be achieved by making constructor of a class private ( or protected: In case you want to extend this class.).
2.    Provides a global point of access to instance:
This can be achieved by keeping static reference to unique instance and having method to get that instance.

Tuesday, September 20, 2011

Design Patterns




A brief history of design patterns:


Christopher Alexander:
Design patterns were first described by architect Christopher Alexander in his book A Pattern Language: Towns, Buildings, Construction (Oxford University Press, 1977).

Christopher Alexander says,

"Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice"

Gang of Four (or GoF):

The concept Christopher introduced and called patterns -- abstracting solutions to recurring design problems – caught the attention of researchers in other fields, especially those developing object-oriented software in the mid-to-late 1980s.