标签:security 构造 eric png 用户 ext blog 函数 data
介绍
为子系统中的一组接口提供一个一致的界面,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
标签:security 构造 eric png 用户 ext blog 函数 data
原文地址:http://www.cnblogs.com/taotaodetuer/p/6182889.html