Click here to Skip to main content
13,195,379 members (59,773 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.3K views
2 bookmarked
Posted 5 Feb 2010

Builder

, 6 Feb 2010
Rate this:
Please Sign up or sign in to vote.
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.

/** 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:

/** 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:

/** 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:

/** 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:

//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

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:

System.out.print(String.format("Laptop: %s, %s, %s, %s, %s", MonitorResolution,
    Processor, Memory, HDD, Battery));

License

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

Share

About the Author

Andriy Buday
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.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171019.1 | Last Updated 6 Feb 2010
Article Copyright 2010 by Andriy Buday
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid