最近准备学习下以前项目中用到的设计模式,这里代码都只展现核心业务代码,省略去大多不重要的代码。算法
代码大可能是以前一块儿工做的小伙伴coding出来的,我这里作一个学习和总结,我相信技术能力的提升都是先从模仿开始的,学习别人的代码及设计思想也是一种提高的方式。数据库
后续还会有观察者模式、责任链模式的博客产出,都是工做中正式运用到的场景输出,但愿对看文章的你也有启发和帮助。设计模式
1 class Client { 2 public static void main(String[] args) { 3 ICalculator calculator = new Add(); 4 Context context = new Context(calculator); 5 int result = context.calc(1,2); 6 System.out.println(result); 7 } 8 9 10 interface ICalculator { 11 int calc(int a, int b); 12 } 13 14 15 static class Add implements ICalculator { 16 @Override 17 public int calc(int a, int b) { 18 return a + b; 19 } 20 } 21 22 23 static class Sub implements ICalculator { 24 @Override 25 public int calc(int a, int b) { 26 return a - b; 27 } 28 } 29 30 31 static class Multi implements ICalculator { 32 @Override 33 public int calc(int a, int b) { 34 return a * b; 35 } 36 } 37 38 39 static class Divide implements ICalculator { 40 @Override 41 public int calc(int a, int b) { 42 return a / b; 43 } 44 } 45 46 47 static class Context { 48 private ICalculator mCalculator; 49 50 51 public Context(ICalculator calculator) { 52 this.mCalculator = calculator; 53 } 54 55 56 public int calc(int a, int b) { 57 return this.mCalculator.calc(a, b); 58 } 59 }}
1 @Getter 2 public enum MsgCollectEnum { 3 4 /** 5 * 枚举入口:用户首次提问 给医生 文案内容(医生id拼链接) 6 */ 7 FIRST_QUESTION_CONTENT(2101, 1, MsgSmsEnum.SMS_FIRST_QUESTION_CONTENT, MsgPushEnum.PUSH_FIRST_QUESTION_CONTENT, MsgWechatEnum.WECHAT_FIRST_QUESTION_CONTENT); 8 9 10 /** 11 * 短信文案:用户首次提问 给医生 文案内容 12 */ 13 SMS_FIRST_QUESTION_CONTENT(STTurnLinkEnum.DOCTOR_QUESTION_SETTING_PAGE.getStoapp(), "您好,有一位用户向您发起咨询,请确认接单,赶快进入APP查看吧!{0}"); 14 15 16 /** 17 * Push文案:用户首次提问 给医生 文案内容 18 */ 19 PUSH_FIRST_QUESTION_CONTENT(STTurnLinkEnum.DOCTOR_QUESTION_SETTING_PAGE.getStoapp(), STPushAudioEnum.PAY_SUCCESS.getType(), "您好, 有一位用户向您发起了咨询服务"); 20 21 22 ...... 23 }
1 MsgContext msgContext = new MsgContext(); 2 msgContext.setDoctorId(questionDO.getDoctorId()); 3 msgContext.setReceiveUid(questionDO.getDrUid()); 4 msgContext.setMsgType(MsgCollectEnum.FIRST_QUESTION_CONTENT.getType()); 5 this.stContextStrategyFactory.doStrategy(String.valueOf(msgContext.getMsgType()), QuestionMsgStrategy.class).handleSeniority(msgContext);
1 @Slf4j 2 public class STContextStrategyFactory { 3 public <O extends STIContext, T extends STIContextStrategy<O>> STIContextStrategy<O> doStrategy(String type, Class<T> clazz) { 4 Map<String, T> beanMap = STSpringBeanUtils.getBeanMap(clazz); 5 if (MapUtils.isEmpty(beanMap)) { 6 log.error("获取class:{} 为空", clazz.getName()); 7 } 8 try { 9 for (Map.Entry<String, T> entry : beanMap.entrySet()) { 10 Object real = STAopTargetUtils.getTarget(entry.getValue()); 11 STStrategyAnnotation annotation = real.getClass().getAnnotation(STStrategyAnnotation.class); 12 List<String> keySet = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(annotation.type()); 13 if (keySet.contains(type)) { 14 return entry.getValue(); 15 } 16 } 17 } catch (Exception e) { 18 log.error("获取目标代理对象失败:{}", e); 19 } 20 log.error("strategy type = {} handle is null", type); 21 return null; 22 } 23 }
1 @Component 2 @STStrategyAnnotation(type = "2101-2104-2113-2016", description = "发给医生,无其余附属信息") 3 public class QuestionMsgSimpleToDoctorStrategyImpl extends AbstractQuestionSendMsgStrategy { 4 5 6 @Autowired 7 private RemoteMsgService remoteMsgService; 8 @Autowired 9 private QuestionDetailService questionDetailService; 10 11 12 @Override 13 public StarSmsIn buildSmsIn(MsgContext context) { 14 // do something 15 } 16 17 18 @Override 19 public StarPushIn buildPushIn(MsgContext context) { 20 // do something 21 } 22 23 24 ...... 25 26 27 } 28 29 30 @Slf4j 31 public abstract class AbstractQuestionSendMsgStrategy implements QuestionMsgStrategy { 32 /** 33 * 构建短信消息 34 * 35 * @param context 36 * @return 37 */ 38 public abstract StarSmsIn buildSmsIn(MsgContext context); 39 40 41 /** 42 * 构建push消息 43 * 44 * @param context 45 * @return 46 */ 47 public abstract StarPushIn buildPushIn(MsgContext context); 48 49 50 /** 51 * 构建微信公众号 52 * 53 * @param context 54 * @return 55 */ 56 public abstract StarWeChatIn buildWeChatIn(MsgContext context); 57 58 59 @Override 60 public STResultInfo handleSeniority(MsgContext msgContext) { 61 // buildMsg and send kafka 62 } 63 }