最近準備學習下以前項目中用到的設計模式,這裏代碼都只展現核心業務代碼,省略去大多不重要的代碼。算法
代碼大可能是以前一塊兒工做的小夥伴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 }