Builder Pattern
The builder pattern is a creational design pattern that allows you to separate the steps of creating a complex object from its representation.
The builder pattern is a creational design pattern whose main purpose is to separate the construction process of a complex object from its representation, so that objects with different representations can be created.
Summary
Intent
Separate a complex construction process from its representation, so that the same construction process can create different representations.
Main problem solved
In software systems, the creation of a complex object usually consists of multiple parts; the combination of these parts often changes, but the algorithm of combination is relatively stable.
When to use it
When some basic parts remain unchanged while their combination often changes.
How to solve
Separate the changing parts from the unchanging parts.
Key code
- Builder: create and provide instances.
- Director: manage the dependencies of the built instances and control the construction process.
Application Example
- Going to KFC, hamburgers, cola, fries, fried chicken wings, etc. are unchanged, while their combinations often change, generating different "set meals".
- Java Date Class
StringBuilder。
Advantages
- Separate the construction process from the representation, making the construction process more flexible and able to build different representations.
- Can better control the construction process and hide specific construction details.
- High code reusability; the same builder can be reused in different construction processes.
Disadvantages
- If a product has few attributes, the builder pattern may lead to code redundancy.
- Increases the number of classes and objects in the system.
Use Cases
- The objects to be generated have complex internal structures.
- The internal attributes of the objects to be generated are interdependent.
Notes
The difference from the factory pattern is: the builder pattern focuses more on the order of part assembly.
structures
The builder pattern contains the following main roles:
-
Product (Product): the complex object to be built. A product class usually contains multiple parts or attributes.
-
Abstract Builder (Builder): defines the abstract interface for building the product, including methods for building each part of the product.
-
Concrete Builder (Concrete Builder): implements the abstract builder interface, specifically determining how to build each part of the product, and is responsible for returning the final built product.
-
Director (Director): responsible for calling the builder's methods to build the product; the director does not know the specific construction process, only cares about the order and manner of product construction.
Create a class that implements
We assume a business case of a fast-food restaurant, in which a typical set meal can be a Burger and a Cold drink. A Burger can be a Veg Burger or a Chicken Burger, which are wrapped in paper boxes. A Cold drink can be coke or pepsi, which are in bottles.
We will create a Item Interface and Implementation Item interface and concrete classes implementing it, as well as a Packing Interface and Implementation Packing interface and its concrete classes for representing food packaging; hamburgers are wrapped in paper boxes, cold drinks are in bottles.
Then we create a Meal class with Item 's ArrayList and a Item to create different types of Meal object's MealBuilder。BuilderPatternDemo Class uses MealBuilder to create a Meal。
Step 1
Create interfaces representing food items and food packaging.
Item.java
public
interface
Item
{
public
String
name
(
)
;
public
Packing
packing
(
)
;
public
float
price
(
)
;
}
Packing.java
public
interface
Packing
{
public
String
pack
(
)
;
}
Step 2
Create concrete classes that implement the Packing interface.
Wrapper.java
public
class
Wrapper
implements
Packing
{
@
Override
public
String
pack
(
)
{
return
"
Wrapper
"
;
}
}
Bottle.java
public
class
Bottle
implements
Packing
{
@
Override
public
String
pack
(
)
{
return
"
Bottle
"
;
}
}
Step 3
Create an abstract class that implements the Item interface, which provides default functionality.
Burger.java
public
abstract
class
Burger
implements
Item
{
@
Override
public
Packing
packing
(
)
{
return
new
Wrapper
(
)
;
}
@
Override
public
abstract
float
price
(
)
;
}
ColdDrink.java
public
abstract
class
ColdDrink
implements
Item
{
@
Override
public
Packing
packing
(
)
{
return
new
Bottle
(
)
;
}
@
Override
public
abstract
float
price
(
)
;
}
Step 4
Create concrete classes that extend Burger and ColdDrink.
VegBurger.java
public
class
VegBurger
extends
Burger
{
@
Override
public
float
price
(
)
{
return
25
.0
f
;
}
@
Override
public
String
name
(
)
{
return
"
Veg Burger
"
;
}
}
ChickenBurger.java
public
class
ChickenBurger
extends
Burger
{
@
Override
public
float
price
(
)
{
return
50
.5
f
;
}
@
Override
public
String
name
(
)
{
return
"
Chicken Burger
"
;
}
}
Coke.java
public
class
Coke
extends
ColdDrink
{
@
Override
public
float
price
(
)
{
return
30
.0
f
;
}
@
Override
public
String
name
(
)
{
return
"
Coke
"
;
}
}
Pepsi.java
public
class
Pepsi
extends
ColdDrink
{
@
Override
public
float
price
(
)
{
return
35
.0
f
;
}
@
Override
public
String
name
(
)
{
return
"
Pepsi
"
;
}
}
Step 5
Create a Meal class with the Item objects defined above.
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
.0
f
;
for
(
Item
item
:
items
)
{
cost
+=
item
.
price
(
)
;
}
return
cost
;
}
public
void
showItems
(
)
{
for
(
Item
item
:
items
)
{
System
.
out
.
(
"
Item :
"
item
.
name
(
)
)
;
System
.
out
.
(
"
, Packing :
"
item
.
packing
(
)
.
pack
(
)
)
;
System
.
out
.
println
(
"
, Price :
"
item
.
price
(
)
)
;
}
}
}
Step 6
Create a MealBuilder class; the actual builder class is responsible for creating Meal objects.
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
;
}
}
Step 7
BuiderPatternDemo uses MealBuilder to demonstrate the builder pattern (Builder Pattern).
BuilderPatternDemo.java
public
class
BuilderPatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
MealBuilder
mealBuilder
=
new
MealBuilder
(
)
;
Meal
vegMeal
=
mealBuilder
.
prepareVegMeal
(
)
;
System
.
out
.
println
(
"
Veg Meal
"
)
;
vegMeal
.
showItems
(
)
;
System
.
out
.
println
(
"
Total Cost:
"
vegMeal
.
getCost
(
)
)
;
Meal
nonVegMeal
=
mealBuilder
.
prepareNonVegMeal
(
)
;
System
.
out
.
println
(
"
\n
\n
Non-Veg Meal
"
)
;
nonVegMeal
.
showItems
(
)
;
System
.
out
.
println
(
"
Total Cost:
"
nonVegMeal
.
getCost
(
)
)
;
}
}
Step 8
Run the program, output result:
Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 25.0
Item : Coke, Packing : Bottle, Price : 30.0
Total Cost: 55.0
Non-Veg Meal
Item : Chicken Burger, Packing : Wrapper, Price : 50.5
Item : Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.5