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();
{
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
}
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 void buildCalendar()
{
//put all panels at proper positions
}
@Override
public void createBottomPanel()
{
//create bottom panel
}
@Override
public void createTopPanel()
{
//create top panel
}
public abstract JComponent getCalendar()
{ //return the object
}}
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