`
wdt1988520
  • 浏览: 14358 次
社区版块
存档分类
最新评论

中介者模式(Mediator Pattern)

 
阅读更多

中介者模式又称调停者模式,属行为模式。

 用一个中介者来封装多个对象之间负责的交互行为,中介者对象使各交互对象之间不需显示的声明相互引用,从而使对象之间松耦合,而且可以独立的改变他们之间的交互行为。

 

 

 

角色分析:

     Mediator(抽象中介者):

        定义同事类之间的通讯接口。

    ConcreteMediator(具体中介者):

        实现同事类之间的通讯接口,并维护它对各个同事类的引用。

     Colleague(抽象同事类):

       定义同事类的公共方法

    ConcreteColleague(具体同事类):

       具体同事类为抽象同事类的子类,每一个同事类都会引用一个中介者对象,同事类之间的通讯,先告知中介者,由中介者完成通讯过程。

 

使用场景:

       1、多个对象之间存现相互的引用。

       2、一个对象对多个对象之间有通信,可将通信行为放入中介者,从而将那个对象独立出来。

 

例:网上简易聊天室

package mediator;


/**
 *create by datao.wang 2014-2-10-上午10:40:32	
 *抽象中介者  聊天房间
 */
public abstract class AbstractChatroom {
    //注册成员
	public abstract void regiser(Member member);
	//发送消息
	public abstract void sendText(String from,String to,String message);
}


package mediator;

import java.util.HashMap;
import java.util.Map;


/**
 *create by datao.wang 2014-2-10-上午10:45:57	
 *具体中介者
 */
public class Chatroom extends AbstractChatroom {
    //引用同事对象
	private Map<String,Member> map=new HashMap<String,Member>();
	@Override
	public void regiser(Member member) {
		if(map.get(member.getName())==null){
			map.put(member.getName(),member);
		}
	}

	@Override
	public void sendText(String from,String to,String message) {
		Member member=map.get(to);
		if(member!=null){
			member.receiveMessage(from,message);
		}
	}

}


package mediator;
/**
 *create by datao.wang 2014-2-10-上午10:48:58	
 * 抽象同事角色
 */
public abstract class Member {
	//对中介者的引用
	public AbstractChatroom chatroom;
  
  	public  String name;
  	
  	public abstract void receiveMessage(String from,String message);
  	
	public abstract void sendMessage(String to,String message);

	public AbstractChatroom getChatroom() {
		return chatroom;
	}
	
	public void setChatroom(AbstractChatroom chatroom) {
		this.chatroom = chatroom;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
  
}


package mediator;
/**
 *create by datao.wang 2014-2-10-上午11:11:12	
 *具体同事角色
 */
public class ConcreteMember extends Member {

	public ConcreteMember(String name){
		this.name=name;
	}
	@Override
	public void receiveMessage(String from, String message) {
		System.out.println(message+"----------------------");
	}

	@Override
	public void sendMessage(String to, String message) {
		this.chatroom.sendText(this.getName(), to, message);
	}
	
}


 

分享到:
评论

相关推荐

    中介者模式(Mediator Pattern)原理图

    中介者模式(Mediator Pattern)是一种行为型设计模式,用于减少对象之间的直接相互依赖,使得对象间的交互通过一个中介者对象来进行协调。在中介者模式中,对象之间不再直接相互调用,而是通过中介者对象来传递消息...

    设计模式之中介者模式(Mediator Pattern)

    用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

    PHP设计模式之中介者模式(Mediator Pattern)入门与应用案例详解

    咱们先来看下中介者模式(Mediator Pattern)的定义,它就是,用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互,这种模式又称为调停...

    Java设计模式之中介者模式(Mediator Pattern)简介

    主要介绍了Java设计模式之中介者模式(Mediator Pattern),需要的朋友可以参考下

    C#版 24种设计模式

    提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式(Mediator Pattern) 装饰模式...

    php设计模式 Mediator (中介者模式)

    php /** * 中介者模式 * * 用一个中介对象来封装一系列的对象交互,使各对象不需要显式地相互引用从而使其耦合松散,而且可以独立地改变它们之间的交互 */ abstract class Mediator { abstract public function send($...

    用Java实现23种设计模式

    中介者模式(Mediator Pattern) 备忘录模式(Memento Pattern) 观察者模式(Observer Pattern) 状态模式(State Pattern) 空对象模式(Null Object Pattern) 策略模式(Strategy Pattern) 模板模式...

    C#设计模式_设计模式_C#_

    中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)

    C#设计模式(23种设计模式)

    中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式...

    设计模式PPT

     中介者模式(Mediator Pattern)  备忘录模式(Memento Pattern)  观察者模式(Observer Pattern)  状态模式(State Pattern)  策略模式(Strategy Pattern)  模板方法模式(Template Method ...

    设计模式代码——c#

    18. 中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式...

    23种设计模式 (创建型,结构型,行为型)

    中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式...

    33种JAVA设计模式DEMO

    中介者模式(Mediator Pattern) 备忘录模式(Memento Pattern) 观察者模式(Observer Pattern) 状态模式(State Pattern) 空对象模式(Null Object Pattern) 策略模式(Strategy Pattern) 模板模式(Template ...

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列...中介者模式(Mediator) 备忘录模式(Memento) 观察者模式(Observer) 状态模式(State) 策略模式(Strategy) 模板方法模式(Template Method) 访问者模式(Visitor)

    32种设计模式

    中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23...

    中介者模式

    中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。中介者模式属于行为型模式。

    design-pattern-java.pdf

    遍历聚合对象中的元素——迭代器模式(四) 遍历聚合对象中的元素——迭代器模式(五) 遍历聚合对象中的元素——迭代器模式(六) 中介者模式-Mediator Pattern 协调多个对象之间的交互——中介者模式(一) 协调多...

    Android编程设计模式之中介者模式详解

    中介者模式(Mediator Pattern)也称为调解者模式或调停者模式,Mediator本身就有调停者和调解者的意思。 在日常生活中调停者或调解者这个角色我们见得比较多的是“和事老”,也就是说调解两个有争端的人的角色,举...

Global site tag (gtag.js) - Google Analytics