美文网首页
Builder模式

Builder模式

作者: 王小贱_ww | 来源:发表于2017-06-17 16:45 被阅读15次

    Builder模式?(好熟悉,貌似有说不上来),在这里好好总结一下。

    Builder模式的介绍

    Builder模式是一步一步创建一个复杂对象的创建者模式, 它允许用户在不知道内部构建细节的情况下,可以精确的控制对象的构造流程。该模式为了将构建复杂对象的过程和它的部件解耦,将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示,

    Builder模式的角色介绍

    • Product产品类 (产品的抽象类)
    • Builder (抽象Builder,规范产品的组件,一般有子类实现具体的组件过程)
    • ConcreteBuilder(具体的Builder类)
    • Director (统一组装过程)

    代码

    public abstract class Product {
    /**
     * 计算机抽象类(Product)
     */
    
        protected  String mBoard;
        protected  String mDisplay;
        protected  String mOs;
    
        public void setmBoard(String mBoard) {
            this.mBoard = mBoard;
        }
    
        public void setmDisplay(String mDisplay) {
            this.mDisplay = mDisplay;
        }
        //设置操作系统,让子类自己去实现
        public abstract  void setmOs();
    }
    
    public class MacBook extends Product {
    /**
     * 具体的Computer类, MacBook
     */
    
        @Override
        public void setmOs() {
            mOs = "苹果系统10.00";
         }
    }
    
    public abstract class Builder {
    /**
     * 抽象Builder
     */
    
        public abstract void buildBoard(String board);
    
        public abstract void buildDisplay(String display);
    
        public abstract void buildOs();
    
        public abstract Product create();
    }
    
    public class MacbookBuilder extends Builder {
    /**
     * 具体的Builder
     */
        private Product product = new MacBook();
    
        @Override
        public void buildBoard(String board) {
            product.setmBoard(board);
        }
    
        @Override
        public void buildDisplay(String display) {
            product.setmDisplay(display);
        }
    
        @Override
        public void buildOs() {
            product.setmOs();
        }
    
        @Override
        public Product create() {
            return product;
        }
    }
    
    public class Director {
    /**
     * Director 负责构建product
     */
        private Builder builder = null;
    
        public Director(Builder builder) {
            this.builder = builder;
        }
    
        public void construct(String board, String display) {
            builder.buildBoard(board);
            builder.buildDisplay(display);
            builder.buildOs();
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Builder builder = new MacbookBuilder();
            Director director = new Director(builder);
            director.construct("英特尔", "25寸");
            System.out.println(builder.create().toString());
    
        }
    }
    
    在实际的Android开发中,我们一般这样应用
     Person person = new Person.Builder().setName("test").......set.....build();
    
    
        public class Person {
        private String name;//用户不知道有哪些参数(参数太多)
    
        public Person(Builder builder) {
            name = builder.name;
        }
    
    
        public static class Builder {
    
            private String name;
    
            public Builder setName(String str) {
                name = str;
                return this;
            }
    
            public Person build() {
                return new Person(this);
            }
        }
    }
    

    AlertDialog源码分析

    相关文章

      网友评论

          本文标题:Builder模式

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