Builder模式

2017-07-21  本文已影响1人  耳_总

Builder模式是一步一步构建一个复杂对象的创建型模式,他允许用户在不知道构建细节的情况下,可以更加精细的控制构建流程。该模式是是为了将构件细节和它的部件解耦,使得构建过程和部件分离开来。构建过程和部件可以进行扩展。

举个栗子:
假设我们创建一个人,一个人需要body、head、hand、foot这些必要的属性,还可能需要一些装饰的属性如:衣服(clothes)、鞋子(shoe)、帽子(cap)等等,然后我们开始动手了,这些属性在我们构造一个对象的时候要么通过构造函数传递,要么通过set方法:

public class Man {
    private String head;
    private String body;
    private String hand;
    private String foot;
    private String clothes;
    private String shoe;
    private String cap;

    public Man(String head) {
        this.head = head;
    }

    public Man(String head, String body) {
        this.head = head;
        this.body = body;
    }

    public Man(String head, String body, String hand) {
        this.head = head;
        this.body = body;
        this.hand = hand;
    }

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getHand() {
        return hand;
    }

    public void setHand(String hand) {
        this.hand = hand;
    }

    public String getFoot() {
        return foot;
    }

    public void setFoot(String foot) {
        this.foot = foot;
    }
}

在我们使用的时候:

public class MyClass {

    public MyClass() {
    }

    public static void main(String[] strs) {
        Man man = new Man("头", "脚");
        man.setHand("手");
        man.setCap("帽子");
        ...
    }

}

可以看到构造方法有非常多个,而且通过还要通过setxxx方式去设置属性,使用起来非常麻烦而且容易出错,在这种情况下,builder模式就能很好解决问题:

public class Man {
    private String head;
    private String body;
    private String hand;
    private String foot;
    private String clothes;
    private String shoe;
    private String cap;

    public Man(Builder builder) {
        this.head = builder.head;
        this.body = builder.body;
        this.hand = builder.hand;
        this.foot = builder.foot;
        this.clothes = builder.clothes;
        this.shoe = builder.shoe;
        this.cap = builder.cap;
    }



    public class Builder {
        private String head;
        private String body;
        private String hand;
        private String foot;
        private String clothes;
        private String shoe;
        private String cap;

        public Builder(){

        }

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

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

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

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

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

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

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

        public Man build() {
            return  new Man(this);
        }
    }
}
public static void main(String[] strs) {
       new Man.Builder()
               .setCap("帽子")
               .setHead("头")
               .setBody("身体")
               .build();
               
    }

这样子用链式调用的模式来构造对象非常清晰明了。

上一篇 下一篇

猜你喜欢

热点阅读