Monday, September 8, 2008

Creational Patterns - Singleton, Factory, & Abstract Fac


If you're interested in refreshing your understanding of Design Patterns and their Types before moving on to the details of Creational Patterns then you may refer to this article - Design Patterns - Types & Precepts >>


Creational Design Patterns


As the name suggests these design patterns are used to control the instantiation of a class. Why do we need it? Because the program should not be dependent on how an user of the class instantiates it. The supplier of the class should take the responsibility of controlling the instantiation process in case it affects the execution of the program. For Example: if a class is supposed to have only one instance for it to work properly for an application then in such a case the supplier of the class needs to make the necessary arrangements that all the users of that cass get only the one shared instance. This is normally achieved by using the Singleton Design Pattern which we'll discuss next.


Few popular Creational Design Patterns


The Singleton Pattern - this design pattern is used to ensure that only one instance of the class is being used across the entire application. 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;}
...
}


The Factory Pattern - this 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 do we implement this design pattern? 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();
}
...
}


The Abstract Factory Pattern - this design pattern is very similar to the Factory Pattern with the only difference that it takes the abstraction to another level higher. This pattern is basically used to return one of several related classes of objects i.e., one of several Factories (which in turn return the actual instance based on the passed parameter as explained above). One common example of this design pattern is to implement user interfaces. Suppose you're building an UI for three platforms - Windows, Mac, and Linux. Now you can use Abstract Factory pattern to return each of the Factories (Windows, Mac, and Linux) which will themselves return one of the various UI Components based on the passed parameter.


public abstract class UI{
public abstract UIComponent getButton(int, int, int, int);

...
}
public class UIComponent{
private int x1;private int y1;private int x2;private int y2;
public UIComponent(int x1, int y1, int x2, int y2){...}
}
public class Button extends UIComponent{...}

...
public class WindowsUI extends UI{...}
public class UIFactory {
private UI ui;
public UI getUI(String type){
if(type.equals("Win")) ui = new WindowsUI();

...

return ui;

}
}


This is just a sample implementation to help you understand how actually we make use of the Abstract Factory Pattern. The actual UI implmenetation will of course be quite different.


Liked the article? You may like to Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. You can find the 'Followers' widget in the rightmost sidebar.



Share/Save/Bookmark


No comments: