代码设计模式

文章目录

概要

工厂模式和模板模式

其实目前工厂模式和模板模式一直搞得不太清楚, 粗略写下demo示例
就是通过一个入口可以分流去不同方式实现

demo示例

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    // 获取某一接口的所有实现类,并通过枚举完成策略模式
    Map<String, TaskService> map = applicationContext.getBeansOfType(TaskService.class);
    taskServiceMap = new HashMap<>();
    map.forEach((key, value) -> {
        for (ContentType contentType : value.getType()) {
            taskServiceMap.put(contentType, value);
        }
    });
}

/**
 * 获取检测服务
 *
 * @param type type
 * @return task service
 */
public TaskService getTaskService(ContentType type) {
    return taskServiceMap.get(type);
}

以上基于的是spring提供的applicationContext.getBeansOfType方法, 他会把全部的重写了某个接口全部的类返回回来

下面另外一种示例, 通过枚举获得类对象
通过Class<? extends IExtractAnimal> extractorClazz; 这种方法保存类对象, 需要的时候通过newInstacnce 创建对象出来

public enum AnimalInfoType {
DOG(“.dog”, “this is a dog”, Dog.class),
CAT(“.pptx”, “this is a cat”, Cat.class),
PIG(“.docx”, “this is a pig”, Pig.class),

/**
 * 重量
 */
private String weight;

/**
 * 颜色
 */
private String color;

/**
 * 动物抽取器
 */
private Class<? extends IExtractAnimal> extractorClazz;

FileInfoType(String weight, String color, Class<? extends IExtractAnimal> extractorClazz) {
    this.weight = weight;
    this.color = color;
    this.extractorClazz = extractorClazz;
}

public Class<? extends IExtractAnimal> getExtractorClazz() {
    return extractorClazz;
}

/**
 * 获取动物类型
 *
 * @param filePath fileSuffix
 * @return DocumentType
 */
public static Optional<AnimalInfoType> getFileInfoBySuffix(String weight) {
    for (FileInfoType type : AnimalInfoType.values()) {
        if (weight.endsWith(String.valueOf(type.weight))) {
            return Optional.of(type);
        }
    }
    return Optional.empty();
}

}

public class AnimalFactory {

static List<String> supportTypeList = Arrays.asList("dog", "cat", "pig");


public static IExtractFileInfo getInstance(AnimalType animalType) {
    if (animalType == null) {
        return null;
    }       
        return infoType.getExtractorClazz().newInstance();      
}

}

Optional animalInfoType = AnimalInfoType.getFileInfoBySuffix(currentResult.getRealPath());
if (!animalInfoType.isPresent()) {
return;
}
// 根据工厂类获得对应的动物类
IExtractAnimal extractAnimal = AnimalFactory.getInstance(animalInfoType.get());

相关推荐

  1. 代码设计模式

    2024-01-06 08:50:05       61 阅读
  2. 设计模式代码

    2024-01-06 08:50:05       27 阅读
  3. 设计模式——原型模式代码示例

    2024-01-06 08:50:05       52 阅读
  4. C++设计模式代码--单例模式

    2024-01-06 08:50:05       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-06 08:50:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-06 08:50:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-06 08:50:05       87 阅读
  4. Python语言-面向对象

    2024-01-06 08:50:05       96 阅读

热门阅读

  1. 设计模式之单例模式

    2024-01-06 08:50:05       63 阅读
  2. Hive 源码

    2024-01-06 08:50:05       54 阅读
  3. 搭建网站环境(IIS+php+mysql)

    2024-01-06 08:50:05       52 阅读
  4. Websocket实时更新商品信息

    2024-01-06 08:50:05       50 阅读
  5. Spring Boot和Spring主要区别:

    2024-01-06 08:50:05       60 阅读
  6. k8s-二进制部署

    2024-01-06 08:50:05       51 阅读
  7. Ubuntu20.04安装suiteCRM

    2024-01-06 08:50:05       56 阅读
  8. 使用conda管理Python虚拟环境

    2024-01-06 08:50:05       61 阅读
  9. conda

    conda

    2024-01-06 08:50:05      58 阅读
  10. conda创建、查看、删除虚拟环境

    2024-01-06 08:50:05       57 阅读
  11. 从0开始的编程生活

    2024-01-06 08:50:05       54 阅读
  12. Sentinel 使用

    2024-01-06 08:50:05       52 阅读
  13. EasyExcel的追加写入(新增POI、CSV)

    2024-01-06 08:50:05       56 阅读