青海教育厅门户网站,电子商务网站建设招标书,海外公司推广,wordpress 幻灯需求背景 
1、在管控流程中#xff0c;涉及到的业务操作很多#xff0c;不同的业务具有不同的策略实现 举个具体的例子#xff1a; 在比价管控流程中#xff0c;有比价策略和管控策略#xff0c;每个业务的比价和管控均不一样。因此使用策略模式来开发 
整体架构流程 
1、…需求背景 
1、在管控流程中涉及到的业务操作很多不同的业务具有不同的策略实现 举个具体的例子 在比价管控流程中有比价策略和管控策略每个业务的比价和管控均不一样。因此使用策略模式来开发 
整体架构流程 
1、定义业务策略枚举 比价 和 管控 
/*** description:* author: hongbin.zheng* create: 2023-07-17 16:33**/
public enum StrategyType {priceCompare,priceControl;
}2、定义业务策略注解标注出 策略的类 
/*** description:* author: hongbin.zheng* create: 2023-07-17 16:34**/
Retention(value  RetentionPolicy.RUNTIME)
Target(value  ElementType.TYPE)
Component
Inherited
public interface StrategyAnnotation {/*** 策略类型*/StrategyType[] strategyType();/*** 具体的策略建*/String[] dataType() default {};
} 
3、定义策略基本方法 
/*** description:* author: hongbin.zheng* create: 2023-07-17 16:46**/
public interface PriceCompareStrategy {/*** 执行价格比价*/public String doPriceCompare();
}/*** description:* author: hongbin.zheng* create: 2023-07-17 16:52**/
public interface PriceControlStrategy {/*** 执行管控* return*/public String doPriceControl();
}4、写每个具体实现的策略类 
/*** description:* author: hongbin.zheng* create: 2023-07-17 16:44**/
Service
StrategyAnnotation(strategyType  StrategyType.priceCompare, dataType  firstPriceCompare)
public class FirstPriceCompareStrategy implements PriceCompareStrategy {Overridepublic String doPriceCompare() {System.out.println(-------first price compare start---------);System.out.println(-------do do do ---------);System.out.println(-------first price compare end---------);return first price compare end;}
}/*** description:* author: hongbin.zheng* create: 2023-07-17 16:50**/
Service
StrategyAnnotation(strategyType  StrategyType.priceCompare, dataType  secondPriceCompare)
public class SecondPriceCompareStrategy implements PriceCompareStrategy {Overridepublic String doPriceCompare() {System.out.println(-------second price compare start---------);System.out.println(-------do do do ---------);System.out.println(-------second price compare end---------);return second price compare end;}
} 
/*** description:* author: hongbin.zheng* create: 2023-07-17 16:53**/
Service
StrategyAnnotation(strategyType  StrategyType.priceControl, dataType  firstPriceControl)
public class FirstPriceControlStrategy implements PriceControlStrategy {Overridepublic String doPriceControl() {System.out.println(---------do first price control start ---------------);System.out.println(---------do first price control end ---------------);return do first price control end;}
}/*** description:* author: hongbin.zheng* create: 2023-07-17 16:53**/
Service
StrategyAnnotation(strategyType  StrategyType.priceControl, dataType  secondPriceControl)
public class SecondPriceControlStrategy implements PriceControlStrategy {Overridepublic String doPriceControl() {System.out.println(---------do second price control start ---------------);System.out.println(---------do second price control end ---------------);return do second price control start;}
}Service
StrategyAnnotation(strategyType  {StrategyType.priceCompare, StrategyType.priceControl}, dataType  commonStrategy)
public class PriceCompareAndControlStrategy implements PriceCompareStrategy, PriceControlStrategy {Overridepublic String doPriceCompare() {System.out.println(-------common price compare start---------------);System.out.println(-------common price compare end---------------);return common price compare end;}Overridepublic String doPriceControl() {System.out.println(-------common price control start---------------);System.out.println(-------common price control end---------------);return common price control end;}
}5、编写策略管理类管理所有的策略并且对外暴露出 拿到具体策略类的方法 
package com.dgut.strategy.springboot_strategy;import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;
import java.util.Map;/*** description:* author: hongbin.zheng* create: 2023-07-17 16:59**/
Service
public class StrategyServiceManager {Autowiredprivate ApplicationContext context;/*** 存储策略:类型具体实现类*/private MapStrategyType, MapString, Object map;public E E getStrategyInstance(StrategyType strategyType, String type, ClassE eClass) {MapString, Object strategyTypeMap  map.get(strategyType);if (strategyTypeMap ! null  !map.isEmpty()) {Object bean  strategyTypeMap.get(type);if (bean ! null) {return eClass.cast(bean);}}return null;}PostConstructprivate void init() {map  Maps.newHashMap();MapString, Object beans  context.getBeansWithAnnotation(StrategyAnnotation.class);for (Object value : beans.values()) {StrategyAnnotation annotation  value.getClass().getAnnotation(StrategyAnnotation.class);StrategyType[] strategyTypes  annotation.strategyType();String[] dataTypes  annotation.dataType();if (strategyTypes  null || strategyTypes.length  0 || dataTypes  null || dataTypes.length  0) {throw new RuntimeException(注解上必填字段不能为空);}for (StrategyType strategyType : strategyTypes) {for (String dataType : dataTypes) {map.computeIfAbsent(strategyType, k - Maps.newHashMap()).put(dataType, value);}}}}} 
6、代码流程测试 
package com.dgut.strategy.springboot_strategy;import com.dgut.strategy.springboot_strategy.compare.PriceCompareStrategy;
import com.dgut.strategy.springboot_strategy.control.PriceControlStrategy;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;/*** description:* author: hongbin.zheng* create: 2023-07-18 16:17**/
SpringBootTest
public class StrategyTest {Autowiredprivate StrategyServiceManager strategyServiceManager;Testpublic void test2() {String strategyName  priceControl;String dataType  firstPriceControl;System.out.println(priceControlTest(strategyName, dataType));String dataType2  secondPriceControl;System.out.println(priceControlTest(strategyName, dataType2));String dataType3  commonStrategy;System.out.println(priceControlTest(strategyName, dataType3));}public String priceControlTest(String strategyName, String dataType) {StrategyType strategyType  StrategyType.valueOf(strategyName);if (strategyType  null) {return 管控策略不存在;}PriceControlStrategy strategyInstance  strategyServiceManager.getStrategyInstance(strategyType, dataType, PriceControlStrategy.class);if (strategyInstance  null) {return 找不到管控策略;}return strategyInstance.doPriceControl();}Testpublic void test() {String strategyName  priceCompare;String dataType  firstPriceCompare;System.out.println(priceCompareTest(strategyName, dataType));String dataType2  secondPriceCompare;System.out.println(priceCompareTest(strategyName, dataType2));String dataType3  commonStrategy;System.out.println(priceCompareTest(strategyName, dataType3));}public String priceCompareTest(String strategyName, String dataType) {StrategyType strategyType  StrategyType.valueOf(strategyName);if (strategyType  null) {return 策略不存在;}PriceCompareStrategy strategyInstance  strategyServiceManager.getStrategyInstance(strategyType, dataType, PriceCompareStrategy.class);if (strategyInstance  null) {return 找不到比价策略;}return strategyInstance.doPriceCompare();}
} 
技术细节 
通过spring的context对象解析获取类注解拿到bean常规操作这个要学起来很多地方可以这样子操作呢。 
总结 知道的越多不知道的越多希望对你有帮助 \color{red}知道的越多不知道的越多希望对你有帮助 知道的越多不知道的越多希望对你有帮助 文章转载自: http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.ylyzk.cn.gov.cn.ylyzk.cn http://www.morning.bbtn.cn.gov.cn.bbtn.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.wwdlg.cn.gov.cn.wwdlg.cn http://www.morning.pwmm.cn.gov.cn.pwmm.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.ljfjm.cn.gov.cn.ljfjm.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.hlrtzcj.cn.gov.cn.hlrtzcj.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.gcysq.cn.gov.cn.gcysq.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.dqpnd.cn.gov.cn.dqpnd.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.kmldm.cn.gov.cn.kmldm.cn http://www.morning.rqqn.cn.gov.cn.rqqn.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.skrcn.cn.gov.cn.skrcn.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.fwkpp.cn.gov.cn.fwkpp.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.fpzpb.cn.gov.cn.fpzpb.cn http://www.morning.prgnp.cn.gov.cn.prgnp.cn http://www.morning.gkgb.cn.gov.cn.gkgb.cn http://www.morning.jwncx.cn.gov.cn.jwncx.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.hlxxl.cn.gov.cn.hlxxl.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.mxnfh.cn.gov.cn.mxnfh.cn http://www.morning.fydsr.cn.gov.cn.fydsr.cn