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

简介:
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 <string Participant > _participants =new Dictionary <string Participant >();
    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 类及其实现 Beatle NonBeatle
/// <summary>
///  The 'AbstractColl eague' 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 'ConcreteColl eague' 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 'ConcreteColl eague' 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、运行结果
中介者模式( Mediator Pattern ),定义一个中介对象来封装系列对象之间的交互。中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互。中介者模式一般应用于一组对象以定义良好但是复杂的方法进行通信的场合,以及想定制一个分布在多个类中的行为,而不想生成太多的子类的场合。









本文转自 灵动生活 51CTO博客,原文链接:http://blog.51cto.com/smartlife/277270,如需转载请自行联系原作者

目录
相关文章
|
5天前
|
设计模式
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
这篇文章详细解释了工厂模式,包括简单工厂、工厂方法和抽象工厂三种类型。每种模式都通过代码示例展示了其应用场景和实现方法,并比较了它们之间的差异。简单工厂模式通过一个工厂类来创建各种产品;工厂方法模式通过定义一个创建对象的接口,由子类决定实例化哪个类;抽象工厂模式提供一个创建相关或依赖对象家族的接口,而不需要明确指定具体类。
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
|
5天前
|
设计模式 Java
设计模式--适配器模式 Adapter Pattern
这篇文章介绍了适配器模式,包括其基本介绍、工作原理以及类适配器模式、对象适配器模式和接口适配器模式三种实现方式。
|
6天前
|
设计模式 Java
Java设计模式-中介者模式(20)
Java设计模式-中介者模式(20)
|
2月前
|
设计模式 前端开发 Java
【十三】设计模式~~~行为型模式~~~中介者模式(Java)
文章详细介绍了中介者模式(Mediator Pattern),这是一种对象行为型模式,用于封装一系列对象的交互,降低系统耦合度,并简化对象之间的交互关系。通过案例分析、结构图、时序图和代码示例,文章展示了中介者模式的组成部分、实现方式和应用场景,并讨论了其优点、缺点和适用情况。
【十三】设计模式~~~行为型模式~~~中介者模式(Java)
|
3月前
|
设计模式 JavaScript
js设计模式【详解】—— 中介者模式
js设计模式【详解】—— 中介者模式
57 5
|
4月前
|
设计模式
中介者模式-大话设计模式
中介者模式-大话设计模式
|
4月前
|
设计模式
设计模式-05建造者模式(Builder Pattern)
设计模式-05建造者模式(Builder Pattern)
|
4月前
|
设计模式 Java 程序员
Java设计模式之中介者模式详解
Java设计模式之中介者模式详解
|
3天前
|
设计模式 数据库连接 PHP
PHP中的设计模式:提升代码的可维护性与扩展性在软件开发过程中,设计模式是开发者们经常用到的工具之一。它们提供了经过验证的解决方案,可以帮助我们解决常见的软件设计问题。本文将介绍PHP中常用的设计模式,以及如何利用这些模式来提高代码的可维护性和扩展性。我们将从基础的设计模式入手,逐步深入到更复杂的应用场景。通过实际案例分析,读者可以更好地理解如何在PHP开发中应用这些设计模式,从而写出更加高效、灵活和易于维护的代码。
本文探讨了PHP中常用的设计模式及其在实际项目中的应用。内容涵盖设计模式的基本概念、分类和具体使用场景,重点介绍了单例模式、工厂模式和观察者模式等常见模式。通过具体的代码示例,展示了如何在PHP项目中有效利用设计模式来提升代码的可维护性和扩展性。文章还讨论了设计模式的选择原则和注意事项,帮助开发者在不同情境下做出最佳决策。
|
18天前
|
设计模式 算法 安全
设计模式——模板模式
模板方法模式、钩子方法、Spring源码AbstractApplicationContext类用到的模板方法
设计模式——模板模式