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月前
|
设计模式 缓存 安全
【设计模式】单例模式:确保类只有一个实例
【设计模式】单例模式:确保类只有一个实例
56 0
|
5月前
|
设计模式 算法 Java
行为型设计模式-策略模式(Strategy Pattern)
行为型设计模式-策略模式(Strategy Pattern)
|
23天前
|
设计模式 算法 数据库连接
PHP中的设计模式:提高代码的可维护性与扩展性本文旨在探讨PHP中常见的设计模式及其应用,帮助开发者编写出更加灵活、可维护和易于扩展的代码。通过深入浅出的解释和实例演示,我们将了解如何使用设计模式解决实际开发中的问题,并提升代码质量。
在软件开发过程中,设计模式是一套经过验证的解决方案模板,用于处理常见的软件设计问题。PHP作为流行的服务器端脚本语言,也有其特定的设计模式应用。本文将重点介绍几种PHP中常用的设计模式,包括单例模式、工厂模式和策略模式,并通过实际代码示例展示它们的具体用法。同时,我们还将讨论如何在实际项目中合理选择和应用这些设计模式,以提升代码的可维护性和扩展性。
49 4
|
9天前
|
设计模式 SQL 安全
PHP中的设计模式:单例模式的深入探索与实践在PHP开发领域,设计模式是解决常见问题的高效方案集合。它们不是具体的代码,而是一种编码和设计经验的总结。单例模式作为设计模式中的一种,确保了一个类仅有一个实例,并提供一个全局访问点。本文将深入探讨单例模式的基本概念、实现方式及其在PHP中的应用。
单例模式在PHP中的应用广泛,尤其在处理数据库连接、日志记录等场景时,能显著提高资源利用率和执行效率。本文从单例模式的定义出发,详细解释了其在PHP中的不同实现方法,并探讨了使用单例模式的优势与注意事项。通过对示例代码的分析,读者将能够理解如何在PHP项目中有效应用单例模式。
|
26天前
|
设计模式 数据库连接 PHP
PHP中的设计模式:如何提高代码的可维护性与扩展性在软件开发领域,PHP 是一种广泛使用的服务器端脚本语言。随着项目规模的扩大和复杂性的增加,保持代码的可维护性和可扩展性变得越来越重要。本文将探讨 PHP 中的设计模式,并通过实例展示如何应用这些模式来提高代码质量。
设计模式是经过验证的解决软件设计问题的方法。它们不是具体的代码,而是一种编码和设计经验的总结。在PHP开发中,合理地使用设计模式可以显著提高代码的可维护性、复用性和扩展性。本文将介绍几种常见的设计模式,包括单例模式、工厂模式和观察者模式,并通过具体的例子展示如何在PHP项目中应用这些模式。
|
21天前
|
设计模式
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
这篇文章详细解释了工厂模式,包括简单工厂、工厂方法和抽象工厂三种类型。每种模式都通过代码示例展示了其应用场景和实现方法,并比较了它们之间的差异。简单工厂模式通过一个工厂类来创建各种产品;工厂方法模式通过定义一个创建对象的接口,由子类决定实例化哪个类;抽象工厂模式提供一个创建相关或依赖对象家族的接口,而不需要明确指定具体类。
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
|
23天前
|
设计模式 SQL 安全
PHP中的设计模式:单例模式的深入探索与实践在PHP的编程实践中,设计模式是解决常见软件设计问题的最佳实践。单例模式作为设计模式中的一种,确保一个类只有一个实例,并提供全局访问点,广泛应用于配置管理、日志记录和测试框架等场景。本文将深入探讨单例模式的原理、实现方式及其在PHP中的应用,帮助开发者更好地理解和运用这一设计模式。
在PHP开发中,单例模式通过确保类仅有一个实例并提供一个全局访问点,有效管理和访问共享资源。本文详细介绍了单例模式的概念、PHP实现方式及应用场景,并通过具体代码示例展示如何在PHP中实现单例模式以及如何在实际项目中正确使用它来优化代码结构和性能。
34 2
|
21天前
|
设计模式 Java
设计模式--适配器模式 Adapter Pattern
这篇文章介绍了适配器模式,包括其基本介绍、工作原理以及类适配器模式、对象适配器模式和接口适配器模式三种实现方式。
|
5月前
|
设计模式 算法
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
68 1
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
|
4月前
|
设计模式 安全 Java
Java中的单例模式是一种设计模式,它保证一个类只有一个实例,并提供一个全局访问点
Java单例模式确保类仅有一个实例,并提供全局访问点。常见实现包括: - 饿汉式:静态初始化,线程安全。 - 懒汉式:延迟初始化,需同步保证线程安全。 - 双重检查锁定:优化懒汉式,减少同步开销。 - 静态内部类:延迟加载,线程安全。 - 枚举:简洁线程安全,不适用于复杂构造。 - 容器实现:如Spring框架,用于依赖注入。选择依据需求,如延迟加载、线程安全和扩展性。
64 10