美文网首页
建造者模式(3)-盖房子

建造者模式(3)-盖房子

作者: Shaw_Young | 来源:发表于2020-12-27 16:34 被阅读0次
    package com.young.builder.improve;
    
    /**
     * 产品->Product
     *
     * @author Shaw_Young
     * @date 2020/10/6 20:57
     */
    public class House {
    
        private String baise;
        private String wall;
        private String roofed;
    
        public String getBaise() {
            return baise;
        }
    
        public void setBaise(String baise) {
            this.baise = baise;
        }
    
        public String getWall() {
            return wall;
        }
    
        public void setWall(String wall) {
            this.wall = wall;
        }
    
        public String getRoofed() {
            return roofed;
        }
    
        public void setRoofed(String roofed) {
            this.roofed = roofed;
        }
    }
    
    package com.young.builder.improve;
    
    /**
     * 抽象建造者
     *
     * @author Shaw_Young
     * @date 2020/10/6 20:59
     */
    public abstract class HouseBuilder {
        protected House house = new House();
    
        //将建造的流程写好,抽象的方法
        public abstract void buildBasic();
    
        public abstract void buildWall();
    
        public abstract void roofed();
    
        //建造房子后,将产品(房子)返回
        public House buildHouse() {
            return house;
        }
    }
    
    package com.young.builder.improve;
    
    /**
     * @author Shaw_Young
     * @date 2020/10/6 21:02
     */
    public class CommonHouse extends HouseBuilder {
        @Override
        public void buildBasic() {
            System.out.println("普通房子打地基5米");
        }
    
        @Override
        public void buildWall() {
            System.out.println("普通房子砌墙10cm");
        }
    
        @Override
        public void roofed() {
            System.out.println("普通房子屋顶");
        }
    }
    
    package com.young.builder.improve;
    
    /**
     * @author Shaw_Young
     * @date 2020/10/6 21:04
     */
    public class HighBuilding extends HouseBuilder {
    
        @Override
        public void buildBasic() {
            System.out.println("高楼打地基100米");
        }
    
        @Override
        public void buildWall() {
            System.out.println("高楼砌墙20cm");
        }
    
        @Override
        public void roofed() {
            System.out.println("高楼的透明屋顶");
        }
    }
    
    package com.young.builder.improve;
    
    /**
     * 指挥者,这里去指定制作流程
     *
     * @author Shaw_Young
     * @date 2020/10/6 21:05
     */
    public class HouseDirector {
    
        HouseBuilder houseBuilder = null;
    
        /**
         * 构造器传入houseBuilder
         */
        public HouseDirector(HouseBuilder houseBuilder) {
            this.houseBuilder = houseBuilder;
        }
    
        /**
         * 通过setter,传入houseBuilder
         */
        public void setHouseBuilder(HouseBuilder houseBuilder) {
            this.houseBuilder = houseBuilder;
        }
    
        /**
         * 如何处理建造房子的流程,交给指挥者
         */
        public House constructHouse() {
            houseBuilder.buildBasic();
            houseBuilder.buildWall();
            houseBuilder.roofed();
            return houseBuilder.buildHouse();
        }
    
    }
    
    package com.young.builder.improve;
    
    /**
     * @author Shaw_Young
     * @date 2020/10/6 21:10
     */
    public class Client {
    
        public static void main(String[] args) {
            //盖普通房子
            CommonHouse commonHouse = new CommonHouse();
            //准备创建房子的指挥者
            HouseDirector houseDirector = new HouseDirector(commonHouse);
            //完成盖房子,返回产品(房子)
            House house = houseDirector.constructHouse();
        }
    
    }
    

    相关文章

      网友评论

          本文标题:建造者模式(3)-盖房子

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