Pages

Tuesday, 16 April 2013

Difference between Singleton and Factory Design Patterns: Purpose and Implementation

Difference between Singleton and Factory Design Patterns: Purpose and Implementation

In this article we will try to understand difference between singleton and factory design patterns. What is the need of these design patterns and how do we implement singleton and factory design patterns in real life? Lets start with singleton design patterns.

Singleton design pattern is a design pattern that restricts the instantiation of a class to one object. The Singleton pattern ensures that only one instance of the class exists and  provides a global point for accessing it.

What is the purpose of singleton design pattern?

The purpose of the singleton design pattern is where you want all calls to go through the same instance. Singleton design pattern ensures that no matter how many times a client (or multiple clients) ask for an instance of this specific Type to be created (instantiated) for them, they will always get the exact same one and only one instance of the type. It guarantees that only one instance of the type can ever be instantiated.

This is useful when exactly one object is needed to coordinate actions across the system. There is a Type which is managing a 200 MB in memory cached copy of some data structure - you only want one copy to exist.

How to implement singleton design patterns?

Implementation of a singleton pattern must satisfy the single instance and global access principles. It requires a mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among class objects. The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made private. Note the distinction between a simple static instance of a class and a singleton: although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed. Another notable difference is that static member classes cannot implement an interface, unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, it really has to be a singleton.

The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one. If the programming language has concurrent processing capabilities the method should be constructed to execute as a mutually exclusive operation.

For example: You may like to use this design pattern to implement Cache of an application. How do we implement the singleton Design Pattern? The easiest way is to have a static variable storing the refence of the shared instance and keeping the constructor(s) private so that the users can't call the new operator to create instances. Such an implementation provides a static public method to the consumers to get the shared instance.

class SingletonImpl{
...
private static singletonInstance = null;
...
private SingletonImpl(){
...
}
public synchronized static getSingleton(){
if(singletonInstance == null) singletonInstance = new SingletonImpl();
return singletonInstance;}
...
}

Limitation of singleton design patterns

Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

Read in detail on DZone

What is Factory Design Pattern?

The purpose of the Factory method is to provide a new instance of the object whenever the Factory method is being called. When you are dealing with lot of different interrelated classes, it is better to use the Factory to streamline the object creational.The Factory pattern defines an interface for creating objects (no limitation on how many) and usually abstracts the control of which class to instantiate.

What is the purpose of factory design pattern?

The Factory design pattern is used to return an instance of one of several possible classes depending on what parameters are actually passed by the consumer.

How to implement factory design patterns?

We normally use inheritance to implement this. Suppose we have a base class named 'Base' and two chilldren of this base class named 'Child1' and 'Child2'. Now the Factory class which is the decision making body based on what the consumer provides decides whether to return an instance of the class 'Child1' or 'Child2'. We can easily set the return type of the method of the Factory (which is exposed to the consumers) as the Base Class type and then the method can easily return instances of either of the two children.

class BaseClass {
...
}
class Child1 extends BaseClass{
...
}
class Child2 extends BaseClass{
...
}
public class FactoryImpl{
...
public BaseClass getChild(String parameter){
...
if(parameter.equals("Child1")) //... sample cond impl
return new Child1();
else return new Child2();
}
...
}


Read Factory Pattern in detail on DZone and ASPAlliance

No comments:

Post a Comment