Monday, September 15, 2008

Creational Patterns contd. - Builder & Prototype


Creational Patterns contd. - Builder Design Pattern & Prototype Design Pattern


Builder Design Pattern - this design pattern is used for building a different set of objects based on some passed parameters. We already know that Factory Pattern also does a similar thing, so why to have a different design pattern. Well... Factory Pattern is used to return objects of different type (descendents of a common base class) based on passed parameters, but here we build a set of objects and not just return a descendant object. Got the point?


Let's take an example to understand it better. Suppose you have some data (let's say a table) which you would like to be shown using different graph types based on how many records the table has. For instance, if the number of records are less than 5 then you would like to have a pie-chart otherwise a bar-chart. In such a case this design pattern suggests you to have an abstract class representing the base data (the records to be shown in our case, say in form of Vector or some other suitable collection). You can now derive that abstract class into two concrete classes - PieDisplay and BarDisplay and now one of these two concrete classes can be chosen inside a DisplayFactory class depending upon whether the number of records.


abstract class DisplayRecords{

private Vector recordsToDisplay;

public DisplayRecords(Vector records){

recordsToDisplay = records; }

abstract public Panel display();

...

...

}


class PieDisplay extends DisplayRecords{

...

public PieDisplay(Vector records){

super(records); }

public Panel display(){

//... implementation of pie graph display

... }

...

}


class BarDisplay extends DisplayRecords{

...

public BarDisplay(Vector records){

super(records); }

public Panel display(){

//... implementation of bar graph display

... }

...

}


//...Factory class to make a choice

class DisplayFactory{

...

private DisplayRecords displayRecords;

public DisplayRecords getDisplay(Vector records){

//...building the display

if(records.size() <=5 )

displayRecords = new PieDisplay(records);

else displayRecords = new BarDisplay(records);

return displayRecords;}

...

}



Prototype Design Pattern - this design pattern is used in cases where the instance creation of the class is very complex and/or time consuming so instead of creating new objects everytime we copy the already existing original instance and modify the properties of that instance as required. For example: suppose you have created an object by executing many time consuming database queries and if the new objects can be constructed by modifying the queried data (especially in the cases where you read the DB to create various kind of reports) then you would certainly not like to fire all the queries all over again to create new objects. Instead copying the original instance and modifying the copied data to suit the requirements of the new report will be a better option here.


How to copy an alraedy existing instance? You can use clone method to do that for you. Not sure how to use Cloning? Read this article to have a understanding of what Cloning is all about and how can you implement Shallow Cloning and Deep Cloning - Cloning in Java >>


The limitations of this design pattern is that you may come across some classes where you may not implement cloning - for instance, if the classes are not new instead they are already existing and being widely used, if the classes contain objects which can't implement Serializable interafce, or some other possible reason which may stop the classes to become Cloneable. This completes our discussion of the popular Creational Design Patterns.


Lastly, you may like to summarize all five creational design patterns. Here is a quick briefing:-


Singleton Design Pattern - used to ensure that there is only one global instance shared by all the consumers. It's normally implemented by having a static field which is assigned to the global instance on its first use.


Factory Design Pattern - used to choose and return an an instance based on some passed parameters. The common base class is used as the return type for all the descendants instances.


Abstract Factory Design Pattern - it provides one level higher abstraction that the Factory Design Pattern and it's used to return one of the many groups based on some passed parameter. These groups are normally implemented using Factory Design Pattern. That means Abstract Factory normally returns Factory :-)


Builder Design Pattern - used for assembing and returing a set of objects based on some passed parameters. The selection is normally made usnig a Factory and the assembly is made by extending common abstract base class.


Prototype Design Pattern - used when instantiation of a class is very complex and/or time-consuming. Instead of direct instantiation and already existing instance is Cloned and the necessary changes and made to the cloned copy.



Share/Save/Bookmark


No comments: