Java Design Patterns--Builder Pattern

1 Builder Pattern Builder Pattern

Purpose: To separate a complex build from its representation so that the same build process can create different representations;
Implementation: Builder: Creates and provides instances, Director: Manages dependencies of built instances.

The builder pattern can be considered in the following cases:
1. The product objects to be generated have complex internal structures, and these product objects usually contain multiple member attributes;
2. The attributes of the product objects to be generated depend on each other, and its generation needs to be specified Sequence;
3. The creation process of an object is independent of the class that created the object. In the builder pattern, by introducing the conductor class, the creation process is encapsulated in the conductor class, not in the builder class and the client class;
4. Isolate the creation and use of complex objects, and enable the same creation process to create different products.

2 Implementation

Code scene: During the Warring States Period, various vassal states had a lot of reform movements in order to gain a favorable position in the struggle for hegemony and not be annexed by other states. Talents have carried out a series of reforms to the country to make the country look new.

2.1 Code Implementation

Product Category: Country

public class Country {
    //国家的名字
    private String countryname;
    //国家的政治
    private String polotics;
    //国家的军事
    private String army;
    //国家的农业
    private String agriculture;
    //国家的新面貌
    private String buildResult;

    public String getCountryname() {
        return countryname;
    }

    public void setCountryname(String countryname) {
        this.countryname = countryname;
    }

    public String getPolotics() {
        return polotics;
    }

    public void setPolotics(String polotics) {
        this.polotics = polotics;
    }

    public String getArmy() {
        return army;
    }

    public void setArmy(String army) {
        this.army = army;
    }

    public String getAgriculture() {
        return agriculture;
    }

    public void setAgriculture(String agriculture) {
        this.agriculture = agriculture;
    }

    public String getBuildResult() {
        return buildResult;
    }

    public void setBuildResult(String buildResult) {
        this.buildResult = buildResult;
    }
    //展示国家风采
    public void showPower() {
        System.out.println("国家:" + countryname);
        System.out.println("政治:" + polotics);
        System.out.println("军事:" + army);
        System.out.println("农业:" + agriculture);
        System.out.println("结果:" + buildResult);
        System.out.println("-----------------------");
    }
}

builder abstract class

public abstract class Builder {
    //政治改革
    public abstract void buildPolotics();
    //军事改革
    public abstract void buildArmy();
    //农业改革
    public abstract void buildAgriculture();
    //改革成果
    public abstract Country getCountry();
}

The specific builder Shang Yang: made some reforms to the Qin state

public class ShangYangBuilder extends Builder {
    //为秦国进行改革
    private Country qin = new Country();
    @Override
    public void buildPolotics() {
        qin.setPolotics("中央集权,推行县制");
        qin.setCountryname("秦");
    }
    @Override
    public void buildArmy() {
        qin.setArmy("励军功,实行二十等爵制");
    }
    @Override
    public void buildAgriculture() {
        qin.setAgriculture("废井田,开阡陌");
        qin.setBuildResult("日渐强盛,收复失地,终一统天下!");
    }
    @Override
    public Country getCountry() {
        return qin;
    }
}

Concrete Builder Shin Bu-ha: Made some reforms to Korea

public class ShenBuHaiBuilder extends Builder {
    //为韩国进行改革
    private Country han = new Country();;
    @Override
    public void buildPolotics() {
        han.setPolotics("整顿吏治,加强君主集权统治");
        han.setCountryname("韩");
    }
    @Override
    public void buildArmy() {
        han.setArmy("整肃军兵,严酷训练");

    }
    @Override
    public void buildAgriculture() {
        han.setAgriculture("多开荒地,多种粮食");
        han.setBuildResult("政局得到稳定,贵族受到限制,百姓渐趋富裕!");

    }
    @Override
    public Country getCountry() {
        return han;
    }
}

Director Category: Princes

//director导演角色 
public class King {
    //诸侯授权变法者执行变法
    public Country authorizeConstruct(Builder b) {
        b.buildPolotics();
        b.buildArmy();
        b.buildAgriculture();
        return b.getCountry();
    }
}

2.2 Involving roles

The builder pattern structure diagram includes the following roles:

Builder (abstract builder): It specifies an abstract interface for creating each part of a Product object, and generally declares two types of methods in this interface. One method is buildPartX(), which is used to create various parts of complex objects; the other One class of methods is getResult(), which returns complex objects. Builder can be either an abstract class or an interface.

ConcreteBuilder (concrete builder): It implements the Builder interface, implements the specific construction and assembly methods of each component, defines and clarifies the complex objects it creates, and can also provide a method to return the created complex product objects.

Product (Product Role): It is a complex object that is built, containing multiple components, and the specific builder creates the internal representation of the product and defines its assembly process.

Director: The director is also known as the director class. It is responsible for arranging the construction order of complex objects. There is an association between the director and the abstract builder, and the components of the builder object can be called in its construct() construction method. Construction and assembly methods to complete the construction of complex objects. The client generally only needs to interact with the conductor, determine the type of the specific builder on the client, and instantiate the specific builder object (also through the configuration file and reflection mechanism), and then pass the constructor or Setter method of the conductor class Pass this object into the conductor class.

2.3 call

caller

public class Client {
    public static void main(String[] args) {
        // 发法执行人:申不害
        Builder shenBuHai = new ShenBuHaiBuilder();
        // 发法执行人:商鞅
        Builder shangYang = new ShangYangBuilder();
        // 诸侯
        King king = new King();
        // 诸侯授权执行变法
        Country han = king.authorizeConstruct(shenBuHai);
        Country qin = king.authorizeConstruct(shangYang);
        han.showPower();
        qin.showPower();
    }
}

result:

国家:韩
政治:整顿吏治,加强君主集权统治
军事:整肃军兵,严酷训练
农业:多开荒地,多种粮食
结果:政局得到稳定,贵族受到限制,百姓渐趋富裕!
-----------------------
国家:秦
政治:中央集权,推行县制
军事:励军功,实行二十等爵制
农业:废井田,开阡陌
结果:日渐强盛,收复失地,终一统天下!
-----------------------

The difference between builder and factory mode:
Builder mode: focus on the behavior of producing products
Factory mode: focus on the attributes of producing products


Code address: click to jump

References:
[1] Graphical Design Patterns/(Japanese) Hiroshi Yuki; translated by Yang Wenxuan. – Beijing: People’s Posts and Telecommunications Press, 2017.1.
[ 2 ] Wikipedia Design
Patterns [ 3 ] Geek Academy WIKI – Design Patterns .
[ 4 ] Rookie Tutorial – Design Patterns .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325463266&siteId=291194637