0% found this document useful (0 votes)
297 views8 pages

Builder Design Pattern

The document discusses the Builder design pattern in software engineering. The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations. It builds an object step-by-step and returns the final product. For example, when building a computer, individual components like the CPU, monitor, and peripherals can be constructed separately and then combined into a final computer object. The Builder pattern improves modularity and allows more control over the final product construction. Sample Java code is provided to demonstrate building a computer product using the Builder pattern.

Uploaded by

kasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
297 views8 pages

Builder Design Pattern

The document discusses the Builder design pattern in software engineering. The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations. It builds an object step-by-step and returns the final product. For example, when building a computer, individual components like the CPU, monitor, and peripherals can be constructed separately and then combined into a final computer object. The Builder pattern improves modularity and allows more control over the final product construction. Sample Java code is provided to demonstrate building a computer product using the Builder pattern.

Uploaded by

kasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA Means DURGA SOFT

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 1
JAVA Means DURGA SOFT

Builder Design Pattern


Builder, as the name suggests builds complex objects from simple ones step-by-step. It
separates the construction of complex objects from their representation.

Problem: In order to complete a task working with simple and individual objects is always
complex process to perform.

Solution: Use Builder design pattern. Which says creates complex object from simple objects
and work with that complex object to complete the task.

As a programmer (user) who utilizes this complex object need not bather about the
simple and individual objects that are involved in complex object creation.

Ex: When we create object for our frame class (which extends predefined java.awt.Frame class)
lots of individual components like Text Field, Button, Label, List Box etc. objects will be created
internally. But as a user of this Frame, the Window class object never bather about those
individual objects. This is nothing but Builder Design Pattern.

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 2
JAVA Means DURGA SOFT

Builder pattern is used to construct a complex object step by step and the final step will
return the object. The process of constructing an object should be generic so that it can be used
to create different representations of the same object.

For example, you can consider construction of a home. Home is the final end product
(object) that is to be returned as the output of the construction process. It will have many
steps, like basement construction, wall construction and so on roof construction. Finally the
whole home object is returned. Here using the same process you can build houses with
different properties.

Note: GOF says,

“Separate the construction of a complex object from its representation so that the same
construction process can create different representations” i.e. Builder Design Pattern

Application Areas: Use the Builder pattern when

 The creation algorithm of a complex object is independent from the parts that actually
compose the object.
 The system needs to allow different representations for the objects that are being built.

Note

Each builder is independent of others. This improves modularity and makes the building
of other builders easy. Because, each builder builds the final product step by step, we have more
control on the final product.

Sample Code:
// BuilderTest.java

class Cpu

public String name()

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 3
JAVA Means DURGA SOFT

return "CPU";

public int price()

return 7000;

class Monitor

public String name()

return "Monitor";

public int price()

return 5000;

class Perphirals

public String name()

return "Perphirals";
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 4
JAVA Means DURGA SOFT

public int price()

return 4500;

public class BuilderTest

public static void main(String[] args) throws Exception

BuilderTest bt=new BuilderTest();

String s=bt.parts();

int cost=bt.calPrice();

System.out.println();

System.out.println("Your Product Consists Of "+s);

System.out.println();

System.out.println("Your Product Total Cost is Rs."+cost+"/-");

public String parts()

return "One "+new Cpu().name()+", One "+new Monitor().name()+" & Some "+

new Perphirals().name();

public int calPrice()


DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 5
JAVA Means DURGA SOFT

return new Cpu().price()+new Monitor().price()+new Perphirals().price();

Output

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 6
JAVA Means DURGA SOFT

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 7
JAVA Means DURGA SOFT

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 8

You might also like