노현진's Blog

Builder Pattern

Builder Pattern에 대해 설명하는 페이지입니다.

Posted
Preview Image
By HyunJinNo

Tags

Design Pattern, Java

1. Introduction

  • Purpose
    • Allows for the dynamic creation of objects based upon easily interchangeable algorithms.
  • Use When
    • Runtime control over the creation process is required.
    • Multiple representations of creation algorithms are required.
    • Object creation algorithms should be decoupled from the system.
    • The addition of new creation functionality without changing the core code is necessary.

2. Characteristics

  • Director knows what parts are needed for the final product
  • Concrete builder knows how to produce the part and add it to the final product.
  • A Builder is a Strategy that is specialized to create a composite object or data structure
  • Builder constructs the object step-by-step and the result is requested at a later stage

3. Participants

  • Client
    • selects director and concrete builder to build the product
    • asks concrete builder to return final constructed product
  • Director
    • knows what steps it takes to build a product
    • but it does not know how each step is to be carried out
  • Builder
    • specifies an abstract interface for creating parts of a Product object
  • Concrete Builder
    • constructs and assembles parts of the product by implementing the Builder interface
    • defines and keeps track of the representation it creates
    • provides an interface for retrieving the product
  • Product
    • represents the complex object under construction

4. How to Use (Example)

  • Director

    java
    1public class AerospaceEngineer {
    2    private AirplaneBuilder airplaneBuilder;
    3
    4    public void setAirplaneBuilder(AirplaneBuilder ab) {
    5        airplaneBuilder = ab;
    6    }
    7
    8    public Airplane getAirplane() {
    9        return airplaneBuilder.getAirplane();
    10    }
    11
    12    public void constructAirplane() {
    13        airplaneBuilder.createNewAirplane();
    14        airplaneBuilder.buildWings();
    15        airplaneBuilder.buildPowerplant();
    16        airplaneBuilder.buildAvionics();
    17        airplaneBuilder.buildSeats();
    18    }
    19}
  • Abstract Builder

    java
    1public abstract class AirplaneBuilder {
    2    protected Airplane airplane;
    3    protected String customer;
    4    protected String type;
    5
    6    public Airplane getAirplane() {
    7        return airplane;
    8    }
    9
    10    public void createNewAirplane() {
    11        airplane = new Airplane(customer, type);
    12    }
    13
    14    public abstract void buildWings();
    15    public abstract void buildPowerplant();
    16    public abstract void buildAvionics();
    17    public abstract void buildSeats();
    18}
  • Product

    java
    1public class Airplain {
    2    private String type;
    3    private float wingspan;
    4    private String powerplant;
    5    private int crewSeats;
    6    private int passengerSeats;
    7    private String avionics;
    8    private String customer;
    9
    10    public Airplain(String customer, String type) {
    11        this.customer = customer;
    12        this.type = type;
    13    }
    14
    15    public void setWingspan(float w) {
    16        this.wingspan = w;
    17    }
    18
    19    public void setPowerplant(String p) {
    20        this.powerplant = p;
    21    }
    22
    23    public void setAvionics(String a) {
    24        this.avionics = a;
    25    }
    26
    27    public void setNumberSeats(int crewSeats, int passengerSeats) {
    28        this.crewSeats = crewSeats;
    29        this.passengerSeats = passengerSeats;
    30    }
    31
    32    public String getCustomer() {
    33        return customer;
    34    }
    35
    36    public String getType() {
    37        return type;
    38    }
    39}
  • Concrete Builder

    java
    1public class CropDuster extends AirplaneBuilder {
    2    public CropDuster (String customer) {
    3        super.customer = customer;
    4        super.type = "Crop Duster v3.4";
    5    }
    6
    7    public void buildWings() {
    8        airplane.setWingspan(9f);
    9    }
    10
    11    public void buildPowerplant() {
    12        airplane.setPowerplant("single piston");
    13    }
    14
    15    public void buildAvionics() {}
    16
    17    public void buildSeats() {
    18        airplane.setNumberSeats(1,1);
    19    }
    20}
  • Client

    java
    1public class BuilderExample {
    2    public static void main(String[] args) {
    3        // instantiate the director (hire the engineer)
    4        AerospaceEngineeraero = new AerospaceEngineer();
    5
    6        // instantiate each concrete builder (take orders)
    7        AirplaneBuildercrop = new CropDuster("Farmer Joe");
    8        AirplaneBuilderfighter = new FighterJet("The Navy");
    9        AirplaneBuilderglider = new Glider("Tim Rice");
    10
    11        // build a CropDuster
    12        aero.setAirplaneBuilder(crop);
    13        aero.constructAirplane();
    14        Airplane completedCropDuster= aero.getAirplane();
    15        System.out.println(completedCropDuster.getType() +
    16            " is completed and ready for delivery to " +
    17            completedCropDuster.getCustomer());
    18}

© HyunJinNo. Some rights reserved.