2. Course No: CSE 3120
Shaikh Akib Shahriyar
Lecturer
Department of Computer Science & Engineering
Khulna University of Engineering & Technology
Course Name : Software Engineering & Information
Systems Laboratory
Md. Abdus Salim Mollah
Assistant Professor
Department of Computer Science & Engineering
Khulna University of Engineering & Technology
Course Teacher
2
4. About Presentation
This presentation provide information to understand builder
design pattern, itâs structure, itâs implementation.
We have tried our best to explain the concept in very simple
language. The programming language used for
implementation is Java. But any one from different
programming background can easily understand the
implementation..
4
5. Design Pattern
A pattern is the outline of a reusable solution to a
general problem encountered in a particular
context
In software engineering, a software design
pattern is a general, reusable solution to a
commonly occurring problem within a given
context in software design.
5
7. Builder Design Pattern
The Builder Design Pattern is a creational design pattern,
designed to provide a flexible design solution to various
object creation problems in Object-Oriented Software.
The intent of the Builder Design Pattern is to separate the
construction of a complex object from its representation.
The process of constructing an object should be generic so
that it can be used to create different representations of the
same object.
7
14. Builder
This abstract base class defines all of the steps that must
be taken in order to correctly create a product.
Each step is generally abstract as the actual functionality
of the builder is carried out in the concrete subclasses.
The GetProduct method is used to return the final product.
The builder class is often replaced with a simple interface.
14
16. Concrete Builder
There may be any number of concrete builder classes
inheriting from Builder.
These classes contain the functionality to create a
particular complex product.
16
18. Director
The director class controls the algorithm that generates the
final product object.
A director object is instantiated and its Construct method is
called.
The method includes a parameter to capture the specific
concrete builder object that is to be used to generate the
product.
The director then calls methods of the concrete builder in
the correct order to generate the product object.
On completion of the process, the GetProduct method of
the builder object can be used to return the product.
18
26. Burger.Java
public abstract class Burger implements Item {
@Override public Packing packing() { return new
Wrapper(); } @Override public abstract float price(); }
.
Create abstract classes implementing the item
interface providing default functionalities
26
27. ColdDrink.Java
public abstract class ColdDrink implements Item {
@Override public Packing packing() { return new
Bottle(); } @Override public abstract float price(); }
27
28. Burger.Java
public class VegBurger extends Burger
{ @Override public float price() { return 25.0f; }
@Override public String name() { return "Veg
Burger";
} }
.
Create concrete classes extending Burger and
ColdDrink classes Veg
28
30. Coke.Java
public class Coke extends ColdDrink { @Override
public float price() { return 30.0f; } @Override public
String name() { return "Coke"; } }
30
31. Pepsi.Java
public class Pepsi extends ColdDrink { @Override
public float price() { return 35.0f; } @Override public
String name() { return "Pepsi"; } }
31
32. Meal.Java
import java.util.ArrayList; import java.util.List; public
class Meal { private List<Item> items = new
ArrayList<Item>(); public void addItem(Item item){
items.add(item); } public float getCost(){ float cost =
0.0f; for (Item item : items) { cost += item.price(); }
return cost; } public void showItems(){ for (Item item :
items) { System.out.print("Item : " + item.name());
System.out.print(", Packing : " +
item.packing().pack()); System.out.println(", Price : "
+ item.price()); } } }
Create a Meal class having Item objects defined
above
32
33. MealBuilder.Java
public class MealBuilder { public Meal
prepareVegMeal (){ Meal meal = new Meal();
meal.addItem(new VegBurger()); meal.addItem(new
Coke()); return meal; } public Meal
prepareNonVegMeal (){ Meal meal = new Meal();
meal.addItem(new ChickenBurger());
meal.addItem(new Pepsi()); return meal; } }
.
Create a MealBuilder class, the actual builder
class responsible to create Meal objects
33