建造者模式

Posted yongjixl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了建造者模式相关的知识,希望对你有一定的参考价值。

隐藏复合对象的创建过程,把复合对象的创建过程抽象,通过子类的继承和重载的方式,动态创建对象.

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);
    
  
final String heroName = "Sir Lancelot";

    final Hero hero = new Hero.Builder(Profession.WARRIOR, heroName)
        .withArmor(Armor.CHAIN_MAIL)
        .withWeapon(Weapon.SWORD)
        .withHairType(HairType.LONG_CURLY)
        .withHairColor(HairColor.BLOND)
        .build();

 

以上是关于建造者模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式·建造者模式

设计模式之建造者模式(创建型)

《大话设计模式》——建造者模式

建造者模式(Builder Pattern)

设计模式—建造者模式(Builder)

什么是建造者模式