应用场景:
调停者模式是对象的行为模式。调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用。从而使它们可以较松散地耦合。
当这些对象中的某些对象之间的相互作用发生改变时,不会立即影响到其他的一些对象之间的相互作用。从而保证这些相互作用可以彼此独立地变化。
http://www.cnblogs.com/java-my-life/archive/2012/06/20/2554024.html,写的很好,图不错,可以借鉴下
模拟场景:
老板闲来无事提出一个需求,增加一个小功能,boss首先会找到项目老大,老大一看,首先安排 UI射击师来画图,画图完成之后,再交给工程师1,小房,小闫。
工程师完成之后再交给 测试 ,测试成功 之后 ok
Uml :
代码:
//项目老大接口
- public interface ProjectManagerInterface {
- public void toNextColleague(Colleague c);
- }
projectManager 负责处理同事之间的工作。
- public class ProjectManager implements ProjectManagerInterface {
- private FangDeveloper fang;
- private YanDeveloper yan;
- private Tester tester;
- @Override
- public void toNextColleague(Colleague c) {
- //ba la ba la
- //and so on
- if(c instanceof Designer){
- fang.coding();
- }
- else if(c instanceof FangDeveloper){
- yan.coding();
- }else if(c instanceof YanDeveloper){
- tester.test();
- }else if(c instanceof Tester){
- System.err.println("可以交给boss了");
- }
- }
- public void doAction(){
- Designer designer = new Designer(this);
- fang = new FangDeveloper(this);
- yan = new YanDeveloper(this);
- tester = new Tester(this);
- designer.disignerImage();
- }
- }
- public abstract class Colleague {
- //持有一个调停者对象
- private ProjectManagerInterface mediator;
- /**
- * 构造函数
- */
- public Colleague(ProjectManagerInterface mediator){
- this.mediator = mediator;
- }
- /**
- * 获取 项目老大
- */
- public ProjectManagerInterface getMediator() {
- return mediator;
- }
- }
- public class Designer extends Colleague{
- public Designer(ProjectManagerInterface mediator) {
- super(mediator);
- }
- public void disignerImage(){
- System.err.println("draw a circle...");
- getMediator().toNextColleague(this);
- }
- }
- public class FangDeveloper extends Colleague{
- public FangDeveloper(ProjectManagerInterface mediator) {
- super(mediator);
- }
- public void coding() {
- System.err.println("coding by xiao fang");
- getMediator().toNextColleague(this);
- }
- }
- public class YanDeveloper extends Colleague{
- public YanDeveloper(ProjectManagerInterface mediator) {
- super(mediator);
- }
- public void coding() {
- System.err.println("coding by xiao yanyan");
- getMediator().toNextColleague(this);
- }
- }
- public class Tester extends Colleague {
- public Tester(ProjectManagerInterface mediator) {
- super(mediator);
- }
- public void test(){
- System.err.println("test case...");
- getMediator().toNextColleague(this);
- }
- }
- public class Main {
- public static void main(String[] args) {
- ProjectManager projectManager = new ProjectManager();
- projectManager.doAction();
- }
- }
- draw a circle...
- coding by xiao fang
- coding by xiao yanyan
- test case...
- 可以交给boss了