Android设计模式

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

2018-05-31  本文已影响44人  今阳说

1. 定义:

2. 优缺点

3. Android源码中的体现

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());   
 }
上一篇下一篇

猜你喜欢

热点阅读