Thursday 8 March 2007

Criticism on one the Patterns (Blog 3)

Singleton

Going through briefly on 23 patterns the one I liked and would like to share is Singleton pattern. The reason of choosing this pattern is that it is easy to understand and more explainable. After doing research on internet about this pattern according to my understanding the definition of single pattern is:

“The singleton design pattern is designed to limit instantiation of a class to one object”.

Singleton is most likely the most commonly used design pattern. The main aim of singleton pattern is to provide one instance for each class and a gateway where everyone can access in to it. There are many situations in which a singleton object is necessary: a GUI application must have a single mouse click, an active modem needs one and only one telephone line, an operating system can only have one window manager, and a PC is connected to a single keyboard.

CRITICISM

Intent
The main intention of a Singleton is to make sure to have only one instance of this class with a global point of access to it, thus avoiding the use of global variables and conflicts among them.

Motivation
Sometimes! Some classes should only have one instance, and be available from everywhere; here singleton pattern can be applied. So the instance which is created by a class will have global access and the constructor of the class blocks more than one instance to be produced.

Applicability
Singleton pattern can be used in this case when:

There should be only one instance of a class and can be accessed by many clients from a well known point. If some global object is a client of another one, the latter may not be destroyed until the former terminates. No distinction between individual and composed objects.

Structure
Structure of Singleton Pattern is in the form of a diagram in which the structure is described of how the class can be limited to one instance.

Collaboration
The thing I like about singleton is that and would like to share is that Clients get the right to access a Singleton instance only through the operation which is performed by Singleton's Instance.
Consequences
The Singleton pattern has several benefits:
1-Each instance is controlled properly in such a way that it limits the clients control on it depending how and when they try to access it.

2-Limits the size of name space. As we all know now that singleton pattern creates only one instance of a class therefore it also provides perfection over global variables.. It avoids polluting the name space with simultaneous access. There is a possibility that there could be a subclass of a Singleton class and It is very easy to configure a program with the help of an instance of the extended class.

Implementation
To implement a Singleton patter, class is created with a method which creates a new instance of the object if the object is not there. If an instance is already there it simply returns a reference to that object. To prevent the object to be instantiated in any other way, a private or protected constructor should be created.
Sample Code
Here is a sample of how singleton is used in programming.

public class Singleton
{
private static Singleton NEWINSTANCE = new Singleton();
private Singleton() {}
public static final Singleton getInstance()
{
return NEWINSTANCE;
}}


Known-Uses
In this section, the known uses of this pattern can be understood with the help looking in to examples.
Suppose if we are authorized to have only one license of a particular database, Singleton takes care that only one connection has been to the database to access records and suppose if we had more connections to the database then singleton can also adjust more than one connections. [1]

Related Patterns
Singleton pattern is mostly used with Abstract Factory, Builder, and Prototype. The Singleton is a bit similar to a Cache manager, but with one object.
Reference:
[1]Example of Singleton

9 comments:

Bash said...

I understood what you are trying to explain, but is it mostly used during the development ???

Hani said...

Generally, sometime you might find yourself using a completely static
class (all members static, only static methods) a singleton class would be a good substitute in the software development during that scenario..

John Ssebaale said...

Hi, thanks for the work on critiquing the cataloguing technique for the singleton pattern. Am just wondering, the critism would have come out well had you CRITISED it, that is whether in your view the technique is useful or not. I may be wrong and stand to be corrected
John

John Ssebaale said...

Hi, thanks for the work on critiquing the cataloguing technique for the singleton pattern. Am just wondering, the critism would have come out well had you CRITISED it, that is whether in your view the technique is useful or not. I may be wrong and stand to be corrected
John

Hani said...

Hi John,sorry for the late reply,the critism could be in favour or against the pattern.I am in favour of singleton..!

Anonymous said...

hrzhsvcpydpxlpxkoyirx. http://www.acnetreatment2k.com/ - acne treatment
jrxltz

Anonymous said...

Il semble que vous soyez un expert dans ce domaine, vos remarques sont tres interessantes, merci.

- Daniel

Anonymous said...

This is wonderful blog. I love it.

Anonymous said...

I seldom leave comments on blog, but I have been to this post which was recommended by my friend, lots of valuable details, thanks again.