美文网首页
建造者模式

建造者模式

作者: 天空在微笑 | 来源:发表于2019-08-13 17:28 被阅读0次

    JAVA设计模式之简单粗暴学建造者模式

    将一个复杂对象的构建和它的表示分离,使得同样的构建过程可以创建不同的表示。

    建造者有以下角色:

    • 导演类:负责安排已有模块的安装顺序,最后通知建造者开始建造。
    • 建造者:抽象Builder类,用于规范产品的组建。
    • 具体建造者:实现抽象Builder类的所有方法,并返回建造好的对象。
    • 产品类。
    简单示例

    1、产品类

    public class MobilePhone {
    
        //手机屏幕
        private String screen;
        //电池
        private String battery;
        //话筒
        private String microphone;
        //听筒
        private String phoneReceiver;
        //机身
        private String phoneBody;
    
        public String getScreen() {
            return screen;
        }
    
        public void setScreen(String screen) {
            this.screen = screen;
        }
    
        public String getBattery() {
            return battery;
        }
    
        public void setBattery(String battery) {
            this.battery = battery;
        }
    
        public String getMicrophone() {
            return microphone;
        }
    
        public void setMicrophone(String microphone) {
            this.microphone = microphone;
        }
    
        public String getPhoneReceiver() {
            return phoneReceiver;
        }
    
        public void setPhoneReceiver(String phoneReceiver) {
            this.phoneReceiver = phoneReceiver;
        }
    
        public String getPhoneBody() {
            return phoneBody;
        }
    
        public void setPhoneBody(String phoneBody) {
            this.phoneBody = phoneBody;
        }
    
        @Override
        public String toString() {
            return "MobilePhone{" +
                    "screen='" + screen + '\'' +
                    ", battery='" + battery + '\'' +
                    ", microphone='" + microphone + '\'' +
                    ", phoneReceiver='" + phoneReceiver + '\'' +
                    ", phoneBody='" + phoneBody + '\'' +
                    '}';
        }
    }
    

    2、抽象建造者

    public interface IBuildMobilePhoneBuilder {
        /**
         * 建造手机屏幕
         */
        void buildScreen();
    
        /**
         * 建造手机电池
         */
        void buildBattery();
    
        /**
         * 建造手机听筒
         */
        void buildMicrophone();
    
        /**
         * 建造手机话筒
         */
        void buildPhoneReceiver();
    
        /**
         * 建造手机机身
         */
        void buildPhoneBody();
    
        MobilePhone build();
    }
    
    
    

    3、具体建造者

    public class IPhoneXBuilder implements IBuildMobilePhoneBuilder {
        private MobilePhone mobilePhone;
    
        public IPhoneXBuilder(){
            mobilePhone =  new MobilePhone();
        }
    
        /**
         * 建造手机屏幕
         */
        @Override
        public void buildScreen() {
            mobilePhone.setScreen("OLED显示屏");
        }
    
        /**
         * 建造手机电池
         */
        @Override
        public void buildBattery() {
            mobilePhone.setBattery("2700mAh电池容量");
        }
    
        /**
         * 建造手机听筒
         */
        @Override
        public void buildMicrophone() {
            mobilePhone.setMicrophone("听筒");
        }
    
        /**
         * 建造手机话筒
         */
        @Override
        public void buildPhoneReceiver() {
            mobilePhone.setPhoneReceiver("话筒");
        }
    
        /**
         * 建造手机机身
         */
        @Override
        public void buildPhoneBody() {
            mobilePhone.setPhoneBody("iphoneX机身");
        }
    
    
        /**
         * 创建手机
         * @return
         */
        public MobilePhone build(){
            return mobilePhone;
        }
    }
    
    

    4、导演类

    public class Director {
        /**
         * 建造一部手机
         * @param buildPhone
         * @return
         */
        public MobilePhone createMobilePhone(IBuildMobilePhoneBuilder buildPhone){
    
            buildPhone.buildBattery();
            buildPhone.buildMicrophone();
            buildPhone.buildScreen();
            buildPhone.buildPhoneReceiver();
            buildPhone.buildPhoneBody();
    
            return buildPhone.build();
        }
    }
    
    
    • 屏蔽产品内部组成细节。
    • 具体建造者类之间相互独立,容易扩展。
    • 会产生多余的建造者对象和导演类。

    另一种实现方式:

    public class Student {
    
        String name = null;
        int number = -1;
        String sex = null;
        int age = -1;
        String school = null;
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", number=" + number +
                    ", sex='" + sex + '\'' +
                    ", age=" + age +
                    ", school='" + school + '\'' +
                    '}';
        }
    
        //构建器,利用构建器作为参数来构建Student对象
        public static class StudentBuilder {
            String name = null;
            int number = -1;
            String sex = null;
            int age = -1;
            String school = null;
    
            public StudentBuilder setName(String name) {
                this.name = name;
                return this;
            }
    
            public StudentBuilder setNumber(int number) {
                this.number = number;
                return this;
            }
    
            public StudentBuilder setSex(String sex) {
                this.sex = sex;
                return this;
            }
    
            public StudentBuilder setAge(int age) {
                this.age = age;
                return this;
            }
    
            public StudentBuilder setSchool(String school) {
                this.school = school;
                return this;
            }
    
            public Student build() {
                return new Student(this);
            }
        }
    
        public Student(StudentBuilder builder) {
            this.age = builder.age;
            this.name = builder.name;
            this.number = builder.number;
            this.school = builder.school;
            this.sex = builder.sex;
        }
    }
    
      @Test
        public void thatTest(){
    //        System.out.println(new Director().createMobilePhone(new IPhoneXBuilder()));
            Student a = new Student.StudentBuilder().setAge(13).setName("LiHua").build();
            Student b = new Student.StudentBuilder().setSchool("sc").setSex("Male").setName("ZhangSan").build();
            System.out.println(a);
            System.out.println(b);
        }
    

    相关文章

      网友评论

          本文标题:建造者模式

          本文链接:https://www.haomeiwen.com/subject/ulebjctx.html