Builder builder mode

Reference from: https://www.jianshu.com/p/afe090b2e19c

Introduction:

Builder pattern is a common design pattern in daily development. Its main function is to abstract the process of creating complex things. Different implementation methods of this abstraction and different objects are created. Generally speaking, creating an object usually has a fixed step. We abstract it from this fixed step. Each abstract step will have different implementation methods, and the objects created by different implementation methods will be different. As a common example, we must all know that Tank Wars is a small game. Different modes have different maps (simple version and complex version by default). If we choose the simple version, there may be no blockhouses, mines, etc. in the constructed map, these things may be included in the complex version, We can abstract the construction steps of this map and build different contents according to different versions.

Usage scenario:

If a class has more than four constructors, and some parameters are optional, we should consider using the builder mode to build the object of this class

Mode structure:

As can be seen from the above figure, there are three roles in Buider mode:

  1. Product to build – map of the build
  2. Abstract Builder – build blockhouses, mines, walls, etc
  3. Specific implementation of Builder SimpleBuilder and ComplexBuilder – implementation of construction steps in Builder

Example of classic builder mode:

Next, let's look at how to implement the above map construction process with code:

1.Terrain topographic map:

public class Terrain {
    Wall w;
 Fort f;
 Mine m;
}
class Wall {
    int x, y, w, h;
 public Wall(int x, int y, int w, int h) {
        this.x = x;
 this.y = y;
 this.w = w;
 this.h = h;
 }
}
class Fort {
    int x, y, w, h;
 public Fort(int x, int y, int w, int h) {
        this.x = x;
 this.y = y;
 this.w = w;
 this.h = h;
 }
}
class Mine {
    int x, y, w, h;
 public Mine(int x, int y, int w, int h) {
        this.x = x;
 this.y = y;
 this.w = w;
 this.h = h;
 }
}

2. TerrainBuilder Abstract build process

package com.mashibing.Builder;
public interface TerrainBuilder {
    TerrainBuilder buildWall();
 TerrainBuilder buildFort();
 TerrainBuilder buildMine();
 Terrain build();
}

3. Specific construction process of complextrainbuilder

This specific construction process can have multiple implementations. For example, the game can build multiple different maps according to different modes (simple version, classic version, complex version...)

package com.mashibing.Builder;
public class ComplexTerrainBuilder implements TerrainBuilder{
    Terrain terrain = new Terrain();
 @Override
 public TerrainBuilder buildWall() {
        terrain.w = new Wall(10,10,50,50);
 return this; }
    @Override
 public TerrainBuilder buildFort() {
        terrain.f = new Fort(10,10,50,50);
 return this; }
    @Override
 public TerrainBuilder buildMine() {
        terrain.m = new Mine(10,10,50,50);
 return this; }
    @Override
 public Terrain build() {
        return terrain;
 }
}

4. Main use

package com.mashibing.Builder;
public class Main {
    public static void main(String[] args) {
        TerrainBuilder builder = new ComplexTerrainBuilder();
 Terrain t = builder.buildFort().buildWall().buildMine().build();
 }
}

Example of variant builder mode:

1,Person

package com.mashibing.Builder;
public class Person {
    private int id;
 private String name;
 private int age;
 private double weight;
 private int score;
 private Location loc;
 private Person() {}
    public static class PersonBuilder{
        Person p = new Person();
 public PersonBuilder basicInfo(int id,String name,int age){
            p.id = id;
 p.name = name;
 p.age = age;
 return this; }
        public PersonBuilder weight(double weight){
            p.weight = weight;
 return this; }
        public PersonBuilder score(int score){
            p.score = score;
 return this; }
        public PersonBuilder loc(String street,String roomNo){
            p.loc = new Location(street, roomNo);
 return this; }
        public Person build(){return p;}
    }
}
class Location{
    String street;
 String roomNo;
 public Location(String street, String roomNo) {
        this.street = street;
 this.roomNo = roomNo;
 }
}

2. Use

package com.mashibing.Builder;
public class Main {
    public static void main(String[] args) {
 Person p = new Person.PersonBuilder().basicInfo(1,"zhangsan",18)
                .score(20)
                .weight(128.2)
                .loc("a","a").build();
 }
}

Tags: Java Design Pattern

Posted on Mon, 20 Sep 2021 06:10:03 -0400 by programming_passion