输入实体
基类
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 {
@Override
public Object query(PersonInputDto inputDto) {
if (inputDto instanceof ManPerson) {
ManPerson manPerson = (ManPerson) inputDto;
manPerson.setId(1);
manPerson.setSex("男");
manPerson.setName("name1");
return manPerson;
}
return null;
}
@Override
public Integer getType() {
return 1;
}
}
@Component
public class WomenHandle implements Person {
@Override
public Object query(PersonInputDto inputDto) {
if (inputDto instanceof WomenPerson) {
WomenPerson womenPerson = (WomenPerson) inputDto;
womenPerson.setId(2);
womenPerson.setSex("女");
womenPerson.setName("name2");
return womenPerson;
}
return null;
}
@Override
public 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);
}
@Override
public 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 {
@Resource
private List<Person> personList;
private static final Map<Integer, Person> handlerMap = new HashMap<>();
public Person getAccountModel(Integer type) {
return handlerMap.get(type);
}
@PostConstruct
public void init() {
personList.forEach(value -> handlerMap.put(value.getType(), value));
System.out.println("handlerMap ===> " + JSON.toJSONString(handlerMap));
}
}
服务调用
@RestController
@RequestMapping({"/", ""})
public class DemoController {
@Resource
private MainTestFactory factory;
@Resource
private 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);
}
}