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

简介:

一、中介者模式简介(Brief Introduction

中介者模式(Mediator Pattern),定义一个中介对象来封装系列对象之间的交互。中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互。 Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently

中介者减少了各个同事对象的耦合,使得可以独立地改变和复用各个同事对象和中介者类;由于把对想如何协作进行了抽象,将中介作为一个独立的概念并将其封装在一个对象中,这样关注的对象就从对象各自本身的行为转移到他们之间的交互上来,也就是站在一个更宏伟的角度去考虑系统。

二、解决的问题(What To Solve

中介者模式一般应用于一组对象以定义良好但是复杂的方法进行通信的场合,以及想定制一个分布在多个类中的行为,而不想生成太多的子类的场合。

三、中介者模式分析(Analysis

1、中介者模式结构

Mediator:抽象中介者,定义了同事对象交互的接口。

ConcreteMediator:具体中介者对象,实现抽象类中的方法,此具体中介者对象需要知道所有具体同事类,并从具体同事接受消息,向具体同事对象发送命令。

Colleague类:抽象同事类。

ConcreteColleague1类,ConcreteColleague2:具体同事类,实现抽象同事类中的方法。每一个同时类需要知道中介者对象;每个具体同事类只需要了解自己的行为,而不需要了解其他同事类的情况。

2、源代码

1中介者类Mediator及其具体实现类ConcreteMediator

/// <summary>

/// The 'Mediator' abstract class

/// </summary>

abstract class Mediator

{

    public abstract void Send(string message,Colleague colleague);

}

/// <summary>

/// The 'ConcreteMediator' class

/// </summary>

class ConcreteMediator : Mediator

{

    private ConcreteColleague1 _colleague1;

    private ConcreteColleague2 _colleague2;

    public ConcreteColleague1 Colleague1

    {

        set { _colleague1 = value; }

    }

 

    public ConcreteColleague2 Colleague2

    {

        set { _colleague2 = value; }

    }

    public override void Send(string message,Colleague colleague)

    {

        if (colleague == _colleague1)

        {

            _colleague2.Notify(message);

        }

        else

        {

            _colleague1.Notify(message);

        }

    }

}

 

2、抽象同事类Colleague及其实现类ConcreteColleague1ConcreteColleague2

/// <summary>

/// The 'Colleague' abstract class

/// </summary>

abstract class Colleague

{

    protected Mediator mediator;

    // Constructor

    public Colleague(Mediator mediator)

    {

        this.mediator = mediator;

    }

}

/// <summary>

/// A 'ConcreteColleague' class

/// </summary>

class ConcreteColleague1 : Colleague

{

 

    // Constructor

    public ConcreteColleague1(Mediator mediator)

 

        base(mediator)

    {

    }

    public void Send(string message)

    {

        mediator.Send(message, this);

    }

    public void Notify(string message)

    {

        Console.WriteLine("Colleague1 gets message: "+ message);

    }

}

/// <summary>

/// A 'ConcreteColleague' class

/// </summary>

class ConcreteColleague2 : Colleague

{

    // Constructor

    public ConcreteColleague2(Mediator mediator)

 

        base(mediator)

    {

    }

    public void Send(string message)

    {

        mediator.Send(message, this);

    }

    public void Notify(string message)

    {

        Console.WriteLine("Colleague2 gets message: "+ message);

    }

}

 

3、客户端代码

static void Main(string[] args)

{

    ConcreteMediator m = new ConcreteMediator();

    ConcreteColleague1 c1 = new ConcreteColleague1(m);

    ConcreteColleague2 c2 = new ConcreteColleague2(m);

    m.Colleague1 = c1;

    m.Colleague2 = c2;

    c1.Send("How are you? JamesHao");

    c2.Send("Fine, thanks");

    // Wait for user

    Console.ReadKey();

}

3、程序运行结果

四.中介者模式案例分析(Example

1、场景

实现一个聊天室功能,聊城室就是一个中介者,参与聊天的人就是同事对象,如下图所示

AbstractChatroom:抽象聊天室类,做为Participant的交互的中介。

Register()方法:会员注册功能;Send()方法:发送消息功能。

Chatroom:具体聊天室类,实现抽象聊天室类中的方法。

Participant:参与者类,主要有发送消息Send()功能和接受消息Receive()功能。

Beatle类,NonBeatle:参与者类的具体实现,实现父类Paticipant类中的方法。

2、代码

1、抽象聊天室类AbstractChatroom及其具体聊天室Chatroom

/// <summary>

/// The 'Mediator' abstract class

/// </summary>

abstract class AbstractChatroom

{

    public abstract void Register(Participant participant);

    public abstract void Send(string from, string to, string message);

}

/// <summary>

/// The 'ConcreteMediator' class

/// </summary>

class Chatroom : AbstractChatroom

{

    private Dictionary<stringParticipant> _participants =newDictionary<stringParticipant>();

    public override void Register(Participant participant)

    {

        if (!_participants.ContainsValue(participant))

        {

            _participants[participant.Name] = participant;

        }

        participant.Chatroom = this;

 

    }

    public override void Send(string from, string to, string message)

    {

        Participant participant = _participants[to];

        if (participant != null)

        {

            participant.Receive(from, message);

        }

    }

}

 

2、参与者Participant类及其实现BeatleNonBeatle

/// <summary>

/// The 'AbstractColleague' class

/// </summary>

class Participant

{

    private Chatroom _chatroom;

    private string _name;

    // Constructor

    public Participant(string name)

    {

        this._name = name;

    }

    // Gets participant name

    public string Name

    {

        get { return _name; }

    }

    // Gets chatroom

    public Chatroom Chatroom

    {

        set { _chatroom = value; }

        get { return _chatroom; }

    }

    // Sends message to given participant

    public void Send(string to, string message)

    {

        _chatroom.Send(_name, to, message);

    }

    // Receives message from given participant

    public virtual void Receive(string from, string message)

    {

        Console.WriteLine("{0} to {1}: '{2}'",from, Name, message);

    }

}

/// <summary>

/// A 'ConcreteColleague' class

/// </summary>

class Beatle : Participant

{

    // Constructor

    public Beatle(string name)

        base(name)

    {

    }

    public override void Receive(string from, string message)

    {

        Console.Write("To a Beatle: ");

        base.Receive(from, message);

    }

}

/// <summary>

/// A 'ConcreteColleague' class

/// </summary>

class NonBeatle : Participant

{

    // Constructor

    public NonBeatle(string name)

        base(name)

    {

    }

    public override void Receive(string from, string message)

    {

        Console.Write("To a non-Beatle: ");

        base.Receive(from, message);

    }

}

 

3、客户端代码

static void Main(string[] args)

{

    // Create chatroom

    Chatroom chatroom = new Chatroom();

    // Create participants and register them

    Participant George = new Beatle("George");

    Participant Paul = new Beatle("Paul");

    Participant Ringo = new Beatle("Ringo");

    Participant John = new Beatle("John");

    Participant Yoko = new NonBeatle("Yoko");

 

    chatroom.Register(George);

    chatroom.Register(Paul);

    chatroom.Register(Ringo);

    chatroom.Register(John);

    chatroom.Register(Yoko);

   

    // Chatting participants

    Yoko.Send("John""Hi John!");

    Paul.Send("Ringo""All you need is love");

    Ringo.Send("George""My sweet Lord");

    Paul.Send("John""Can't buy me love");

    John.Send("Yoko""My sweet love");

    // Wait for user

    Console.ReadKey();

}

3、运行结果

五、总结(Summary

中介者模式(Mediator Pattern),定义一个中介对象来封装系列对象之间的交互。中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互。中介者模式一般应用于一组对象以定义良好但是复杂的方法进行通信的场合,以及想定制一个分布在多个类中的行为,而不想生成太多的子类的场合。

参考:http://www.dofactory.com/Patterns/PatternMediator.aspx

    本文转自灵动生活博客园博客,原文链接:http://www.cnblogs.com/ywqu/archive/2010/02/09/1666196.html ,如需转载请自行联系原作者

相关文章
|
1月前
|
设计模式 缓存 安全
【设计模式】单例模式:确保类只有一个实例
【设计模式】单例模式:确保类只有一个实例
20 0
|
3月前
|
设计模式 算法 Java
行为型设计模式-策略模式(Strategy Pattern)
行为型设计模式-策略模式(Strategy Pattern)
|
3月前
|
设计模式 算法
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
39 1
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
|
3月前
|
设计模式 Java 应用服务中间件
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
31 1
|
3月前
|
设计模式 缓存 安全
设计模式 - 创建型模式_ 单例模式 Singleton Pattern
设计模式 - 创建型模式_ 单例模式 Singleton Pattern
39 0
|
9天前
|
设计模式 存储 Java
Java设计模式:解释一下单例模式(Singleton Pattern)。
`Singleton Pattern`是Java中的创建型设计模式,确保类只有一个实例并提供全局访问点。它通过私有化构造函数,用静态方法返回唯一的实例。类内静态变量存储此实例,对外仅通过静态方法访问。
15 1
|
15天前
|
设计模式 SQL 算法
设计模式了解哪些,模版模式
设计模式了解哪些,模版模式
19 0
|
1月前
|
设计模式 Java uml
C++设计模式之 依赖注入模式探索
C++设计模式之 依赖注入模式探索
37 0
|
3月前
|
设计模式 存储 算法
Java 设计模式最佳实践:三、行为模式
Java 设计模式最佳实践:三、行为模式
22 0
|
2月前
|
设计模式 前端开发 JavaScript
观察者模式 vs 发布-订阅模式:两种设计模式的对决!
欢迎来到前端入门之旅!这个专栏是为那些对Web开发感兴趣、刚刚开始学习前端的读者们打造的。无论你是初学者还是有一些基础的开发者,我们都会在这里为你提供一个系统而又亲切的学习平台。我们以问答形式更新,为大家呈现精选的前端知识点和最佳实践。通过深入浅出的解释概念,并提供实际案例和练习,让你逐步建立起一个扎实的基础。无论是HTML、CSS、JavaScript还是最新的前端框架和工具,我们都将为你提供丰富的内容和实用技巧,帮助你更好地理解并运用前端开发中的各种技术。