The builder mode of GOF23 design mode

Posted by FireyIce01 on Mon, 10 Feb 2020 08:30:31 +0100

Catalog

Usage scenarios

- we want to build a complex product. For example: Shenzhou spaceship, Iphone. The creation of this complex product. There is a problem that needs to be addressed:
• is there a step problem in assembling these subcomponents?

– in actual development, the object construction we need is also very complex, and there are many steps to deal with.

The essence of builder mode

- separate the individual construction (by Builder) and assembly (by Director) of the object's subcomponents. Thus, complex objects can be constructed. This pattern is applicable to the complex construction process of an object.

– due to the decoupling of construction and assembly. Different builders, the same assembly, can also make different objects; the same builder, different assembly order can also make different objects.
That is to say, the decoupling of construction algorithm and assembly algorithm is realized and better reuse is realized

Application scenario in development

  • append method of StringBuilder class
  • PreparedStatement in SQL
  • In JDOM, DomBuilder and SAXBuilder

Code example

If we build a spaceship, we have spaceship objects and spaceships have their own properties

/**
 * Spacecraft
 */
public class AirShip {

    private OrbitalModule orbitalModule;//Orbital module
    private Engine engine; //Engine
    private EscapeTower escapeTower;//Escape tower

    public void lauch(){
        System.out.println("Launch!");
    }

    public OrbitalModule getOrbitalModule() {
        return orbitalModule;
    }

    public void setOrbitalModule(OrbitalModule orbitalModule) {
        this.orbitalModule = orbitalModule;
    }

    public Engine getEngine() {
        return engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public EscapeTower getEscapeTower() {
        return escapeTower;
    }

    public void setEscapeTower(EscapeTower escapeTower) {
        this.escapeTower = escapeTower;
    }
}

class OrbitalModule{
    private String name;

    public OrbitalModule(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Engine{
    private String name;


    public Engine(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class EscapeTower{
    private String name;

    public EscapeTower(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Define the builder and assembler. The builder is responsible for returning the equipment to the internal parts of feichuang. The assembler is responsible for assembling the parts and generating the spacecraft.

public interface AirShipBuilder {

    Engine builderEngine();
    OrbitalModule builderOrbitalModule();
    EscapeTower builderEscapeTower();

}
public interface AirShipDirector {

    AirShip directAirShip();//Assembled spacecraft
}

Specific realization of assembler and builder

public class SxtAirShipBuilder implements AirShipBuilder {


    @Override
    public Engine builderEngine() {
        System.out.println("Building shangxuetang brand engine");
        return new Engine("Shangxuetang engine");
    }

    @Override
    public OrbitalModule builderOrbitalModule() {
        System.out.println("Construction of shangxuetang brand orbital module");
        return new OrbitalModule("Shangxuetang brand orbital cabin");
    }

    @Override
    public EscapeTower builderEscapeTower() {
        System.out.println("Building shangxuetang escape tower");
        return new EscapeTower("Shangxuetang escape tower");
    }
}
public class SxtAirShipDirector implements AirShipDirector {

    private AirShipBuilder builder;

    public SxtAirShipDirector(AirShipBuilder builder) {
        this.builder = builder;
    }

    @Override
    public AirShip directAirShip() {
        Engine engine = this.builder.builderEngine();
        EscapeTower escapeTower = this.builder.builderEscapeTower();
        OrbitalModule orbitalModule = this.builder.builderOrbitalModule();

        AirShip ship = new AirShip();
        ship.setEngine(engine);
        ship.setEscapeTower(escapeTower);
        ship.setOrbitalModule(orbitalModule);
        return ship;
    }
}

Code call

public class Client {

    public static void main(String[] args) {
        SxtAirShipDirector sxtAirShipDirector = new SxtAirShipDirector(new SxtAirShipBuilder());
        AirShip ship = sxtAirShipDirector.directAirShip();
        System.out.println(ship);
    }
}

As you can see, the caller only needs to deal with the assembler and assembler, and does not need to manage the internal construction details of the spacecraft to achieve decoupling.

58 original articles published, 311 praised, 280000 visitors+
Private letter follow

Topics: SQL