不说真实业务场景的设计模式就是耍流氓
-
真实场景一:(直播)腾讯云直播api文档中,只支持填写一个接口回调地址,需实现多个回调场景逻辑
-
真实场景二:(社交)Feed流产品,Title会有多个频道,类似关注、推荐、汽车、热门等
-
真实场景三:(支付)APP支持微信、支付宝、招商银行等支付方式
以上三个案例很容易看出来,采用if-else一招解决,快准狠,但是存在一些弊端,每新增一种支付方式、新的频道、新的事件都需要修改核心逻辑,添加else if去实现功能,接下来讲讲我日常开发中最常用的几种解决方案,从简到难,大家自行选择。
下面代码以真实场景二做例子
传统方案一(if + else):
/**
* 内容列表服务
* @author xiaohao
* @since 2023/2/1 15:25
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class ContentListServiceV1 {
private final ContentCenter contentCenter;
/**
* 获取内容列表
* @author xiaohao
* @since 2023/2/1 15:26
*/
public List<Map<String, String>> getContentList(String type) {
List<Map<String, String>> responseList = new ArrayList<>(16);
if (StringUtils.equals("focus", type)) {
responseList = contentCenter.focusList(type);
} else if (StringUtils.equals("recommend", type)) {
responseList = contentCenter.recommendList(type);
} else if (StringUtils.equals("car", type)) {
responseList = contentCenter.carList(type);
} else {
responseList = contentCenter.randomList(type);
}
return responseList;
}
}
暴露接口
/**
* 内容中心
* @author xiaohao
* @since 2023/2/1 15:32
*/
@Service
public class ContentCenter {
public List<Map<String, String>> focusList(String type) {
// 伪代码
Map<String, String> map = new HashMap<>();
map.put("focus", "关注V1");
List<Map<String, String>> list = new ArrayList<>();
list.add(map);
return list;
}
public List<Map<String, String>> recommendList(String type) {
// 伪代码
return new ArrayList<>();
}
public List<Map<String, String>> carList(String type) {
// 伪代码
return new ArrayList<>();
}
public List<Map<String, String>> randomList(String type) {
// 伪代码
return new ArrayList<>();
}
}
方案二(工厂+策略+枚举):
定义接口
/**
* @description 内容列表
* @author xiaohao
* @since 2019/1/20 17:48 下午
*/
public interface ContentListV2 {
/**
* 获取内容列表
* @param type 频道
* @return 内容列表
* @author xiaohao
* @since 2019/1/20 18:36 下午
*/
List<Map<String, String>> getContentList(String type);
}
定义枚举
/**
* 内容列表枚举类
* @author xiaohao
* @since 2019/1/17 11:06 上午
*/
@Getter
public enum ContentListEnum {
/**
*定义枚举
*/
FOCUS("focus", "关注列表"),
RECOMMEND("recommend", "推荐列表"),
CAR("car", "车主列表"),
DEFAULT("default","默认");
private final String code;
private final String name;
ContentListEnum(String code, String name) {
this.code = code;
this.name = name;
}
public static ContentListEnum getEnumByCode(String code) {
Optional<ContentListEnum> first = Arrays.stream(ContentListEnum.values()).filter(p -> StringUtils.equals(p.getCode(), code)).findFirst();
return first.orElse(ContentListEnum.DEFAULT);
}
}
定义工厂
/**
* @description 内容工厂
* @author xiaohao
* @since 2019/1/20 17:41 下午
* @version 1.0
*/
@Slf4j
@Component
public class ContentFactoryV2 {
private static final Map<ContentListEnum, ContentListV2> MAP = new ConcurrentHashMap<>();
/**
* 默认事件类
*/
private static final ContentListV2 DEFAULT= new RandomListV2ServiceImpl();
// 版本升级或新增模块、插件(添加枚举,定好具体实现类)
static {
MAP.put(ContentListEnum.FOCUS, new FocusListV2ServiceImpl());
MAP.put(ContentListEnum.RECOMMEND, new RecommendListV2ServiceImpl());
MAP.put(ContentListEnum.CAR, new CarListV2ServiceImpl());
MAP.put(ContentListEnum.DEFAULT, DEFAULT);
}
public static List<Map<String, String>> contentListEvent(String type) {
ContentListV2 contentList = MAP.get(ContentListEnum.getEnumByCode(type));
return contentList == null ? DEFAULT.getContentList(type) : contentList.getContentList(type);
}
}
暴露接口
/**
* 内容列表服务
* @author xiaohao
* @since 2023/2/1 15:25
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class ContentListServiceV2 {
/**
* 获取内容列表
* @author xiaohao
* @since 2023/2/1 15:26
*/
public List<Map<String, String>> getContentList(String type) {
// 通过工厂,传入type实现
return ContentFactoryV2.contentListEvent(type);
}
}
最终实现类
/**
* 关注
* @author xiaohao
* @since 2023/2/1 15:51
*/
@Service
public class FocusListV2ServiceImpl implements ContentListV2 {
@Override
public List<Map<String, String>> getContentList(String type) {
Map<String, String> map = new HashMap<>();
map.put("focus", "关注V2");
List<Map<String, String>> list = new ArrayList<>();
list.add(map);
return list;
}
}
方案三(工厂+策略+枚举+ApplicationContextAware):
定义接口
/**
* @description 内容列表
* @author xiaohao
* @since 2019/1/20 17:48 下午
*/
public interface ContentListV3 {
/**
* 获取内容列表
* @param type 频道
* @return 内容列表
* @author xiaohao
* @since 2019/1/20 18:36 下午
*/
List<Map<String, String>> getContentList(String type);
/**
* 钩子接口,获取策略标识,用于绑定实现类的事件,对工厂类修改关闭,在业务实现类进行事件绑定
* @return 时间绑定
*/
ContentListV3Enum getSource();
}
定义枚举
/**
* 内容列表枚举类
* @author xiaohao
* @since 2019/1/17 11:06 上午
*/
@Getter
public enum ContentListV3Enum {
/**
*定义枚举
*/
FOCUS("focus", "关注列表"),
RECOMMEND("recommend", "推荐列表"),
CAR("car", "车主列表"),
DEFAULT("default","默认");
private final String code;
private final String name;
ContentListV3Enum(String code, String name) {
this.code = code;
this.name = name;
}
public static ContentListV3Enum getEnumByCode(String code) {
Optional<ContentListV3Enum> first = Arrays.stream(ContentListV3Enum.values()).filter(p -> StringUtils.equals(p.getCode(), code)).findFirst();
return first.orElse(ContentListV3Enum.DEFAULT);
}
}
定义工厂
/**
* @description 内容工厂V3
* @author xiaohao
* @since 2019/1/20 17:41 下午
* @version 1.0
*/
@Slf4j
@Component
public class ContentFactoryV3 implements ApplicationContextAware {
private static final Map<ContentListV3Enum, ContentListV3> MAP = new ConcurrentHashMap<>();
/**
* 默认事件类
*/
private static final ContentListV3 DEFAULT= new RandomListV3ServiceImpl();
/**
* 注册
* @
* @since 2019/2/25 10:55 上午
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, ContentListV3> beanTypeMap = applicationContext.getBeansOfType(ContentListV3.class);
beanTypeMap.values().forEach(strategyObj -> MAP.put(strategyObj.getSource(), strategyObj));
}
/**
* 获取内容
* @author xiaohao
* @since 2023/2/1 16:15
*/
public static List<Map<String, String>> contentListEvent(String type) {
ContentListV3 contentList = MAP.get(ContentListV3Enum.getEnumByCode(type));
return contentList == null ? DEFAULT.getContentList(type) : contentList.getContentList(type);
}
}
暴露接口
/**
* 内容列表服务
* @author xiaohao
* @since 2023/2/1 15:25
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class ContentListServiceV3 {
/**
* 获取内容列表
* @author xiaohao
* @since 2023/2/1 15:26
*/
public List<Map<String, String>> getContentList(String type) {
// 通过工厂,传入type实现
return ContentFactoryV3.contentListEvent(type);
}
}
最终实现类
/**
* 关注
* @author xiaohao
* @since 2023/2/1 15:51
*/
@Service
public class FocusListV3ServiceImpl implements ContentListV3 {
@Override
public List<Map<String, String>> getContentList(String type) {
Map<String, String> map = new HashMap<>();
map.put("focus", "关注V3");
List<Map<String, String>> list = new ArrayList<>();
list.add(map);
return list;
}
@Override
public ContentListV3Enum getSource() {
return ContentListV3Enum.FOCUS;
}
}
方案四(工厂+策略+枚举+抽象+InitializingBean):
定义抽象类
/**
* 内容列表抽象类
* @author xiaohao
* @since 2023/2/2 13:16
*/
@Slf4j
public abstract class AbstractContentListV4 implements InitializingBean {
/**
* 获取内容列表
* @param type 频道
* @return 内容列表
* @author xiaohao
* @since 2019/1/20 18:36 下午
*/
List<Map<String, String>> getContentList(String type) {
log.error("抽象类方法,具体实现请移至子类,重写该方法");
throw new UnsupportedOperationException();
};
}
定义枚举
/**
* 内容列表枚举类
* @author xiaohao
* @since 2019/1/17 11:06 上午
*/
@Getter
public enum ContentListV4Enum {
/**
*定义枚举
*/
FOCUS("focus", "关注列表"),
RECOMMEND("recommend", "推荐列表"),
CAR("car", "车主列表"),
DEFAULT("default","默认");
private final String code;
private final String name;
ContentListV4Enum(String code, String name) {
this.code = code;
this.name = name;
}
public static ContentListV4Enum getEnumByCode(String code) {
Optional<ContentListV4Enum> first = Arrays.stream(ContentListV4Enum.values()).filter(p -> StringUtils.equals(p.getCode(), code)).findFirst();
return first.orElse(ContentListV4Enum.DEFAULT);
}
}
暴露接口
/**
* 内容列表服务
* @author xiaohao
* @since 2023/2/1 15:25
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class ContentListServiceV4 {
/**
* 获取内容列表
* @author xiaohao
* @since 2023/2/1 15:26
*/
public List<Map<String, String>> getContentList(String type) {
// 通过工厂,传入type实现
return ContentFactoryV4.contentListEvent(type);
}
}
最终实现类
/**
* 车主列表
* @author xiaohao
* @since 2023/2/1 15:51
*/
@Service
public class FocusListV4ServiceImpl extends AbstractContentListV4 {
@Override
public List<Map<String, String>> getContentList(String type) {
Map<String, String> map = new HashMap<>();
map.put("focus", "关注V4");
List<Map<String, String>> list = new ArrayList<>();
list.add(map);
return list;
}
/**
* 绑定设置
* @author xiaohao
* @since 2023/2/2 13:17
*/
@Override
public void afterPropertiesSet() throws Exception {
ContentFactoryV4.register(ContentListV4Enum.FOCUS, this);
}
}
方案五(工厂+策略+自定义注解+ApplicationListener<>)
定义接口
/**
* @description 内容列表
* @author xiaohao
* @since 2019/1/20 17:48 下午
*/
public interface ContentListV5 {
/**
* 获取内容列表
* @param type 频道
* @return 内容列表
* @author xiaohao
* @since 2019/1/20 18:36 下午
*/
List<Map<String, String>> getContentList(String type);
}
自定义注解
/**
* 内容列表策略
* @author xiaohao
* @since 2022/8/8 09:35
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ContentListEvent {
/**
* 策略code
*/
String code();
/**
* 备注
*/
String message();
/**
* 是否生效
*/
boolean flag() default true;
}
工厂
/**
* @description 工厂V5
* @author xiaohao
* @since 2019/1/20 17:41 下午
* @version 1.0
*/
@Slf4j
@Component
public class ContentFactoryV5 implements ApplicationListener<ContextRefreshedEvent>, ContentListV5 {
private static Map<String, ContentListV5> MAP = null;
private static Map<String, String> CODE_MAP = null;
/**
* 监听带ContentListEvent注解的策略
* @author xiaohao
* @since 2022/8/8 09:48
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
// 获取bean工厂带LiveViewCode注解的service实例
Map<String, Object> beansWithAnnotation = contextRefreshedEvent.getApplicationContext().getBeansWithAnnotation(ContentListEvent.class);
if (beansWithAnnotation != null) {
MAP = new HashMap<>(16);
CODE_MAP = new HashMap<>(16);
beansWithAnnotation.forEach((key, value) -> {
// 获取实现类注解里的code
// ContentListEvent annotation = AnnotationUtils.findAnnotation(value.getClass(), ContentListEvent.class);
// cglib代理无法获取到注解,这里需要使用spring自带的工具类来操作
ContentListEvent annotation = value.getClass().getAnnotation(ContentListEvent.class);
String code = annotation.code();
String message = annotation.message();
boolean flag = annotation.flag();
if (flag) {
if (!MAP.containsKey(code)) {
MAP.put(code, (ContentListV5) value);
CODE_MAP.put(code, message);
} else {
log.error("策略类code【{}】出现重复,请检查注解code绑定", code);
}
} else {
log.info("策略类【{}】标注失效,不注册策略工厂", value);
}
});
}
}
/**
* 返回列表
* @author xiaohao
* @since 2023/2/2 13:49
*/
@Override
public List<Map<String, String>> getContentList(String type) {
return MAP.get(type).getContentList(type);
}
}
暴露接口
/**
* 内容列表服务
* @author xiaohao
* @since 2023/2/1 15:25
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class ContentListServiceV5 {
private final ContentFactoryV5 contentFactoryV5;
/**
* 获取内容列表
* @author xiaohao
* @since 2023/2/1 15:26
*/
public List<Map<String, String>> getContentList(String type) {
// 通过工厂,传入type实现
return contentFactoryV5.getContentList(type);
}
}
最终实现
/**
* 关注列表
* @author xiaohao
* @since 2023/2/1 15:51
*/
@Service
@ContentListEvent(code = "focus", message = "关注")
public class FocusListV5ServiceImpl implements ContentListV5 {
@Override
public List<Map<String, String>> getContentList(String type) {
Map<String, String> map = new HashMap<>();
map.put("focus", "关注V5");
List<Map<String, String>> list = new ArrayList<>();
list.add(map);
return list;
}
}
最终测试
/**
* 设计模式测试
* @author xiaohao
* @since 2023/2/2 13:26
*/
@Slf4j
@RestController
@RequestMapping("/v1/designPatterns/")
@RequiredArgsConstructor
public class DesignPatternsController {
private final ContentListServiceV1 contentListServiceV1;
private final ContentListServiceV2 contentListServiceV2;
private final ContentListServiceV3 contentListServiceV3;
private final ContentListServiceV4 contentListServiceV4;
private final ContentListServiceV5 contentListServiceV5;
@GetMapping(value = "test")
public void test() {
String type = "focus";
System.out.println(contentListServiceV1.getContentList(type));
System.out.println("******************************");
System.out.println(contentListServiceV2.getContentList(type));
System.out.println("******************************");
System.out.println(contentListServiceV3.getContentList(type));
System.out.println("******************************");
System.out.println(contentListServiceV4.getContentList(type));
System.out.println("******************************");
System.out.println(contentListServiceV5.getContentList(type));
}
}
访问地址:http://localhost:8080/v1/designPatterns/test
结果打印如下:
[{focus=关注V1}]
******************************
[{focus=关注V2}]
******************************
[{focus=关注V3}]
******************************
[{focus=关注V4}]
******************************
[{focus=关注V5}]
对比
名称 | 技术点 | 复杂度 | 优点 | 缺点 |
---|---|---|---|---|
传统方案一 | - | 简单 | 谁都会 | 不便于扩展,新增策略需修改核心代码 |
方案二 | 工厂+策略+枚举 | 一般 | 传统的设计模式,易上手 | 不满足开闭原则,新增策略需要修改工厂代码 |
方案三 | 工厂+策略+枚举+ApplicationContextAware | 中等 | 满足开闭原则,学习过springboot底层更容易上手 | 类膨胀,需添加钩子接口 |
方案四 | 工厂+策略+枚举+抽象+InitializingBean | 中等 | 满足开闭原则,,学习过springboot底层更容易上手 | 单继承,类膨胀 |
方案五 | 工厂+策略+自定义注解+监听 | 高 | 可配置失效策略 | 策略配置不集中 |
结论
1:策略分支小于等于3,直接if-else简单粗暴
2:策略分支多,方案二三四五任选其一,想卷就选复杂度高的
3:策略超过几十种不建议使用
欢迎大家讨论,互相学习
- 注:需要源码请留言