返回“設計模式(C#)系列文章索引”
介紹
為子系統中的一組接口提供一個一致的界面,Facade模式定義了一個高層接口,這個接口使得這一子系統更加容易使用。
示例
有一個Message實體類,某對象對它的操作有Get()方法,另外還有一個對象有一個Validate()方法來判斷用戶是否有權限。現在提供一個高層接口來封裝這兩個方法。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Facade
{
/**//// <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; }
}
}
}
User
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Facade
{
/**//// <summary>
/// 用戶相關類
/// </summary>
public class User
{
/**//// <summary>
/// 驗證用戶是否合法
/// </summary>
/// <param name="userId">UserId</param>
/// <returns></returns>
public bool Validate(string userId)
{
if (userId == "admin")
{
return true;
}
else
{
return false;
}
}
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Facade
{
/**//// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage
{
/**//// <summary>
/// 獲取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("SQL方式獲取Message", DateTime.Now));
return l;
}
}
}
Message
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Facade
{
/**//// <summary>
/// Facade類
/// </summary>
public class Message
{
private string _userId = "";
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="userId">UserId</param>
public Message(string userId)
{
this._userId = userId;
}
/**//// <summary>
/// 獲取Message
/// 首先使用User類的Validate()方法驗證用戶是否合法
/// 然後使用SqlMessage類的Get()方法獲取Message
/// </summary>
/// <returns></returns>
public List<MessageModel> Get()
{
User u = new User();
SqlMessage m = new SqlMessage();
if (u.Validate(_userId))
{
return m.Get();
}
else
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("無權獲取", DateTime.Now));
return l;
}
}
}
}
client
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.Facade;
public partial class Facade : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Message m = new Message("user");
Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString());
Response.Write("<br />");
m = new Message("admin");
Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString());
Response.Write("<br />");
}
}
運行結果
無權獲取 2007-3-20 22:16:50
SQL方式獲取Message 2007-3-20 22:16:50
參考
http://www.dofactory.com/Patterns/PatternFacade.aspx
OK