设计模式-工厂方法模式

目的:

为创建一个对象定义一个接口,但是让子类决定实例化哪个类。工厂方法允许类将实例化延迟到子类。

程序示例:

铁匠生产武器。精灵需要精灵武器,而兽人需要兽人武器。根据客户来召唤正确类型的铁匠。

1.创建武器接口和类型

public interface Weapon{

    WeaponType getWeaponType();
}
//类型
@RequiredArgsConstructor
public enum WeaponType {

  SHORT_SWORD("short sword"),
  SPEAR("spear"),
  AXE("axe"),
  UNDEFINED("");

  private final String title;

  @Override
  public String toString() {
    return title;
  }
}

2.武器实现

@RequiredArgsConstructor
@Getter
public class ElfWeapon implements Weapon {

  private final WeaponType weaponType;

  @Override
  public String toString() {
    return "an elven " + weaponType;
  }
}

@RequiredArgsConstructor
@Getter
public class OrcWeapon implements Weapon {

  private final WeaponType weaponType;

  @Override
  public String toString() {
    return "an orcish " + weaponType;
  }
}

3.生产者接口及其子类(工厂方法的精髓)

public interface Blacksmith{
    Weapon manufactureWeapon(WeaponType type);
}

public class ElfBlacksmith implements Blacksmith{

private static final Map<WeaponType,ElfWeapon> ELFARSENAl;
static {
ELFARSENAl=new EnumMap<WeaponType,ElfWeapon>(WeaponType.class);
    Arrays.stream(WeaponType.values()).forEach(type -> ELFARSENAl.put(type,new ElfWeapon(type)));
}

    @Override
    public Weapon manufactureWeapon(WeaponType type) {
        return ELFARSENAl.get(type);
    }

    @Override
    public String toString() {
        return "The elf blacksmith";
    }
}

public class OrcBlacksmith implements Blacksmith{


    private static final Map<WeaponType,OrcWeapon> ORCARSENAL;
    static {
        ORCARSENAL=new EnumMap<WeaponType,OrcWeapon>(WeaponType.class);
        Arrays.stream(WeaponType.values()).forEach(type -> ORCARSENAL.put(type,new OrcWeapon(type)));

    }
    @Override
    public Weapon manufactureWeapon(WeaponType type) {
        return ORCARSENAL.get(type);
    }

    @Override
    public String toString() {
        return "The orc Blacksmith";
    }
}

测试输出

        OrcBlacksmith orcBlacksmith = new OrcBlacksmith();
        Weapon orcWeapon = orcBlacksmith.manufactureWeapon(WeaponType.SPEAR);
        log.info(MANUFACTURED, orcBlacksmith, orcWeapon);
        orcWeapon = orcBlacksmith.manufactureWeapon(WeaponType.AXE);
        log.info(MANUFACTURED, orcBlacksmith, orcWeapon);

        /*
        The orc Blacksmith manufactured an orcish spear
        The orc Blacksmith manufactured an orcish axe
         */


        ElfBlacksmith elfBlacksmith = new ElfBlacksmith();
        Weapon weapon = elfBlacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
        log.info(MANUFACTURED,  elfBlacksmith,weapon);
        weapon=elfBlacksmith.manufactureWeapon(WeaponType.AXE);
        log.info(MANUFACTURED,  elfBlacksmith,weapon);

        /*
        The elf blacksmith manufactured an elven short_sword
        The elf blacksmith manufactured an elven axe
         */

类图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值