策略模式的定义
- 是指定义了算法家族、分别封装起来,让它们之间可以互相替换,此模式让算法的变化不会影响到使用算法的用户
- 策略模式还可以避免多重分支的if...else...和switch语句
- 策略模式更多的用于算法的封装
适用场景
- 假如系统中有很多类,而他们的区别仅仅在于他们的行为不同
- 一个系统需要动态地在几种算法中选择一种
举个栗子:要举办一个促销活动,促销的方式有很多,供顾客选择,顾客可以根据自己的喜好进行领取优惠,参与促销活动。
/**
* @author wcj
* @description 大型售卖活动
* @date 2019/8/24 15:39
*/
public class Activity {
public void doActivity(Promotion promotion) {
promotion.sales();
}
}
/**
* 促销方式抽象接口,定义抽象方法
*/
public interface Promotion {
void sales();
}
/**
* @author wcj
* @description 暂无促销活动
* @date 2019/8/24 17:52
*/
public class EmptyPromotion implements Promotion{
@Override
public void sales() {
System.out.println("暂无优惠,敬请期待");
}
}
/**
* @author wcj
* @description 团购促销
* @date 2019/8/24 17:52
*/
public class GroupBuyPromotion implements Promotion {
@Override
public void sales() {
System.out.println("团购促销啦~~~");
}
}
/**
* @author wcj
* @description 优惠券促销
* @date 2019/8/24 17:54
*/
public class CouponsPromotion implements Promotion {
@Override
public void sales() {
System.out.println("优惠券促销啦");
}
}
/**
* @author wcj
* @description 现金返现促销
* @date 2019/8/24 17:52
*/
public class CashBackPromotion implements Promotion {
@Override
public void sales() {
System.out.println("现金返现促销啦~~~");
}
}
/**
* @author wcj
* @description 活动测试,模拟客户使用不同的方式进行促销
* @date 2019/8/24 17:55
*/
public class ActivityTest {
public static void main(String[] args) {
//现金促销
new Activity().doActivity(new CashBackPromotion());
//团购促销
new Activity().doActivity(new GroupBuyPromotion());
//优惠券促销
new Activity().doActivity(new CouponsPromotion());
/**
* 第二种方式进行促销,属于比较常见的一种
*/
String type = "CashBack";
if("CashBack".equals(type)){
new Activity().doActivity(new CashBackPromotion());
}else if("GroupBuy".equals(type)){
new Activity().doActivity(new GroupBuyPromotion());
}else if("Coupons".equals(type)){
new Activity().doActivity(new CouponsPromotion());
}//.......
}
}
上面的是一种比较简单,便于理解的一种写法,但是不太利于管理和修改,不符合开闭原则。也可以使用工厂+单例+策略完成,代码重构如下
/**
* 促销活动工厂:用于统一管理促销方式
*/
public class ActivityFactory {
private ActivityFactory(){}
private static Map<String,Object> map = new HashMap<>();
static {
map.put("CashBack",new CashBackPromotion());
map.put("GroupBuy",new GroupBuyPromotion());
map.put("Coupons",new CouponsPromotion());
}
public static Promotion getInstance(String type){
Promotion promotion = (Promotion) map.get(type);
return promotion == null?new EmptyPromotion():promotion;
}
}
/**
* 测试代码:促销工厂管理
*/
Promotion promotion = ActivityFactory.getInstance(type);
promotion.sales();
JDK中策略模式的实现
Comparator类中的compare方法
Arrays类中的parseSort等方法
TreeMap类中的构造器,可以传入需要的比较器
Spring中策略模式的实现:Resouce类
策略模式的优点
- 符合开闭原则,添加新的需求时,不需要修改,只需要新增的类以满足需求
- 避免使用多重条件转移语句,if...else...语句,switch语句
- 使用策略模式提高算法保密性和安全性
策略模式的缺点
- 客户端必须知道所有的策略,并且自行决定使用哪个策略
- 代码中会产生很多策略类,增加维护难度
/**
* 简易版DispatchServlet,主要讲述了spring中的一些调度思想
*
* @author wcj
* @description
* @date 2019/8/26 11:48
*/
public class SimpleDispatchServlet extends HttpServlet {
/**
* 初始化容器:用于将请求url和对应controller绑定
*/
private static List<Hander> list = new ArrayList<>();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
this.service(req, resp);
}
@Override
public void init() {
try {
//反射得到对象,将类中的方法,参数应用初始化时绑定
Class<InfoController> infoControllerClass = InfoController.class;
list.add(new Hander().setController(infoControllerClass)
.setMethod(infoControllerClass.getMethod("getInfo", new Class[]{String.class}))
.setUrl("/userId"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
doDispatchServlet(req, resp);
}
private void doDispatchServlet(HttpServletRequest req, HttpServletResponse resp) {
//获取请求的url
String uri = req.getRequestURI();
Hander h = null;
//将容器中的请求和客户请求地址比对,寻找具体方法,有就直接返回,没有就返回404
for (Hander hander : list) {
if (uri.equals(hander.getUrl())) {
h = hander;
break;
}
}
try {
if (h != null) {
//通过反射调用方法
Object invoke = h.getMethod().invoke(h.getController(), req.getParameter("mid"));
resp.getWriter().write(invoke.toString());
} else {
resp.getWriter().write("404 Not Found");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
doGet(req, resp);
}
/**
* 可以使用内部类,也可以新建一个类
*/
class Hander {
private Object Controller;
private Method method;
private String url;
public Object getController() {
return Controller;
}
public Hander setController(Object controller) {
Controller = controller;
return this;
}
public Method getMethod() {
return method;
}
public Hander setMethod(Method method) {
this.method = method;
return this;
}
public String getUrl() {
return url;
}
public Hander setUrl(String url) {
this.url = url;
return this;
}
}
}
上述代码,使用到了单例、工厂、委派,策略等设计模式