`

MediatorPattern

阅读更多

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

 

比如:有两个人,张三,李四,它们交互非常频繁,那么这两个对象之间耦合性比较强,那么改其中一个对象会对另一个对象产生直接的影响,如果在张三和李四之间加了另外一个中介者,张三和李四之间的交互完全通过中介者来传递,假设张三向和李四说一句话,那么张三就和中介者去说这句话,接着中介者把这句话传到李四这一边,同样李四也是重复这样一个过程.总之中介者是连接各个对象中介的一个角色,也就是说各个对象交互要通过中介者来进行交互,不能互相直接的进行交互.

 

适用性
1.一组对象以定义良好,但是复杂的方式进行通信.产生的相互依赖关系结构混乱且难以理解.

2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象.

3.想定制一个分部在多个类中的行为,而又不想生成太多的子类.

 

构成:
1.抽象中介者(Mediator):定义一个接口用于各同事对象(Colleague)之间通信.

2.具体中介者(ConcreteMediator):协调各个同事对象实现协作的行为,掌握并且维护它的各个同事对象的引用.

3.同事类(Colleague):每一个同事类对象都引用一个中介者对象,每一个同事对象在需要和其他同事对象通信时,就与它的中介者通信.

 

ClassDiagram:

 

SequenceDiagram:

 

//聊天室示例
    class Client
    {
        static void Main(string[] args)
        {
            ChatRoom c = new ChatRoom();

            Participant zhangsan = new ConcreteParticipantA("zhangsan");
            Participant lisi = new ConcreteParticipantB("lisi");
            Participant wangwu = new ConcreteParticipantA("wangwu");

            c.Register(zhangsan);
            c.Register(lisi);
            c.Register(wangwu);

            zhangsan.Send("lisi","hello");
            lisi.Send("wangwu","world");
            wangwu.Send("zhangsan","helloworld");
            Console.ReadKey();
        }
    }

    /// <summary>
    /// 抽象中介者类
    /// </summary>
    interface IChatRoom
    {
        void Register(Participant participant);
        void Send(string from, string to, string message);
    }

    /// <summary>
    /// 具体中介者---聊天室类
    /// </summary>
    class ChatRoom : IChatRoom
    {
        private Hashtable participants = new Hashtable();

        public void Register(Participant participant)
        {
            if (participants[participant.Name] == null)
            {
                participants[participant.Name] = participant;
            }

            participant.ChatRoom = this;
        }

        public void Send(String from, string to, string message)
        {
            Participant participant = participants[to] as Participant;

            if (null != participant)
            {
                participant.Receive(from, message);
            }
        }
    }

    /// <summary>
    /// 抽象同事类(AbstractColleague)
    /// </summary>
    abstract class Participant
    {
        private ChatRoom chatRoom;
        private string name;

        public Participant(string name)
        {
            this.name = name;
        }

        internal ChatRoom ChatRoom
        {
            get { return chatRoom; }
            set { chatRoom = value; }
        }


        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public void Send(string to, string message)
        {
            //调用中介者对象相应的Send方法.
            chatRoom.Send(name, to, message);
        }

        public virtual void Receive(string from, string message)
        {
            Console.WriteLine("{0} to {1}:{2}", from, name, message);
        }
    }

    /// <summary>
    /// 具体的同事类(ConcreteColleagueA)
    /// </summary>
    class ConcreteParticipantA : Participant
    {
        public ConcreteParticipantA(string name)
            : base(name)
        {

        }

        public override void Receive(string from, string message)
        {
            Console.WriteLine("To:" + this.GetType().Name);
            base.Receive(from, message);
        }
    }

    /// <summary>
    /// 具体的同事类(ConcreteColleagueB)
    /// </summary>
    class ConcreteParticipantB : Participant
    {
        public ConcreteParticipantB(string name)
            : base(name)
        {

        }

        public override void Receive(string from, string message)
        {
            Console.WriteLine("To: " + this.GetType().Name);
            base.Receive(from, message);
        }
    }

 

  • 大小: 35 KB
  • 大小: 39.8 KB
分享到:
评论

相关推荐

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

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

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

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

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

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

    MediatorPattern_Jonathan_Sey

    MediatorPattern_Jonathan_Sey

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

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

    C#版 24种设计模式

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

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

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

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

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

    设计模式代码——c#

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

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

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

    用Java实现23种设计模式

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

    设计模式PPT

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

    java设计模式-组合模式

    设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段...

    32种设计模式

    中介者模式(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#23种设计模式

    │ └─MediatorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─20.OberverPattern │ ├─CatOberverPattern │ │ ├─bin │ │ │ └─Debug │ │ ├...

    常用设计模式的IOS实现源码打包下载

    AbstractFactoryPattern AdapterPattern ...MediatorPattern MementoPattern ObserverPattern PrototypePattern ProxyPattern SingletonPattern StatePattern StrategyPattern TemplatePattern VisitorPattern

    design-pattern-java.pdf

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

    Apress.Pro.Design.Patterns.in.Swift

    The Mediator Pattern Chapter 22. The Observer Pattern Chapter 23. The Memento Pattern Chapter 24. The Strategy Pattern Chapter 25. The Visitor Pattern Chapter 26. The Template Method Pattern Part 5 ...

Global site tag (gtag.js) - Google Analytics