代码设计模式

文章目录

概要

工厂模式和模板模式

其实目前工厂模式和模板模式一直搞得不太清楚, 粗略写下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());

相关推荐
小bo波7 小时前
枚举实战
java·设计模式·枚举·后端开发·代码重构
不好听61310 小时前
Prompt 驱动 NLP:用大语言模型重新定义自然语言处理开发范式
设计模式·node.js·nlp
天文家13 小时前
深入理解装饰器与适配器:从设计模式到 Spring AOP 的工程实践
java·设计模式
workflower14 小时前
医院核心竞争力的四大重构
人工智能·安全·设计模式·重构·动态规划·scrum
折哥的程序人生 · 物流技术专研18 小时前
【电商多平台电子面单对接实战|第二篇】抖音代发电子面单对接:从“面条代码”到整洁架构的涅槃之路
设计模式·架构·系统架构·单元测试·代码规范·单一职责原则
葫芦和十三19 小时前
范式之变|Agent 设计,换语言了
人工智能·设计模式
ourenjiang19 小时前
【学习设计模式】原型模式
学习·设计模式·原型模式
贵慜_Derek19 小时前
《从零实现 Agent 系统》连载 20|MCP 与 Code Execution:协议、档位与 Sidecar
人工智能·设计模式·架构
Sam_Deep_Thinking2 天前
结算分摊的策略模式:不同营销活动的扣点计算方案
java·设计模式·架构·系统架构
故渊at2 天前
系列一:架构思想进阶 | 第3篇 SOLID 原则与设计模式实战:从“代码搬运工”到“架构师”的必经之路
观察者模式·设计模式·重构·架构·代理模式