设计模式篇之创建型模式

目录


前言

最近开始整理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

相关推荐
小叶学C++4 分钟前
【C++】类与对象(下)
java·开发语言·c++
ac-er88885 分钟前
PHP“===”的意义
开发语言·php
2401_854391088 分钟前
高效开发:SpringBoot网上租赁系统实现细节
java·spring boot·后端
Cikiss16 分钟前
微服务实战——SpringCache 整合 Redis
java·redis·后端·微服务
wxin_VXbishe17 分钟前
springboot合肥师范学院实习实训管理系统-计算机毕业设计源码31290
java·spring boot·python·spring·servlet·django·php
Cikiss18 分钟前
微服务实战——平台属性
java·数据库·后端·微服务
jk_10124 分钟前
MATLAB中decomposition函数用法
开发语言·算法·matlab
weixin_4640780724 分钟前
C#串口温度读取
开发语言·c#
无敌の星仔27 分钟前
一个月学会Java 第2天 认识类与对象
java·开发语言
OEC小胖胖31 分钟前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web