舒适提示:内容较多建议收藏阅读,大约须要5分钟。java
代码可在GitHub中阅读:欢迎star。github.com/UniqueDong/…git
在经常使用的23种设计模式中其实面没有委派模式(delegate)的影子,可是在 Spring 中委派模式确实用的比较多的一种模式,Spring MVC 框架中的DispatcherServlet其实就用到了委派模式github
其实我我的的理解就是一个特殊的静态代理模式,只不过加入了策略模式,咱们看下面的图。算法
策略模式的环境上下文角色只有一个策略抽象的引用,而后根据条件设置对应的策略调用。而委托持有全部的策略,根据条件去将请求委托到对应的实现类中执行。设计模式
咱们经过一个简单例子来看看策略模式:它只只有一个策略的引用,根据不一样场景切换策略。bash
假设如今要设计一个贩卖各种书籍的电子商务网站的购物车系统。app
一个最简单的状况就是把全部货品的单价乘上数量,可是实际状况确定比这要复杂。框架
好比,本网站可能对全部的高级会员提供每本20%的促销折扣;对中级会员提供每本10%的促销折扣;对初级会员没有折扣。ide
根据描述,折扣是根据如下的几个算法中的一个进行的:函数
代码实现以下:定义抽象策略角色
/** * 抽象折扣类(抽象策略(Strategy)角色) */
public interface MemberStrategy {
/** * 计算图书的价格 * @param booksPrice 原书价格 * @return 打折后的价格 */
double calcPrice(double booksPrice);
}
复制代码
而后定义咱们针对不一样会员的折扣算法实现
/** * 具体策略(ConcreteStrategy)角色 * 初级会员折扣具体策略 */
public class PrimaryMemberStrategy implements MemberStrategy {
@Override
public double calcPrice(double booksPrice) {
System.out.println("对初级会员没有打折");
return booksPrice;
}
}
/** * 中级会员折扣策略 */
public class IntermediteMemberStrategy implements MemberStrategy {
@Override
public double calcPrice(double booksPrice) {
System.out.println("对于中级会员的折扣为10%");
return booksPrice * 0.9;
}
}
/** * 高级会员折扣策略 */
public class AdvanceMemberStrategy implements MemberStrategy {
@Override
public double calcPrice(double booksPrice) {
return booksPrice * 0.8;
}
}
复制代码
最后咱们还需定义个策略上下文角色,它只有一个抽象策略的引用。
/** * 环境(Context)角色:持有一个Strategy的引用。 * Created by unique on 2017/6/1. */
public class PriceContext {
/** * 持有一个具体的策略对象 */
private MemberStrategy memberStrategy;
/** * 构造方法,传入一个具体策略 * @param memberStrategy */
public PriceContext(MemberStrategy memberStrategy) {
this.memberStrategy = memberStrategy;
}
/** * 计算图书的价格 * @param booksPrice * @return */
public double calcPrice(double booksPrice) {
return memberStrategy.calcPrice(booksPrice);
}
public void setMemberStrategy(MemberStrategy memberStrategy) {
this.memberStrategy = memberStrategy;
}
}
复制代码
最后咱们编写客户端来模拟
public class Clinet {
public static void main(String[] args) {
//选择并建立所要的策略
MemberStrategy strategy = new AdvanceMemberStrategy();
//建立环境
PriceContext context = new PriceContext(strategy);
double booksPrice = 500.98;
double price = context.calcPrice(booksPrice);
System.out.println("图书的原价:" + booksPrice + "打折后的最终价格:" + price);
System.out.println("----切换会员----");
strategy = new PrimaryMemberStrategy();
context.setMemberStrategy(strategy);
price = context.calcPrice(booksPrice);
System.out.println("图书的原价:" + booksPrice + "打折后的最终价格:" + price);
}
}
复制代码
打印结果以下所示:
书的原价:500.98打折后的最终价格:400.78400000000005
----切换会员----
对初级会员没有打折
图书的原价:500.98打折后的最终价格:500.98
复制代码
如今咱们回到委派模式,其实跟策略模式很像,区别就是委派模式的 DispatcherServlet 持有全部的委托类引用。
假设如今 Boss 点子来了想作一个根据手机外壳来改变 app 主题样式功能。这个时候就把任务交给了产品经理 ,产品经理就找来程序猿委托开发小哥哥去分工实现。这样Boss就是一个请求,而产品经理就是一个 dispatcherSevlet 。
boss把任务给 leader, 而 leader 作了一个任务的分配和调度的工做,本身没有作工做,而是把具体工做交给具体的执行者去作。
代码实现以下
首先定义咱们的任务执行角色,以及各自分工任务的程序猿
/** * 执行的接口 */
public interface IExcuter {
void execute(String command);
}
/** * 程序猿A执行的工做 */
public class ExcuterA implements IExcuter {
@Override
public void execute(String command) {
System.out.println("员工A 开始作" + command + "的工做");
}
}
/** * 程序猿B执行的任务 */
public class ExcuterB implements IExcuter {
@Override
public void execute(String command) {
System.out.println("员工B 开始作" + command + "的工做");
}
}
复制代码
接着定义咱们的 产品经理
/** * @description: leader 委派者 任务分发的做用 * @ModificationHistory who when What **/
public class LeaderDispatch {
private Map<String, IExcuter> targets = new HashMap<String, IExcuter>();
public Leader() {
targets.put("识别颜色", new ExcuterA());
targets.put("切换主题", new ExcuterB());
}
public void dispatch (String command) {
//根据指令委托到对应的执行者
targets.get(command).execute(command);
}
}
复制代码
最后咱们模拟boss发送指令实现功能
public class Boss {
public static void main(String[] args) {
LeaderDispatch leader = new LeaderDispatch();
//看上去好像是咱们的项目经理在干活
//但实际干活的人是普通员工
//这就是典型,干活是个人,功劳是你的
leader.execute("识别颜色");
leader.execute("切换主题");
}
}
复制代码
最后咱们查看打印结果
员工B 开始作登陆的工做
员工A 开始作加密的工做
复制代码
经过此文咱们从新复习了策略模式,经过策略模式演化出委托模式。
Spring MVC框架中的DispatcherServlet其实就是用到的委派模式,针对以前的MVC执行流程没法理解的HandlerMapping其实就是这里的项目经理维护的各个员工的信息。
上述实例中,为了简单,直接在构造函数中维护了相关实例的引用,可是在具体的HandlerMapping 中,这一步显然要复杂的多。
关注、转发、点赞收藏,个人两小时编写换你一秒的随手关注、转发、点赞收藏