设计模式篇之创建型模式

目录


前言

最近开始整理Java设计模式,本篇主要分享设计模式中的创建型模式,并给出demo代码,适合初中级开发学习。分享书籍《大话设计模式》,分享GitHub学习设计模式仓库


一、简单工厂模式

java 复制代码
// 产品接口
interface Product {
    void operate();
}

// 具体产品类A
class ConcreteProductA implements Product {
    @Override
    public void operate() {
        System.out.println("Product A operation");
    }
}

// 具体产品类B
class ConcreteProductB implements Product {
    @Override
    public void operate() {
        System.out.println("Product B operation");
    }
}

// 简单工厂类
class SimpleFactory {
    public static Product createProduct(String type) {
        if (type.equals("A")) {
            return new ConcreteProductA();
        } else if (type.equals("B")) {
            return new ConcreteProductB();
        } else {
            throw new IllegalArgumentException("Invalid product type.");
        }
    }
}

// 客户端代码
public class Main {
    public static void main(String[] args) {
        Product productA = SimpleFactory.createProduct("A");
        productA.operate();  // Output: Product A operation

        Product productB = SimpleFactory.createProduct("B");
        productB.operate();  // Output: Product B operation
    }
}

二、工厂方法模式

不同工匠打造不同的武器

java 复制代码
public interface Blacksmith {
  Weapon manufactureWeapon(WeaponType weaponType);
}

public class OrcBlacksmith implements Blacksmith {

  private static final Map<WeaponType, OrcWeapon> ORCARSENAL;

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

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

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

public class ElfBlacksmith implements Blacksmith {

  private static final Map<WeaponType, ElfWeapon> ELFARSENAL;

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

  @Override
  public Weapon manufactureWeapon(WeaponType weaponType) {
    return ELFARSENAL.get(weaponType);
  }

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



@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;
  }
}


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;
  }
}


@Slf4j
public class App {

  private static final String MANUFACTURED = "{} manufactured {}";

  /**
   * Program entry point.
   * @param args command line args
   */
  public static void main(String[] args) {

    Blacksmith blacksmith = new OrcBlacksmith();
    Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
    LOGGER.info(MANUFACTURED, blacksmith, weapon);
    weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
    LOGGER.info(MANUFACTURED, blacksmith, weapon);

    blacksmith = new ElfBlacksmith();
    weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
    LOGGER.info(MANUFACTURED, blacksmith, weapon);
    weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
    LOGGER.info(MANUFACTURED, blacksmith, weapon);
  }
}

总结

未完待续,明天继续。。。。。


---- 永不磨灭的番号:我是AK

相关推荐
架构师沉默1 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
Java中文社群3 小时前
重要:Java25正式发布(长期支持版)!
java·后端·面试
每天进步一点_JL4 小时前
JVM 类加载:双亲委派机制
java·后端
用户298698530144 小时前
Java HTML 转 Word 完整指南
java·后端
渣哥4 小时前
原来公平锁和非公平锁差别这么大
java
渣哥5 小时前
99% 的人没搞懂:Semaphore 到底是干啥的?
java
J2K5 小时前
JDK都25了,你还没用过ZGC?那真得补补课了
java·jvm·后端
kfyty7255 小时前
不依赖第三方,不销毁重建,loveqq 框架如何原生实现动态线程池?
java·架构
isysc16 小时前
面了一个校招生,竟然说我是老古董
java·后端·面试
幂简集成explinks6 小时前
e签宝签署API更新实战:新增 signType 与 FDA 合规参数配置
后端·设计模式·开源