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,如需转载请自行联系原作者

目录
相关文章
|
Go
在golang中发起http请求以获取访问域名的ip地址实例(使用net, httptrace库)
这只是追踪我们的行程的简单方法,不过希望你跟着探险家的脚步,即使是在互联网的隧道中,也可以找到你想去的地方。接下来就是你的探险之旅了,祝你好运!
646 26
|
设计模式 Java 程序员
【设计模式】【行为型模式】中介者模式(Mediator)
一、入门 什么是中介者模式? 中介者模式(Mediator Pattern)是一种行为设计模式,旨在减少对象之间的直接依赖,通过引入一个中介者对象来协调多个对象之间的交互。这种模式特别适用于对象间存在
327 9
|
设计模式 Java 数据安全/隐私保护
Java 设计模式:装饰者模式(Decorator Pattern)
装饰者模式属于结构型设计模式,允许通过动态包装对象的方式为对象添加新功能,提供比继承更灵活的扩展方式。该模式通过组合替代继承,遵循开闭原则(对扩展开放,对修改关闭)。
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
这篇文章详细解释了工厂模式,包括简单工厂、工厂方法和抽象工厂三种类型。每种模式都通过代码示例展示了其应用场景和实现方法,并比较了它们之间的差异。简单工厂模式通过一个工厂类来创建各种产品;工厂方法模式通过定义一个创建对象的接口,由子类决定实例化哪个类;抽象工厂模式提供一个创建相关或依赖对象家族的接口,而不需要明确指定具体类。
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
|
设计模式 SQL 安全
PHP中的设计模式:单例模式的深入探索与实践在PHP开发领域,设计模式是解决常见问题的高效方案集合。它们不是具体的代码,而是一种编码和设计经验的总结。单例模式作为设计模式中的一种,确保了一个类仅有一个实例,并提供一个全局访问点。本文将深入探讨单例模式的基本概念、实现方式及其在PHP中的应用。
单例模式在PHP中的应用广泛,尤其在处理数据库连接、日志记录等场景时,能显著提高资源利用率和执行效率。本文从单例模式的定义出发,详细解释了其在PHP中的不同实现方法,并探讨了使用单例模式的优势与注意事项。通过对示例代码的分析,读者将能够理解如何在PHP项目中有效应用单例模式。
|
设计模式 Java
设计模式--适配器模式 Adapter Pattern
这篇文章介绍了适配器模式,包括其基本介绍、工作原理以及类适配器模式、对象适配器模式和接口适配器模式三种实现方式。
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
888 0
|
监控 前端开发 API
一款基于 .NET MVC 框架开发、功能全面的MES系统
一款基于 .NET MVC 框架开发、功能全面的MES系统
727 5
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
445 7
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
377 0