Builder with Director Design Pattern - Java
Plan
Demonstrate Builder with Director design pattern using Java
Example
Lets write a program to build Cars !
Write code to build the followig types of Cars. Program should be extensible, that is adding more types of Cars should not increase the complexity of program.
Sports CarSUV Car
Sports Car should have the following:
- Sports Engine
- 2 Seats
- GPS Installed
- Trip Computer Installed
SUV should have the following:
- SUV Engine
- 7 Seats
- GPS Installed
- Trip Computer Installed
Approach
- First come up with a common interface for both Cars.
Example:
interface Car {
setEngine(Engine typeOfEngine),
setSeats(numOfSeats),
setGPS(true/false)
setTripComputer(true/false)
startCar()
stopCar()
}
Note: Engine can be a SportsEngine or SUVEngine. Engine is a common interface for both types of engines in this case.
Create the following classes implementing Car interface:
SportsCarandSUVCar
Create a Director class.
- It receives an instance of
SportsCarorSUVCar - It will have two methods, one to construct a
SportsCarand another to construct aSUVCar. Example:buildSportsCar()andbuildSUVCar()
Create an Application class
- This is the client
- Create an instance of
Cartype (Sports or SUV) - Create an instance of
Directorand passes theCarinstance to it - Call specific mehod on director to construct the car,
buildSportsCar()orbuildSUVCar() - Get the
Carinstance from builder ex:getProduct() - Note that:
- The process of builing Car is abstracted into
Directorand is hidden from the client. Application does not know the details of building a Car. - Client only need to associate a
builderwith thedirector, launch the construction with director and get the final object from builder - In future, adding one more type of Car can be achieved with minimum impact to client and it does not increase the complexity
- The process of builing Car is abstracted into
How to run the program
- Clone the github repository
git clone https://github.com/smarigowda/BuilderWithDirectorDesignPattern- Open the project in IntelliJ IDE
- Run the program

Sequence Diagram

When to use builder + director pattern ?
When the Object contains many attributes we face issues with Factory and Abstract Factory design patterns which is commonly known as telescopic constructor issue
- When there are many arguments to pass from client program to the Factory class it can cause errors because most of the time, type of arguments are same.
- Some of the parameters may be optional but in Factory pattern, we are forced to send all the parameters and optional parameters need to be set to NULL
- If the object creation is complex, then all that complexity will be part of Factory classes
Notes
- Builder pattern is used to
construct complex objects step by step - Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations
- Builder does not allow other objects to access it while the object is still being built
- If some of the steps need different implementation.
Ex:Walls of aCabinmay be built ofwoodbut walls of aCastleneed to be built withstonethen create different buiilder classes withsame set of building steps, to have common interface. Directorknows which building steps to execute to get a working product. It helps reusing construction routines and alsohides the details of product constructionfrom client codeDirectorplays the role of abstraction, while differrentbuildersacts as implementations
Builder Pattern example in JDK
StringBuilderis an example where we can see the use ofBuilderdesign pattern. Note that it does not ues anyDirector, it just uses series of methods to construct the String step by step. Final string Object can be obtained by callingtoString()method.