java——简易版build模式

参考教程:https://blog.csdn.net/fanxudonggreat/article/details/78927773

public class Computer {
    private String mBoard;
    private String mDisplay;
    private String mOS;

    public String getmBoard() {
        return mBoard;
    }

    public void setmBoard(String mBoard) {
        this.mBoard = mBoard;
    }

    public String getmDisplay() {
        return mDisplay;
    }

    public void setmDisplay(String mDisplay) {
        this.mDisplay = mDisplay;
    }    

    public String getmOS() {
        return mOS;
    }

    public void setmOS() {
       this.mOS = mOS;
    }


    public static class Builder {

        private String mBoard;
        private String mDisplay;
       private String mOS;

       public Builder setBoard(String board) {
            this.mBoard = board;
            return this;
       }

        public Builder setDisplay(String display) {
            this.mDisplay = display;
            return this;
        }

        public Builder setOs() {
            return this;
        }

    /**
     * 组装产品
     */
    private void construct(Computer computer) {
        computer.setmBoard(mBoard);
        computer.setmDisplay(mDisplay);
        computer.setmOS();
    }

    public Computer create() {
        Computer computer = new Computer();
        construct(computer);
        return computer;
    }
}

创建实例:

Computer computer = new Computer.Builder().setBoard("")
                .setDisplay("")
                .setOs()
                .create();

 

posted @ 2018-11-18 15:58  高圈圈  阅读(1792)  评论(0编辑  收藏  举报