程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 設計模式(C#) - 中介者模式(Mediator Pattern)

設計模式(C#) - 中介者模式(Mediator Pattern)

編輯:關於C#

返回“設計模式(C#)系列文章索引”

介紹

用一個中介對象來封裝一系列的對象交互。中介者使各對象不需要顯式地相互引用,從而使其耦合松散,而且可以獨立地改變它們之間的交互。

示例

有一個Message實體類,某個對象對它的操作有Send()和Insert()方法,現在用一個中介對象來封裝這一系列的對象交互。

MessageModel

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Mediator
{
  /**//// <summary>
  /// Message實體類
  /// </summary>
  public class MessageModel
  {
    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="msg">Message內容</param>
    /// <param name="pt">Message發布時間</param>
    public MessageModel(string msg, DateTime pt)
    {
      this._message = msg;
      this._publishTime = pt;
    }

    private string _message;
    /**//// <summary>
    /// Message內容
    /// </summary>
    public string Message
    {
      get { return _message; }
      set { _message = value; }
    }

    private DateTime _publishTime;
    /**//// <summary>
    /// Message發布時間
    /// </summary>
    public DateTime PublishTime
    {
      get { return _publishTime; }
      set { _publishTime = value; }
    }
  }
}

AbstractMessageMediator

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Mediator
{
  /**//// <summary>
  /// 抽象中介者(Mediator)
  /// </summary>
  public abstract class AbstractMessageMediator
  {
    /**//// <summary>
    /// 注冊一個操作Message的對象
    /// </summary>
    /// <param name="AbstractMessage">AbstractMessage</param>
    public abstract void Register(AbstractMessage AbstractMessage);

    /**//// <summary>
    /// 發送Message
    /// </summary>
    /// <param name="from">來自UserId</param>
    /// <param name="to">發送到UserId</param>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public abstract string Send(string from, string to, MessageModel mm);
  }
}

MessageMediator

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Mediator
{
  /**//// <summary>
  /// 中介者(ConcreteMediator)
  /// </summary>
  public class MessageMediator : AbstractMessageMediator
  {
    private Dictionary<string, AbstractMessage> _dictionary = new Dictionary<string, AbstractMessage>();

    /**//// <summary>
    /// 注冊一個操作Message的對象
    /// </summary>
    /// <param name="abstractMessage">AbstractMessage</param>
    public override void Register(AbstractMessage abstractMessage)
    {
      if (!_dictionary.ContainsKey(abstractMessage.UserId))
      {
        _dictionary.Add(abstractMessage.UserId, abstractMessage);
      }

      abstractMessage.AbstractMessageMediator = this;
    }

    /**//// <summary>
    /// 發送Message
    /// </summary>
    /// <param name="from">來自UserId</param>
    /// <param name="to">發送到UserId</param>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public override string Send(string from, string to, MessageModel mm)
    {
      AbstractMessage abstractMessage = _dictionary[to];
      if (abstractMessage != null)
      {
        return abstractMessage.Insert(from, mm);
      }
      else
      {
        return null;
      }
    }
  }
}

AbstractMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Mediator
{
  /**//// <summary>
  /// 操作Message抽象類(Colleague)
  /// </summary>
  public abstract class AbstractMessage
  {
    private AbstractMessageMediator _abstractMessageMediator;
    private string _userId;

    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="userId">UserId</param>
    public AbstractMessage(string userId)
    {
      this._userId = userId;
    }

    /**//// <summary>
    /// UserId
    /// </summary>
    public string UserId
    {
      get { return _userId; }
    }

    /**//// <summary>
    /// 中介者
    /// </summary>
    public AbstractMessageMediator AbstractMessageMediator
    {
      get { return _abstractMessageMediator; }
      set { _abstractMessageMediator = value; }
    }

    /**//// <summary>
    /// 發送Message(由客戶端調用)
    /// </summary>
    /// <param name="to">發送到UserId</param>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public string Send(string to, MessageModel mm)
    {
      return _abstractMessageMediator.Send(_userId, to, mm);
    }

    /**//// <summary>
    /// 接受Message(由中介者調用)
    /// </summary>
    /// <param name="from">來自UserId</param>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public abstract string Insert(string from, MessageModel mm);
  }
}

SqlMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Mediator
{
  /**//// <summary>
  /// Sql方式操作Message(ConcreteColleague)
  /// </summary>
  public class SqlMessage : AbstractMessage
  {
    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="userId">UserId</param>
    public SqlMessage(string userId)
      : base(userId)
    {

    }

    /**//// <summary>
    /// 接受Message(由中介者調用)
    /// </summary>
    /// <param name="from">來自UserId</param>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public override string Insert(string from, MessageModel mm)
    {
      return "Sql方式插入Message(" + from + "發送給" + base.UserId + ")"
        + " - 內容:" + mm.Message
        + " - 時間:" + mm.PublishTime.ToString();
    }
  }
}

XmlMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Mediator
{
  /**//// <summary>
  /// Xml方式操作Message(ConcreteColleague)
  /// </summary>
  public class XmlMessage : AbstractMessage
  {
    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="userId">UserId</param>
    public XmlMessage(string userId)
      : base(userId)
    {

    }

    /**//// <summary>
    /// 接受Message(由中介者調用)
    /// </summary>
    /// <param name="from">來自UserId</param>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public override string Insert(string from, MessageModel mm)
    {
      return "Xml方式插入Message(" + from + "發送給" + base.UserId + ")"
        + " - 內容:" + mm.Message
        + " - 時間:" + mm.PublishTime.ToString();
    }
  }
}

Test

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Pattern.Mediator;

public partial class Mediator : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    AbstractMessageMediator messageMediator = new MessageMediator();

    AbstractMessage user1 = new SqlMessage("user1");
    AbstractMessage user2 = new SqlMessage("user2");
    AbstractMessage user3 = new XmlMessage("user3");
    AbstractMessage user4 = new XmlMessage("user4");

    messageMediator.Register(user1);
    messageMediator.Register(user2);
    messageMediator.Register(user3);
    messageMediator.Register(user4);

    Response.Write(user1.Send("user2", new MessageModel("你好!", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(user2.Send("user1", new MessageModel("我不好!", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(user1.Send("user2", new MessageModel("不好就不好吧。", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(user3.Send("user4", new MessageModel("吃了嗎?", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(user4.Send("user3", new MessageModel("沒吃,你請我?", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(user3.Send("user4", new MessageModel("不請。", DateTime.Now)));
    Response.Write("<br />");
  }
}

運行結果

Sql方式插入Message(user1發送給user2) - 內容:你好! - 時間:2007-5-19 23:43:19

Sql方式插入Message(user2發送給user1) - 內容:我不好! - 時間:2007-5-19 23:43:19

Sql方式插入Message(user1發送給user2) - 內容:不好就不好吧。 - 時間:2007-5-19 23:43:19

Xml方式插入Message(user3發送給user4) - 內容:吃了嗎? - 時間:2007-5-19 23:43:19

Xml方式插入Message(user4發送給user3) - 內容:沒吃,你請我? - 時間:2007-5-19 23:43:19

Xml方式插入Message(user3發送給user4) - 內容:不請。 - 時間:2007-5-19 23:43:19

參考

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

OK

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved