设计模式(四)委派模式

委派模式
两个角色, 受托人,委托人


项目经理(委托人):安排任务
普通员工(受托人):执行任务


特点:
1.类似中介的功能
2.持有被委托人的引用
3.不关心过程,只关心结果


主要是为了隐藏复杂的逻辑过程

public interface IExecutor {

	public void doning();
}
public class ExecutorA implements IExecutor{

	public void doning() {
		System.out.println("员工A开始执行任务");
	}
}

public class ExecutorB implements IExecutor{

	public void doning() {
		System.out.println("员工B开始执行任务");
	}
}

//委托人,项目经理
public class Dispatcher implements IExecutor{
	IExecutor executor;
	
	public Dispatcher(IExecutor executor) {
		this.executor=executor;
	}
	
	//虽然也有执行方法
	//但是他不做事,只分配任务
	public void doning() {
		this.executor.doning();
	}

}

public class Main {
public static void main(String[] args) {
	Dispatcher dispatcher=new Dispatcher(new ExecutorA());
	//看上去是项目经理干活
	//实际是普通员工干活
	dispatcher.doning();
}
}






猜你喜欢

转载自blog.csdn.net/q975583865/article/details/81054100