美文网首页Android设计模式
Android设计模式-2-建造者模式

Android设计模式-2-建造者模式

作者: 今阳说 | 来源:发表于2018-05-31 20:54 被阅读44次

    1. 定义:

    • 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
    • 在用户不知道对象的建造过程和细节的情况下,可以直接创建复杂的对象。

    2. 优缺点

    • 优点:隐藏复杂的实现细节,易于解耦,方便扩展
    • 缺点:当产品内部变化复杂,产品间差异较大时,不适合使用建造者模式

    3. Android源码中的体现

    • AlertDialog.Builder,StringBuilder, StringBuffer 等,这些都是我们日常开发中经常会用到的

    4. 实例演示

    • 下面结合一个实例来分析一下建造者模式的原理,以组装手机为例:
    1. 首先是定义一个手机的基类,包含一些手机的共有属性和方法
    abstract class MobilePhone {
            protected String cpu;  // 微处理器
            protected String ram; // 内存
            protected String os; // 系统
    
            protected MobilePhone() {
            }
    
            public abstract void setCPU(String cpu){
                this.cpu=cpu;
            }
    
            public void setRAM(String ram) {
                this.ram = ram;
            }
    
            public void setOS(String os) {
                this.os = os;
            }
    
            @Override
            public String toString() {
                return "MobilePhone{" +
                        "cpu='" + cpu + '\'' +
                        ", ram=" + ram +
                        ", os='" + os + '\'' +
                        '}';
            }
        }
    
    1. 创建具体的实现类,如华为手机,实现了基类的抽象方法,并扩展增加了AI智能芯片
    class HuaWeiPhone extends MobilePhone {
           //新增了一个AI智能芯片
          protected String aiCpu;
    
          public void setAiCpu(String aiCpu) {
              this.aiCpu = aiCpu;
          }
    
          @Override
          public String toString() {
              return "HuaWeiPhone{" +
                      "cpu='" + cpu + '\'' +
                      ", ram=" + ram +
                      ", os='" + os + '\'' +
                      ", aiCpu='" + aiCpu + '\'' +
                      '}';
          }
      }
    
    1. 创建一个建造者的基类,通过范型控制要构造的实例类型
    abstract class PhoneBuilder<T extends MobilePhone> {
            protected T phone;
    
            public abstract PhoneBuilder buildRAM(int ram);
    
            public abstract PhoneBuilder buildOS(String os);
    
            public abstract T build();
        }
    
    1. 创建华为手机的建造者
    class HuaWeiBuilder extends PhoneBuilder<HuaWeiPhone> {
            HuaWeiParam mHuaWeiParam;
    
            public HuaWeiBuilder() {
                mHuaWeiParam = new HuaWeiParam();
            }
    
            public HuaWeiBuilder buildAICPU(boolean isSupportAi) {
                if (isSupportAi)
                    mHuaWeiParam.aiCpu = "麒麟AI芯片";
                return this;
            }
    
            // 内存的build方法值提供内存容量的定制,而厂商品牌固定使用AMD的
            @Override
            public HuaWeiBuilder buildRAM(int ram) {
                mHuaWeiParam.ram = String.format("AMD-007 %dG", ram);
                return this;
            }
    
            @Override
            public HuaWeiBuilder buildOS(String os) {
                mHuaWeiParam.os = os;
                return this;
            }
    
            @Override
            public HuaWeiPhone build() {
                phone = new HuaWeiPhone();
                //我大华为有钱任性,cpu只用970,不提供build方法
                // 说明利用建造者模式可以自由的隐藏一些不想被外界改变的属性
                phone.setCPU("麟970"); 
                phone.setOS(mHuaWeiParam.os);
                phone.setRAM(mHuaWeiParam.ram);
                phone.setAiCpu(mHuaWeiParam.aiCpu);
                return phone;
            }
    
            class HuaWeiParam {
                String aiCpu;
                String os;
                String ram;
            }
        }
    
    1. 使用Builder构建手机实例,如下程序,构造不同型号的华为手机就非常方便了
    private void methodBuilderPattern() {
            HuaWeiPhone huaWeiP20 = new HuaWeiBuilder()
                    .buildOS("Android 8.0")
                    .buildRAM(6)
                    .buildAICPU(true)
                    .build();
            LjyLogUtil.i(huaWeiP20.toString());    
            HuaWeiPhone huaWeiMate30 = new HuaWeiBuilder()
                    .buildOS("红旗Linux")
                    .buildRAM(8)
                    .buildAICPU(false)
                    .build();
            LjyLogUtil.i(huaWeiMate10.toString());   
     }
    

    相关文章

      网友评论

        本文标题:Android设计模式-2-建造者模式

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