Extension of the builder mode

Mode extension

In addition to the above purposes, the builder mode also has a commonly used method in development.

That is, when a class constructor needs to pass in many parameters, if you create an instance of this class, the code readability will be very poor, and it is easy to introduce errors .

At this point, you can use the builder mode for reconstruction.

The code before reconstruction is as follows:


public class Phone {
    private String cpu;
    private String screen;
    private String memory;
    private String mainboard;
​
    public Phone(String cpu, String screen, String memory, String mainboard) {
        this.cpu = cpu;
        this.screen = screen;
        this.memory = memory;
        this.mainboard = mainboard;
    }
​
    public String getCpu() {
        return cpu;
    }
​
    public void setCpu(String cpu) {
        this.cpu = cpu;
    }
​
    public String getScreen() {
        return screen;
    }
​
    public void setScreen(String screen) {
        this.screen = screen;
    }
​
    public String getMemory() {
        return memory;
    }
​
    public void setMemory(String memory) {
        this.memory = memory;
    }
​
    public String getMainboard() {
        return mainboard;
    }
​
    public void setMainboard(String mainboard) {
        this.mainboard = mainboard;
    }
​
    @Override
    public String toString() {
        return "Phone{" +
                "cpu='" + cpu + '\'' +
                ", screen='" + screen + '\'' +
                ", memory='" + memory + '\'' +
                ", mainboard='" + mainboard + '\'' +
                '}';
    }
}
public class Client {
    public static void main(String[] args) {
        //构建Phone对象
        Phone phone = new Phone("intel","三星屏幕","金士顿","华硕");
        System.out.println(phone);
    }
}


The Phone object is constructed in the client code above, and four parameters are passed. What if there are more parameters?

The readability of the code and the cost of use are relatively high.



Refactored code:

Phone.java

package com.itheima.pattern.builder.demo2;


/**
 * @version v1.0
 * @ClassName: Phone
 * @Description: 手机类
 * @Author: dym
 */
public class Phone {

    private String cpu;
    private String screen;
    private String memory;
    private String mainboard;

    //私有构造方法
    private Phone(Builder builder) {
        this.cpu = builder.cpu;
        this.screen = builder.screen;
        this.memory = builder.memory;
        this.mainboard = builder.mainboard;
    }

    @Override
    public String toString() {
        return "Phone{" +
                "cpu='" + cpu + '\'' +
                ", screen='" + screen + '\'' +
                ", memory='" + memory + '\'' +
                ", mainboard='" + mainboard + '\'' +
                '}';
    }

    public static final class Builder {
        private String cpu;
        private String screen;
        private String memory;
        private String mainboard;

        public Builder cpu(String cpu) {
            this.cpu = cpu;
            return this;
        }

        public Builder screen(String screen) {
            this.screen = screen;
            return this;
        }
        public Builder memory(String memory) {
            this.memory = memory;
            return this;
        }
        public Builder mainboard(String mainboard) {
            this.mainboard = mainboard;
            return this;
        }

        //使用构建者创建Phone对象
        public Phone build() {
            return new Phone(this);
        }
    }
}

Client.java

package com.itheima.pattern.builder.demo2;

/**
 * @version v1.0
 * @ClassName: Client
 * @Description: TODO(一句话描述该类的功能)
 * @Author: dym
 */
public class Client {
    public static void main(String[] args) {
        //创建手机对象   通过构建者对象获取手机对象
        Phone phone = new Phone.Builder()
                .cpu("intel")
                .screen("三星屏幕")
                .memory("金士顿内存条")
                .mainboard("华硕主板")
                .build();

        System.out.println(phone);
    }
}



The refactored code is more convenient to use and can also improve development efficiency to a certain extent.

Guess you like

Origin blog.csdn.net/qq_39368007/article/details/113946295