美文网首页
设计模式6:建造者模式

设计模式6:建造者模式

作者: akak18183 | 来源:发表于2017-05-07 11:27 被阅读0次

    但凡用过若干安卓第三方库的人,都应该对Builder DP(Design Pattern)也就是建造者模式不会感到陌生,例如Okhttp。
    在我看来,建造者模式就是使用链式赋值,比一个个参数地set省力得多。

    代码:
    现在我们需要建造一个英雄(Hero),有职业(Profession)、名字(Name)、发型(HairType)、发色(HairColor)、护甲(Armor)、武器(Weapon)等属性。
    假如不用建造者模式的话,我们怎么创建一个英雄实例呢?自然只能一个参数一个参数地使用set方法。
    使用builder,单行即可:

    /**
     * 
     * Hero, the class with many parameters.
     * 
     */
    public final class Hero {
    
      private final Profession profession;
      private final String name;
      private final HairType hairType;
      private final HairColor hairColor;
      private final Armor armor;
      private final Weapon weapon;
    
      private Hero(Builder builder) {
        this.profession = builder.profession;
        this.name = builder.name;
        this.hairColor = builder.hairColor;
        this.hairType = builder.hairType;
        this.weapon = builder.weapon;
        this.armor = builder.armor;
      }
    
      public Profession getProfession() {
        return profession;
      }
    
      public String getName() {
        return name;
      }
    
      public HairType getHairType() {
        return hairType;
      }
    
      public HairColor getHairColor() {
        return hairColor;
      }
    
      public Armor getArmor() {
        return armor;
      }
    
      public Weapon getWeapon() {
        return weapon;
      }
    
      @Override
      public String toString() {
    
        StringBuilder sb = new StringBuilder();
        sb.append("This is a ")
                .append(profession)
                .append(" named ")
                .append(name);
        if (hairColor != null || hairType != null) {
          sb.append(" with ");
          if (hairColor != null) {
            sb.append(hairColor).append(' ');
          }
          if (hairType != null) {
            sb.append(hairType).append(' ');
          }
          sb.append(hairType != HairType.BALD ? "hair" : "head");
        }
        if (armor != null) {
          sb.append(" wearing ").append(armor);
        }
        if (weapon != null) {
          sb.append(" and wielding a ").append(weapon);
        }
        sb.append('.');
        return sb.toString();
      }
    
      /**
       * 
       * The builder class.
       * 
       */
      public static class Builder {
    
        private final Profession profession;
        private final String name;
        private HairType hairType;
        private HairColor hairColor;
        private Armor armor;
        private Weapon weapon;
    
        /**
         * Constructor
         */
        public Builder(Profession profession, String name) {
          if (profession == null || name == null) {
            throw new IllegalArgumentException("profession and name can not be null");
          }
          this.profession = profession;
          this.name = name;
        }
    
        public Builder withHairType(HairType hairType) {
          this.hairType = hairType;
          return this;
        }
    
        public Builder withHairColor(HairColor hairColor) {
          this.hairColor = hairColor;
          return this;
        }
    
        public Builder withArmor(Armor armor) {
          this.armor = armor;
          return this;
        }
    
        public Builder withWeapon(Weapon weapon) {
          this.weapon = weapon;
          return this;
        }
    
        public Hero build() {
          return new Hero(this);
        }
      }
    }
    

    可以看到,set方法都不见了,取而代之的是一个builder类。一般而言,builder类就像是建造者模式的一个标志,假如不是用建造者模式,还是别取这个名字,以免误导。反而言之,建造者模式使用builder作为建造类就是理所当然的事情了,约定俗成。

    相关文章

      网友评论

          本文标题:设计模式6:建造者模式

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