设计模式篇之创建型模式

目录


前言

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

相关推荐
瑞士卷@几秒前
MyBatis入门到精通(Mybatis学习笔记)
java·数据库·后端·mybatis
Code_Geo9 分钟前
agent设计模式:第一章节—提示链
microsoft·设计模式·agent·模型
梵得儿SHI10 分钟前
Java 反射机制深度剖析:性能与安全性的那些坑
java·开发语言·安全·反射·动态代理·性能·反射机制
fsnine16 分钟前
Python图形化界面——pyqt5教程
开发语言·python·qt
虫小宝16 分钟前
Java分布式架构下的电商返利APP技术选型与架构设计实践
java·分布式·架构
007php00720 分钟前
百度面试题解析:Zookeeper、ArrayList、生产者消费者模型及多线程(二)
java·分布式·zookeeper·云原生·职场和发展·eureka·java-zookeeper
嵌入式-老费24 分钟前
Easyx图形库应用(和lua结合使用)
开发语言·lua
AsiaLYF25 分钟前
kotlin中MutableStateFlow和MutableSharedFlow的区别是什么?
android·开发语言·kotlin
Asuncion00736 分钟前
Docker核心揭秘:轻量级虚拟化的革命
服务器·开发语言·docker·云原生
4Forsee1 小时前
【Android】浅析 Android 的 IPC 跨进程通信机制
android·java