1. 策略模式介绍
1、根据它来避免 if-else 或 switch 分支判断,避免使用多重条件转移语句;
2、支持"开闭原则",可以在原有基础上选择行为方法,同时允许增加行为方法。
2. 策略模式结构类
① 接口或抽象类:自定义接口或抽象类,由多个实现类具体实现;
② 抽象方法:实现类中可以继承或重写它;
③ Bean实例:Spring提供注入ApplicationContext即应用上下文接口获取;通过不同BeanName(JDK提供的@Resource依赖注入原理)实现分支判断转移语句。
3.策略模式代码案例
一、首先定义枚举类,通过name绑定BeanName
java
public enum ServicesNameEnum {
CONSUMER_MEMBER("consumerMember", "consumerMemberService"),
CONSUMER_USERINFOS("consumerUserInfos", "consumerUserInfosService");
private final String name;
private final String beanName;
ServicesNameEnum(String name, String beanName) {
this.name = name;
this.beanName = beanName;
}
/**
* 通过name获取IOC中对象实例
*
* @param name
* @return
*/
public static String getBeanNameByName(String name) {
for (ServicesNameEnum servicesNameEnum : ServicesNameEnum.values()) {
if (servicesNameEnum.getName().equals(name)) {
return servicesNameEnum.getBeanName();
}
}
return name;
}
public String getName() {
return name;
}
public String getBeanName() {
return beanName;
}
}
二、定义接口
java
public interface IConsumerService {
List<UserInfo> findUserInfos(String authority);
List<Order> findOrders(List<Long> orderIds);
String insertMember(Member member) throws Exception;
void insertUserInfo(UserInfo userInfo);
void process(UserInfo userInfo);
}
三、第一个接口实现类
java
@Service
public class ConsumerMemberService implements IConsumerService {
@Override
public List<UserInfo> findUserInfos(String authority) {
System.out.println("ConsumerMemberService开始执行----------------------------");
return null;
}
@Override
public List<Order> findOrders(List<Long> orderIds) {
System.out.println("ConsumerMemberService开始执行----------------------------");
return null;
}
@Override
public String insertMember(Member member) throws Exception {
return null;
}
@Override
public void insertUserInfo(UserInfo userInfo) {
}
@Override
public void process(UserInfo userInfo) {
}
}
四、第二个接口实现类
javascript
@Service
public class ConsumerUserInfosService implements IConsumerService {
@Override
public List<UserInfo> findUserInfos(String authority) {
System.out.println("ConsumerUserInfosService开始执行----------------------------");
return null;
}
@Override
public List<Order> findOrders(List<Long> orderIds) {
System.out.println("ConsumerUserInfosService开始执行----------------------------");
return null;
}
@Override
public String insertMember(Member member) throws Exception {
return null;
}
@Override
public void insertUserInfo(UserInfo userInfo) {
}
@Override
public void process(UserInfo userInfo) {
}
}
五、依赖注入ApplicationContext容器对象,根据BeanName获取具体对象实例
java
@Autowired
private ApplicationContext applicationContext;
@RequestMapping(value = "/getInstanceByBeanName",method = RequestMethod.GET)
public void getInstanceByBeanName(String name) {
// 获取name
name = "consumerUserInfos";
// name绑定BeanName
String beanName = ServicesNameEnum.getBeanNameByName(name);
// ApplicationContext即应用上下文通过BeanName获取具体对象实例(JDK提供的@Resource原理)
// IConsumerService 为接口或抽象类并且多个实现类;最终实例对象由beanName决定
IConsumerService iConsumerService= (IConsumerService) applicationContext.getBean(beanName);
// 多态;最终调用实现类的重写方法
iConsumerService.findUserInfos(null);
}
六、控制台执行结果
javascript
ConsumerUserInfosService开始执行----------------------------