Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / Java

Builder

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Feb 2010CPOL 5.5K   2  
Builder GoF Design patter with Java. Build your laptop example.

Imagine that you have shop where your Customers could buy Laptops with configuration they prefer. You need to develop system which will allow you easily build any configuration of Laptop for any User. How could you easily accomplish this?

Introduction

Builder is a Creational Design Pattern which allows you build some whole Product (Laptop) by constructing together some parts like Processor, Memory, Monitor, HDD, Battery and so on. So your employee talks to Customer and asks questions which memory do you want and so on. Or otherwise, if Customer is not technical, employee could ask "Do you need this laptop for gaming?". So you need some steps to construct your computer and they are defined in Abstract Builder.

C#
/** Abstract Builder */
public abstract class LaptopBuilder {
    protected Laptop laptop;
    
    public void createNewLaptop(){
        laptop = new Laptop();
    }

    public Laptop getMyLaptop(){
        return laptop;
    }
    //mentioned steps to build laptop
    public abstract void setMonitorResolution();
    public abstract void setProcessor();
    public abstract void setMemory();
    public abstract void setHDD();
    public abstract void setBattery();
}

If your Customer answers like "Yes, I wanna play... play...!!!". You already have implementation of Concrete Builder for gaming laptop like:

Java
/** Concrete Builder */
public class GamingLaptopBuilder extends LaptopBuilder {
    public void setBattery() {
        laptop.Battery = "6 lbs";
    }
    public void setHDD() {
        laptop.HDD = "500 Gb";
    }
    public void setMemory() {
       laptop.Memory = "6144 Mb";   
    }
    public void setMonitorResolution() {
        laptop.MonitorResolution = "1900X1200";
    }
    public void setProcessor() {
        laptop.Processor = "Core 2 Duo, 3.2 GHz";
    }
}

Or, if your Customer is a business guy and he watches presentations and Excel reports in plain:

Java
/** Concrete Builder */
public class TripLaptopBuilder extends LaptopBuilder {
    public void setBattery() {
        laptop.Battery = "12 lbs";
    }
    public void setHDD() {
        laptop.HDD = "250 Gb";
    }
    public void setMemory() {
       laptop.Memory = "2048 Mb";
    }
    public void setMonitorResolution() {
        laptop.MonitorResolution = "1200X800";
    }
    public void setProcessor() {
        laptop.Processor = "Celeron 2 GHz";
    }
}

To manage steps to build your Laptop basing on the answer, you need Director:

Java
/** Director */
public class BuyLaptop {
    private LaptopBuilder laptopBuilder;
    
    public void setLaptopBuilder(LaptopBuilder lBuilder){
        laptopBuilder = lBuilder;
    }
    
    public Laptop getLaptop(){
        return laptopBuilder.getMyLaptop();
    }
    
    public void constructLaptop(){
        
        laptopBuilder.createNewLaptop();
        
        laptopBuilder.setMonitorResolution();
        laptopBuilder.setProcessor();
        laptopBuilder.setMemory();
        laptopBuilder.setHDD();
        laptopBuilder.setBattery();
    }
}

Here, we have usage code:

Java
//Your system could have bulk of builders
TripLaptopBuilder tripBuilder = new TripLaptopBuilder();
GamingLaptopBuilder gamingBuilder = new GamingLaptopBuilder();

BuyLaptop shopForYou = new BuyLaptop();//director

shopForYou.setLaptopBuilder(gamingBuilder);//Customer answered that he wants to play
shopForYou.constructLaptop();

Laptop laptop = shopForYou.getLaptop();//just get what he wants
laptop.print();

Output

Java
Laptop: 1900X1200, Core 2 Duo, 3.2 GHz, 6144 Mb, 500 Gb, 6 lbs

Easy, simple and great pattern.

What Have I Learned Regarding Java?

Nothing special, just how to format strings:

Java
System.out.print(String.format("Laptop: %s, %s, %s, %s, %s", MonitorResolution,
    Processor, Memory, HDD, Battery));
This article was originally posted at http://andriybuday.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer SoftServe
Ukraine Ukraine
I'm very pragmatic and self-improving person. My goal is to become successful community developer.
I'm young and love learning, these are precondition to my success.

Currently I'm working in dedicated Ukrainian outsourcing company SoftServe as .NET developer on enterprise project. In everyday work I'm interacting with lot of technologies which are close to .NET (NHibernate, UnitTesting, StructureMap, WCF, Win/WebServices, and so on...)

Feel free to contact me.

Comments and Discussions

 
-- There are no messages in this forum --