求上进的人,不要总想着靠谁,人都是自私的,自己才是最靠得住的人。
我不想干的活,你来干——委派模式
1. 委派模式
定义
委派模式又叫委托模式。它的基本作用就是负责任务的调度和任务分配,将任务的分配和执行分离开来。可以看作是一种特殊情况下的静态代理的全权代理
不属于GOF 23中设计模式之一
属于行为型设计模式
应用场景
委派对象本身不知道如何处理一个任务(或一个请求),把请求交给其它对象来处理。
实现程序的解耦
优点
通过任务委派能够将一个大型的任务细化,然后通过统一的管理这些子任务的完成情况实现任务的跟进,能够加快任务的执行效率。
缺点
任务委派方式需要根据任务的复杂程度进行不同的改变,在任务比较复杂的情况下可能需要进行多重委派,容易造成紊乱。
2. 简单委派模式
委派模式在生活中很常见。比如说我们公司的老板给经理下达一个任务命令。那么经理一般不自己去做,他会看看手底下有没有人擅长做,然后委派给这个员工做。
public interface IEmployee {
void doing(String task);
}
public class EmployeeA implements IEmployee {
private String goooAt = "编程";
public void doing(String task) {
System.out.println("我是员工A,我擅长" + goooAt + ",现在开始做任务:" + task);
}
}
public class EmployeeB implements IEmployee {
private String goooAt = "平面设计";
public void doing(String task) {
System.out.println("我是员工B,我擅长" + goooAt + ",现在开始做任务:" + task);
}
}
public class Leader implements IEmployee{
private Map<String, IEmployee> map;
public Leader() {
map = new HashMap<String, IEmployee>();
map.put("开发", new EmployeeA());
map.put("海报", new EmployeeB());
}
public void doing(String task) {
if (!map.containsKey(task)) {
System.out.println("这个任务我不擅长,拒绝做");
} else {
map.get(task).doing(task);
}
}
}
public class Boss {
public void command(String task, Leader leader){
leader.doing(task);
}
}
public class Test {
public static void main(String[] args) {
new Boss().command("开发", new Leader());
new Boss().command("海报", new Leader());
new Boss().command("捕鱼", new Leader());
}
}
我是员工A,我擅长编程,现在开始做任务:开发
我是员工B,我擅长平面设计,现在开始做任务:海报
这个任务我不擅长,拒绝做

3. ClassLoader之双亲委派
JDK的经典委派模式的案例:ClassLoader。翻开源码,loadClass()方法:
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
其中:
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
就是双亲委派模式。parent的定义private final ClassLoader parent; 本身就是一个ClassLoader,就是如果有父类,那么就调用父类的loadClass方法,一直找到最顶层的父类,如果没有,就调用本身的。就是为了防止类重复加载。
4. 委派模式和代理模式的区别
委派模式是行为模式,代理模式是结构型模式
委派模式注重的是任务派遣,注重结果;
代理模式注重的是代码的增强,注重过程。
委派模式是一种特殊的静态代理,相当于全权代理。
感谢您阅读本文,如果您觉得文章写的对您有用的话,请您点击上面的“关注”,点个赞,这样您就可以持续收到《JAVA架构师之路》的最新文章了。文章内容属于自己的一点点心得,难免有不对的地方,欢迎在下方评论区探讨,你们的关注是我创作优质文章的动力。
本文探讨了设计模式中的委派模式,解释了其作为任务调度和分配的角色,强调了它在程序解耦中的应用。文章通过ClassLoader的双亲委派机制实例阐述了委派模式,并对比了委派模式与代理模式的区别。最后,指出了委派模式在处理复杂任务时可能存在的挑战。
1254

被折叠的 条评论
为什么被折叠?



