Skip to main content

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 Car
  • SUV 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:

  • SportsCar and SUVCar

Create a Director class.

  • It receives an instance of SportsCar or SUVCar
  • It will have two methods, one to construct a SportsCar and another to construct a SUVCar. Example: buildSportsCar() and buildSUVCar()

Create an Application class

  • This is the client
  • Create an instance of Car type (Sports or SUV)
  • Create an instance of Director and passes the Car instance to it
  • Call specific mehod on director to construct the car, buildSportsCar() or buildSUVCar()
  • Get the Car instance from builder ex: getProduct()
  • Note that:
    • The process of builing Car is abstracted into Director and is hidden from the client. Application does not know the details of building a Car.
    • Client only need to associate a builder with the director, 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

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 a Cabin may be built of wood but walls of a Castle need to be built with stone then create different buiilder classes with same set of building steps, to have common interface.
  • Director knows which building steps to execute to get a working product. It helps reusing construction routines and also hides the details of product construction from client code
  • Director plays the role of abstraction, while differrent builders acts as implementations

Builder Pattern example in JDK

  • StringBuilder is an example where we can see the use of Builder design pattern. Note that it does not ues any Director, it just uses series of methods to construct the String step by step. Final string Object can be obtained by calling toString() method.