• 设计模式——中介者模式


    中介者模式:如果一个系统中对象之间的联系呈现为网状结构,对象之间存在大量的多对多关系,将导致关系及其复杂,这些对象称为“同事对象”,

                     我们可以引入一个中介者对象使各个同时对象只跟中介者对象打交道。将复杂的网络结构化解为如下的星形结构。使用中介者模式来

                     集中相关对象之间复杂的沟通和控制方式。

    优点:  通过将对象的解耦增加对象的可复用性。
               通过控制逻辑集中,可以简化系统维护。

    应用场景:GUI开发中的window对象或DOM对象。
                  MVC模式中的Control部分也是中介者,是Modle和View的中介。

    但是在实际开发中如果各个"同事对象"之间的关系不是特别复杂,就没有必要用中介者模式,会增加代码的复杂度,而且中介者模式的有点也体现不出来。

    下面的Demo只是演示,如果像下面这样简单的关系就没有必要用中介者模式。

    Demo用的是面向继承而不是面向接口,这不违反面向接口编程的设计原则,因为在下面这种情况下,使用继承会有更好的代码复用度,更好的运用多态。

    例子很简单,很容易看明白。

    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * 中介者
     * @author wxisme
     *
     */
    abstract class Mediator {
    	protected Map<String,Colleague> map = new HashMap<String , Colleague>();
    	
    	public void addColleague(String name, Colleague colleague) {
    		colleague.setMediator(this);
    		map.put(name, colleague);
    	}
    	
    	public void removeColleague(String name) {
    		map.remove(name);
    	}
    	
    	public abstract void execute(String name, String method);
    
    }
    
    class ConcreteMediator extends Mediator {
    	@Override
    	public void execute(String name, String method) {
    		if(name.equals("ConcretColleagueA")) {
    			if(method.equals("self")) {
    				this.map.get(name).self();
    			}
    			else {
    				this.map.get(name).out();
    			}
    		}
    		else {
    			if(method.equals("self")) {
    				this.map.get(name).self();
    			}
    			else {
    				this.map.get(name).out();
    			}
    		}
    	}
    }
    
    /**
     * 同事类
     * @author wxisme
     *
     */
    
    abstract class Colleague {
    	
    	protected Mediator mediator;
    	
    	public void setMediator(Mediator mediator) {
    		this.mediator = mediator;
    	}
    	
    	public abstract void self();
    	public abstract void out();
    
    }
    
    class ConcretColleagueA extends Colleague {
    	
    	public void self() {
    		System.out.println("A doing self things.");
    	}
    	
    	public void out() {
    		System.out.println("A cooperate with other Colleague.");
    	}
    	
    }
    
    class ConcretColleagueB extends Colleague {
    	
    	public void self() {
    		System.out.println("B doing self things.");
    	}
    	
    	public void out() {
    		System.out.println("B cooperate with other Colleague.");
    	}
    	
    }
    
    public class Client {
    	public static void main(String[] args) {
    		Mediator mediator = new ConcreteMediator();
    		
    		Colleague colleagueA = new ConcretColleagueA();
    		mediator.addColleague("ConcretColleagueA", colleagueA);
    		Colleague colleagueB = new ConcretColleagueB();
    		mediator.addColleague("ConcretColleagueB", colleagueB);
    		
    		colleagueA.self();
    		colleagueA.out();
    		System.out.println("Finish job");
    		colleagueB.self();
    		colleagueB.out();
    		System.out.println("Finish job");
    		
    		
    	}
    }
    

     运行结果:

    A doing self things.
    A cooperate with other Colleague.
    Finish job
    B doing self things.
    B cooperate with other Colleague.
    Finish job

  • 相关阅读:
    VUE(vue对象的简单属性)
    使用Dockerfile封装Django镜像
    Django路由小知识
    字符编码小知识
    python值的引用传递和go语言的值传递
    centos输入正确的账号和密码登陆不进去
    迅为4412开发平台Zigbee模块在物联网智能家居中的应用
    全新升级4412开发板项目学习实战资料
    迅为4418开发板平台应用于智能门禁系统
    【分享】iTOP-iMX6UL开发板驱动看门狗 watchdog 以及 Linux-c 测试例程
  • 原文地址:https://www.cnblogs.com/wxisme/p/4546723.html
Copyright © 2020-2023  润新知