当前位置: 首页 > news >正文

工厂模式+策略模式

输入实体

基类

import lombok.Data;@Data
public class PersonInputDto {private Integer id;private String name;
}

子类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ManPerson extends PersonInputDto {private String sex;
}@Data
@AllArgsConstructor
@NoArgsConstructor
public class WomenPerson extends PersonInputDto {private String sex;
}

创建抽象接口

接口

public interface Person {Object query(PersonInputDto inputDto);Integer getType();
}

抽象具体实现

@Component
public class ManHandle implements Person {@Overridepublic Object query(PersonInputDto inputDto) {if (inputDto instanceof ManPerson) {ManPerson manPerson = (ManPerson) inputDto;manPerson.setId(1);manPerson.setSex("男");manPerson.setName("name1");return manPerson;}return null;}@Overridepublic Integer getType() {return 1;}
}
@Component
public class WomenHandle implements Person {@Overridepublic Object query(PersonInputDto inputDto) {if (inputDto instanceof WomenPerson) {WomenPerson womenPerson = (WomenPerson) inputDto;womenPerson.setId(2);womenPerson.setSex("女");womenPerson.setName("name2");return womenPerson;}return null;}@Overridepublic Integer getType() {return 2;}
}

两种方式创建工厂

创建工厂1

import com.alibaba.fastjson.JSON;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;import java.util.Collection;
import java.util.HashMap;
import java.util.Map;@Component
public class MainTestFactory implements ApplicationListener<ApplicationReadyEvent> {private static final Map<Integer, Person> handlerMap = new HashMap<>();public Person getAccountModel(Integer type) {return handlerMap.get(type);}@Overridepublic void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {Collection<Person> values = applicationReadyEvent.getApplicationContext().getBeansOfType(Person.class).values();values.forEach(value -> handlerMap.put(value.getType(), value));System.out.println("handlerMap ===> " + JSON.toJSONString(handlerMap));}
}

创建工厂2

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Component
public class MainTestFactory2 {@Resourceprivate List<Person> personList;private static final Map<Integer, Person> handlerMap = new HashMap<>();public Person getAccountModel(Integer type) {return handlerMap.get(type);}@PostConstructpublic void init() {personList.forEach(value -> handlerMap.put(value.getType(), value));System.out.println("handlerMap ===> " + JSON.toJSONString(handlerMap));}
}

服务调用

@RestController
@RequestMapping({"/", ""})
public class DemoController {@Resourceprivate MainTestFactory factory;@Resourceprivate MainTestFactory2 mainTestFactory2;@PostMapping("/test")@ResponseBody@ApiOperation("test")public String test() {ManPerson query = (ManPerson) factory.getAccountModel(1).query(new ManPerson());System.out.println(JSON.toJSONString(query));ManPerson query1 = (ManPerson) factory.getAccountModel(2).query(new ManPerson());System.out.println(query1);WomenPerson query2 = (WomenPerson) mainTestFactory2.getAccountModel(2).query(new WomenPerson());return JSON.toJSONString(query2);}}
http://www.lryc.cn/news/346349.html

相关文章:

  • TMS320F28335学习笔记-时钟系统
  • 【Apache POI】Apache POI-操作Excel表格-简易版
  • MySQL系列之索引
  • 【问题分析】锁屏界面调起google语音助手后壁纸不可见【Android 14】
  • Java入门基础学习笔记8——注释
  • 上班工资太低了,哪些副业可以多赚钱?
  • 原子学习笔记4——GPIO 应用编程
  • 查看iqn编码
  • 如何安全的使用密码登录账号(在不知道密码的情况下)
  • 软件需求和设计评审
  • 论文笔记ColdDTA:利用数据增强和基于注意力的特征融合进行药物靶标结合亲和力预测
  • 如何防止WordPress网站内容被抓取
  • 全球化战略中的技术支柱:出海企业的网络技术解决方案
  • 在Linux上安装并运行RabbitMQ
  • 使用 docker-compose 搭建个人博客 Halo
  • 《这就是ChatGPT》读书笔记
  • 更专业的汽车软件研发工具链,怿星重磅发布新产品
  • Stable Diffusion:AI绘画的新纪元
  • 有5个excel表,每个表有6列。用python把这5个表合成1个表。
  • 【回溯算法】【Python实现】最大团问题
  • CMakeLists.txt语法规则:foreach 循环基本用法
  • redis集群-主从机连接过程
  • 去哪里找高清视频素材?推荐几个短视频素材免费网站
  • 从互联网医院源码到搭建:开发视频问诊小程序的技术解析
  • 【Linux】常见指令(二)
  • python元类与C#、Java中的反射
  • Echart.js绘制时间线并绑定事件
  • Flutter弹窗链-顺序弹出对话框
  • 1290.二进制链表转整数
  • P8803 [蓝桥杯 2022 国 B] 费用报销