Wednesday, May 26, 2010

The Builder Pattern

    The Builder pattern, as its name suggests, builds objects step by step. Usually the building process is an abstraction to the user and the actual building is taken care of by the individual objects. The builder class is called as the 'Director'. The Director is responsible for the correct creation of the concrete object. It is better understood if we take an example. Let us assume this: We have a application which pops up a calendar. The user may want to look at only a month or look at all the months of a year. (May be this is a mind blowing application which shows appointment details in detail if a monthly calendar is shown or just blocked days of a year if yearly calendar is shown. It doesn't matter for our discussion here anyways).
    Lets see how this is done. First we need to create a abstract class which serves as a parent for the monthly and yearly calendar classes.

AbstractCalendar.java:

public abstract class AbstractUICalendar
{  
    public abstract void createTopPanel();
    public abstract void createBottomPanel();
    public abstract void buildCalendar();  
    public abstract JComponent getCalendar();     
}

Now we create the Monthly and yearly calendars.

MonthlyCalendar.java

public class MonthlyCalendar extends AbstractUICalendar
{
    @Override
    public void buildCalendar()
    {
       //put all panels at proper positions
    }
         
    @Override
    public void createBottomPanel()
    {
        //create bottom panel
    }
   
    @Override
    public void createTopPanel()
    {
       //create top panel
    }
    @Override
    public abstract JComponent getCalendar()
    {
         //return the object
    }
}

YearlyCalendar.java

public class YearlyCalendar extends AbstractUICalendar
{
    @Override
    public void buildCalendar()
    {
       //put all panels at proper positions
    }
         
    @Override
    public void createBottomPanel()
    {
        //create bottom panel
    }
   
    @Override
    public void createTopPanel()
    {
       //create top panel
    }
    @Override
    public abstract JComponent getCalendar()
    {
         //return the object
    }
}

    I have skipped all the low level details of the panel creations and calendar creation logic in the code so that you can concentrate on the Pattern instead of the how the calendar is being created.
    Now we have created the concrete classes. It time for us to create the Director. If you remember, the Director is responsible for building the required object. Below is that Director.

public class CalendarBuilder
{
    AbstractUICalendar m_calendar;
    public CalendarBuilder(AbstractUICalendar calendar)
    {
        m_calendar = calendar;
    }
    public JComponent getUI()
    {
        m_calendar.createTopPanel();
        m_calendar.createBottomPanel();
        m_calendar.buildCalendar();
        return m_calendar.getCalendar(); 

        /*The above returns the complete calendar*/
    }
}


    The Builders are ready now. Now we can make use of the builder to get a calendar of our choice. This 'choice' of the calendar UI is determined by the calendar object that we pass to the constructor of CalendarBuilder. Lets see how this is done below:

BuilderMain.java


public class BuilderMain
{
    public static void main(String[] args)

    {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    AbstractUICalendar calendar = null;
   
    String calType = "MONTHLY"; //Hardcoded choice
   
    if(calType.equals("MONTHLY")
    {
        calendar = new MonthlyCalendar();
    }
    else
    {
        calendar = new YearlyCalendar();
    }  
    CalendarBuilder cb = new CalendarBuilder(calendar);  
    frame.getContentPane().add(cb.getUI());  
    frame.pack();
    frame.setVisible(true);
    }
}



    In the above example I have hardcoded the choice. But in real time applications, the choice may come from a button or any other parameter.
    The result of the above code is given below just for your reference. 

Monthly Calendar 

Yearly Calendar

    If you feel the Builder pattern somewhat similar to Abstract Factory pattern, read on. Or else....well.. read on anyway. The Abstract Factory pattern creates objects based on some condition and return the objects to the caller. But the builder pattern 'builds' objects step by step based on the object presented to it.
    This makes the builder pattern very flexible and you can add more classes easily.
You have more control over the build process because of the step-by-step building process of this pattern.

No comments:

Post a Comment