一、业务场景
做了微信支付,支付宝支付,然后突然要加一个银联支付,那么如果代码方法一起就会导致代码不是很好操作所以,采用策略模式进行。
二、直接上代码
1、策略接口:执行的方法
package com.cxy.ssp.strategy; public interface Strategy { String getVpcList(DemoPojo demoPojo); }
2、实体类:
package com.cxy.ssp.strategy; public class DemoPojo { private String poolid; private String orderId; private String name; public String getPoolid() { return poolid; } public void setPoolid(String poolid) { this.poolid = poolid; } public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public DemoPojo(String poolid, String orderId, String name) { this.poolid = poolid; this.orderId = orderId; this.name = name; } public DemoPojo() { } }
3、实现类:
package com.cxy.ssp.strategy.impl; import com.cxy.ssp.strategy.DemoPojo; import com.cxy.ssp.strategy.Strategy; import org.springframework.stereotype.Component; @Component("A") public class SA implements Strategy { @Override public String getVpcList(DemoPojo demoPojo) { System.out.println("A,getVpcList ==========="+demoPojo); return demoPojo.getName(); } } package com.cxy.ssp.strategy.impl; import com.cxy.ssp.strategy.DemoPojo; import com.cxy.ssp.strategy.Strategy; import org.springframework.stereotype.Component; @Component("B") public class SB implements Strategy { @Override public String getVpcList(DemoPojo demoPojo) { System.out.println("b,getVpcList ==========="+demoPojo); return demoPojo.getName(); } } package com.cxy.ssp.strategy.impl; import com.cxy.ssp.strategy.DemoPojo; import com.cxy.ssp.strategy.Strategy; import org.springframework.stereotype.Component; @Component("C") public class SC implements Strategy { @Override public String getVpcList(DemoPojo demoPojo) { System.out.println("c"+demoPojo); return demoPojo.getName(); } }
4、策略context
package com.cxy.ssp.strategy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /***、 *通过Spring将实现Strategy的实现类都自动注入到strategyMap类中, * 当用户传入选择的资源池时,自动根据资源池id去对应的资源池实现中查找资源。 * */ @Service public class SimpleContext { @Autowired private final Map<String, Strategy> strategyMap = new ConcurrentHashMap<>(); public SimpleContext(Map<String, Strategy> strategyMap) { this.strategyMap.clear(); strategyMap.forEach((k, v)-> this.strategyMap.put(k, v)); } public String getResource(DemoPojo demoPojo){ return strategyMap.get(demoPojo.getPoolid()).getVpcList(demoPojo); } }
5、策略服务L:
package com.cxy.ssp.controller; import com.cxy.ssp.strategy.DemoPojo; import com.cxy.ssp.strategy.SimpleContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController public class TestController { @Autowired private SimpleContext simpleContext; @PostMapping("/choose") public String choose(@RequestBody DemoPojo demoPojo){ return simpleContext.getResource(demoPojo); } }
package com.cxy.ssp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SspApplication { public static void main(String[] args) { SpringApplication.run(SspApplication.class, args); } }
三、测试
四、改造代码
package com.cxy.ssp.strategy.impl; import com.cxy.ssp.strategy.DemoPojo; import com.cxy.ssp.strategy.Strategy; import org.springframework.stereotype.Component; @Component("A") public class SA implements Strategy { @Override public String getVpcList(DemoPojo demoPojo) { System.out.println("A,getVpcList ==========="+demoPojo); return demoPojo.getName()+"a"; } } package com.cxy.ssp.strategy.impl; import com.cxy.ssp.strategy.DemoPojo; import com.cxy.ssp.strategy.Strategy; import org.springframework.stereotype.Component; @Component("B") public class SB implements Strategy { @Override public String getVpcList(DemoPojo demoPojo) { System.out.println("b,getVpcList ==========="+demoPojo); return demoPojo.getName()+"b"; } } package com.cxy.ssp.strategy.impl; import com.cxy.ssp.strategy.DemoPojo; import com.cxy.ssp.strategy.Strategy; import org.springframework.stereotype.Component; @Component("C") public class SC implements Strategy { @Override public String getVpcList(DemoPojo demoPojo) { System.out.println("c"+demoPojo); return demoPojo.getName()+"c"; } }
那么如果传输的那个poolid传输错误了改怎么处理呢:
服务器端报错误:说明没有这个方法,
那么可以在context中进行限制:
首先判断是否包含,如果不包含,就可以直接threw一个异常,让前台直接显示。