工厂模式+策略模式

输入实体

基类

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.mzph.cn/web/7146.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

嵌入式学习day11

每日面试题 全局变量可不可以定义在可被多个.c文件包含的头文件中&#xff0c;为啥&#xff1f; 可以&#xff0c;在不同的C文件中各自用static声明的全局变量&#xff0c;变量名可能相同&#xff0c;但是各自C文件中的全局变量的作用域为该文件&#xff0c;所以互不干扰。 …

渗透 如何防御ARP欺骗,LLMNR-MDNS-NBNS等协议的作用

一. 如何防御ARP欺骗&#xff1f; 1.使用双向IP/MAC绑定&#xff1b; 2.使用静态ARP缓存表&#xff1b; 3.使用ARP服务器&#xff0c;通过服务器来查找ARP转换表来响应其他机器的广播&#xff1b; 4.使用ARP欺骗防护软件&#xff1b; 5.在网关设备上部署防ARP欺骗攻击功能…

Python自动化测试【selenium面试题】

一、selenium中如何判断元素是否存在&#xff1f; expected_conditions模块提供了16种判断方法&#xff0c;以下方法是判断元素存在DOM中&#xff1a; presence_of_element_located """ An expectation for checking that an element is present on the DOM of…

分布式与一致性协议之ZAB协议(三)

ZAB协议 主节点崩溃了&#xff0c;怎么办&#xff1f; 众所周知&#xff0c;系统在运行中不可避免会出现各种各样的问题&#xff0c;比如进程崩溃了、服务器死机了&#xff0c;这些问题会导致很严重的后果&#xff0c;让系统没办法继续运行。在ZAB协议中&#xff0c;写请求是…

深度学习论文: DMAD: Dual Memory Bank for Real-World Anomaly Detection

深度学习论文: DMAD: Dual Memory Bank for Real-World Anomaly Detection DMAD: Dual Memory Bank for Real-World Anomaly Detection PDF: https://arxiv.org/pdf/2403.12362 PyTorch代码: https://github.com/shanglianlm0525/CvPytorch PyTorch代码: https://github.com/sh…

如何快速找到并处理jar依赖冲突问题

处理jar文件的依赖冲突一般有以下几种方法&#xff1a; 1、使用Maven的dependency:tree命令 Maven的dependency:tree命令可以帮助你快速找到项目的所有依赖的树状图。你可以在命令行运行以下命令&#xff1a; mvn dependency:tree你也可以使用-Dincludes参数来过滤某些特定的…

数智先锋 | 多场景数据治理案例,释放数据要素生产力

数据作为第五大生产要素&#xff0c;成为释放新质生产力的关键基础。 当前各个行业数字化建设如火如荼&#xff0c;全力挖掘数据价值以驱动行业高质量应用发展。数据治理成为数据要素价值发挥的重要基础和前提。 数据治理不单是技术问题&#xff0c;不是依赖工具就能解决的&a…

JVM之内存分配的详细解析

内存分配 两种方式 不分配内存的对象无法进行其他操作&#xff0c;JVM 为对象分配内存的过程&#xff1a;首先计算对象占用空间大小&#xff0c;接着在堆中划分一块内存给新对象 如果内存规整&#xff0c;使用指针碰撞&#xff08;Bump The Pointer&#xff09;。所有用过的内…

首屏优化-图片懒加载的代码实践(可直接运行)

实现图片懒加载时&#xff0c;可以使用 JavaScript 监听滚动事件&#xff0c;并根据图片是否进入可视区域来延迟加载图片。 方案一 <!DOCTYPE html> <html><head><style>.placeholder {width: 300px;height: 200px;background-color: #ccc;}.image {…

一文搞定文件的读写操作

文件的操作 C C语言中&#xff0c;文件的读取操作流程如下&#xff1a; 定义一个文件的指针将指针指向需要打开的文件&#xff0c;并且赋予该指针权限&#xff08;读&#xff0c;写&#xff0c;追加&#xff09;然后开始向文件写/读数据 打开文件 定义文件指针&#xff1a;F…

Android面试必考题1-15

目录 Service 和 主进程区别 一、性能优化 2.什么情况下会导致内存泄露 3.如何避免 OOM 异常

链式队列基本操作

链式队列的基本概念 链式队列是一种常见的数据结构&#xff0c;它使用链表作为其底层数据存储结构。链式队列的特点是动态的内存分配&#xff0c;可以有效地处理队列的入队和出队操作。下面&#xff0c;我将介绍链式队列的实现方法&#xff0c;并提供相应的C语言代码示例。 链…

C语言-设置控制台信息

Win_API Win_API是Windows应用程序接口&#xff08;Windows Application Programming Interface&#xff09;的缩写&#xff0c;它是一组函数、系统服务和程序接口&#xff0c;允许开发者在微软Windows操作系统上创建应用程序。Win32 API 是Windows API的一个主要部分&#xff…

Linux PXE高效批量网络装机

系统初始化 systemctl disable --now firewalld.service setenforce 0 vim /etc/selinux/config 安装软件 yum install -y tftp-server xinetd dhcp vsftpd syslinux 复制 vmlinuz initrd.img pxelinux.0 到 /var/lib/tftpboot/ 目录 [rootlocalhost ~]# cd /mnt/…

设计模式之业务代表模式

在编程江湖的风雨中漂泊多年&#xff0c;每当我遇到那些错综复杂的业务逻辑和系统交互&#xff0c;总有一个模式像一位忠诚的骑士&#xff0c;默默守护着我的代码城堡&#xff0c;那就是——业务代表模式&#xff08;Business Delegate Pattern&#xff09;。它不是最耀眼的明星…

C++ 如何进阶?

一、C基础&#xff08;3个月&#xff09; 1、面向对象的三大特性&#xff1a;封装、继承、多态 2、类的访问权限&#xff1a;private、protected、public 3、类的构造函数、析构函数、赋值函数、拷贝函数 4、移动构造函数与接贝构造函数对比 5、深接贝与浅贝的区别 6、空…

【C++语言】类和对象--默认成员函数 (中)

文章目录 前言类的六个默认成员函数&#xff1a;1. 构造函数概念特性做了什么&#xff1f;易错注意&#xff1a;显式定义和默认构造函数 2. 析构函数概念特征做了什么?注意事项&#xff1a; 3.拷贝构造函数概念特征做了什么&#xff1f;注意事项&#xff1a; 4.赋值运算符重载…

MySQL基础_4.排序与分页

文章目录 一、排序二、分页 一、排序 使用 ORDER BY 子句排序&#xff1a; ASC&#xff08;ascend&#xff09;: 升序DESC&#xff08;descend&#xff09;:降序 二、分页 MySQL中使用 LIMIT 实现分页&#xff0c;格式&#xff1a; LIMIT [位置偏移量,] 行数第一个“位置偏…

Linux程序库文件调试测试方法

Linux编译后的.so文件是需要进行上机测试的&#xff0c;对于已经量产的硬件平台来说一般是通过具有相同功能的样机测试新版本的功能&#xff0c;具体如下。 Linux的量产固件由于已经经过裁剪系统内部的usr lib etc等目录是只读的权限不可以修改因此不能将测试库直接放到其下进行…

JavaScript异步编程——01-单线程和异步任务

单线程 JS 是单线程的 JavaScript 语言的执行是单线程的。即同一时间&#xff0c;只能处理一个任务。 具体来说&#xff0c;所谓单线程&#xff0c;是指 JS 引擎中负责解释和执行 JavaScript 代码的线程只有一个&#xff0c;即同一时间&#xff0c;只能处理一个任务。这个任…