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

简介:

一、中介者模式简介(Brief Intro du ction

中介者模式( 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 :具体中介者对象,实现抽象类中的方法,此具体中介者对象需要知道所有具体同事类,并从具体同事接受消息,向具体同事对象发送命令。
Co ll eague 类:抽象同事类。
ConcreteCo ll eague1 类,ConcreteCo ll eague2 :具体同事类,实现抽象同事类中的方法。每一个同时类需要知道中介者对象;每个具体同事类只需要了解自己的行为,而不需要了解其他同事类的情况。

2、源代码

1 中介者类 Mediator 及其具体实现类 ConcreteMediator
///   <summary>
///  The 'Mediator' abstract class
///   </summary>
abstract  class Mediator
{
    public abstract void  Send(string  message,Co ll eague  co ll eague);
}
///   <summary>
///  The 'ConcreteMediator' class
///   </summary>
class  ConcreteMediator  : Mediator
{
    private ConcreteCo ll eague1  _co ll eague1;
    private ConcreteCo ll eague2  _co ll eague2;
    public ConcreteCo ll eague1  Co ll eague1
     {
        set  { _co ll eague1 = value ; }
     }
 
    public ConcreteCo ll eague2  Co ll eague2
     {
        set  { _co ll eague2 = value ; }
     }
    public override void  Send(string  message,Co ll eague  co ll eague)
     {
        if  (co ll eague == _co ll eague1)
         {
             _co ll eague2.Notify(message);
         }
        else
         {
             _co ll eague1.Notify(message);
         }
     }
}
 
2 、抽象同事类 Co ll eague 及其实现类 ConcreteCo ll eague1 ConcreteCo ll eague2
///   <summary>
///  The 'Co ll eague' abstract class
///   </summary>
abstract  class Co ll eague
{
    protected Mediator  mediator;
    // Constructor
    public  Co ll eague(Mediator  mediator)
     {
        this .mediator = mediator;
     }
}
///   <summary>
///  A 'ConcreteCo ll eague' class
///   </summary>
class  ConcreteCo ll eague1  : Co ll eague
{
 
    // Constructor
    public  ConcreteCo ll eague1(Mediator  mediator)
 
         base (mediator)
     {
     }
    public void  Send(string  message)
     {
         mediator.Send(message, this );
     }
    public void  Notify(string  message)
     {
        Console .WriteLine("Co ll eague1 gets message: " + message);
     }
}
///   <summary>
///  A 'ConcreteCo ll eague' class
///   </summary>
class  ConcreteCo ll eague2  : Co ll eague
{
    // Constructor
    public  ConcreteCo ll eague2(Mediator  mediator)
 
         base (mediator)
     {
     }
    public void  Send(string  message)
     {
         mediator.Send(message, this );
     }
    public void  Notify(string  message)
     {
        Console .WriteLine("Co ll eague2 gets message: " + message);
     }
}
 
3 、客户端代码
static  void  Main (string [] args)
{
    ConcreteMediator  m = new ConcreteMediator ();
    ConcreteCo ll eague 1  c 1 = new ConcreteCo ll eague1 (m);
    ConcreteCo ll eague 2  c 2 = new ConcreteCo ll eague2 (m);
     m.Co ll eague1 = c1;
     m.Co ll eague2 = c2;
     c1.Send("How are you? JamesHao" );
     c2.Send("Fine, t hank s" );
    // Wait for user
    Console .ReadKey();
}

3、程序运行结果











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

目录
相关文章
|
1月前
|
设计模式 供应链 安全
【再谈设计模式】中介者模式 - 协调对象间交互的枢纽
中介者模式定义了一个中介对象来封装一组对象之间的交互方式。中介者使得各对象之间不需要显式地相互引用,从而降低了它们之间的耦合度。它通过将对象之间的交互逻辑集中到中介者对象中,使得系统的结构更加清晰,易于维护和扩展。
53 18
【再谈设计模式】中介者模式 - 协调对象间交互的枢纽
|
6月前
|
设计模式
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
这篇文章详细解释了工厂模式,包括简单工厂、工厂方法和抽象工厂三种类型。每种模式都通过代码示例展示了其应用场景和实现方法,并比较了它们之间的差异。简单工厂模式通过一个工厂类来创建各种产品;工厂方法模式通过定义一个创建对象的接口,由子类决定实例化哪个类;抽象工厂模式提供一个创建相关或依赖对象家族的接口,而不需要明确指定具体类。
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
|
6月前
|
设计模式 Java
设计模式--适配器模式 Adapter Pattern
这篇文章介绍了适配器模式,包括其基本介绍、工作原理以及类适配器模式、对象适配器模式和接口适配器模式三种实现方式。
|
6月前
|
设计模式 Java
Java设计模式-中介者模式(20)
Java设计模式-中介者模式(20)
|
7月前
|
设计模式 前端开发 Java
【十三】设计模式~~~行为型模式~~~中介者模式(Java)
文章详细介绍了中介者模式(Mediator Pattern),这是一种对象行为型模式,用于封装一系列对象的交互,降低系统耦合度,并简化对象之间的交互关系。通过案例分析、结构图、时序图和代码示例,文章展示了中介者模式的组成部分、实现方式和应用场景,并讨论了其优点、缺点和适用情况。
【十三】设计模式~~~行为型模式~~~中介者模式(Java)
|
8月前
|
设计模式 JavaScript
js设计模式【详解】—— 中介者模式
js设计模式【详解】—— 中介者模式
100 5
|
9月前
|
设计模式
设计模式-05建造者模式(Builder Pattern)
设计模式-05建造者模式(Builder Pattern)

热门文章

最新文章