乐在其中设计模式(C#) - 外观模式(Facade Pattern)
原文:乐在其中设计模式(C#) - 外观模式(Facade Pattern)
作者:webabcd
介绍
为子系统中的一组接口提供一个一致的界面,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()[].PublishTime.ToString());
Response.Write("<br />");
m = new Message("admin");
Response.Write(m.Get()[].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
[源码下载]
乐在其中设计模式(C#) - 外观模式(Facade Pattern)的更多相关文章
- 二十四种设计模式:外观模式(Facade Pattern)
外观模式(Facade Pattern) 介绍为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.示例有一个Message实体类,某对象对它 ...
- python : 设计模式之外观模式(Facade Pattern)
#为啥要用外观模式举例说明 这个例子很形象,直接从人家博客上贴过来的,参考链接在下面 不知道大家有没有比较过自己泡茶和去茶馆喝茶的区别,如果是自己泡茶需要自行准备茶叶.茶具和开水,如图1(A)所示,而 ...
- 【UE4 设计模式】外观模式 Facade Pattern
概述 描述 外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.外观模式又称为门面模式,它是一 ...
- 设计模式系列之外观模式(Facade Pattern)——提供统一的入口
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 使用C# (.NET Core) 实现适配器模式 (Adapter Pattern) 和外观模式 (Facade Pattern)
本文的概念内容来自深入浅出设计模式一书 现实世界中的适配器(模式) 我带着一个国标插头的笔记本电脑, 来到欧洲, 想插入到欧洲标准的墙壁插座里面, 就需要用中间这个电源适配器. 面向对象的适配器 你有 ...
- 8.4 GOF设计模式三: 外观模式 Facade
GOF设计模式三: 外观模式 Facade “现有系统”功能强大.复杂,开发“新系统”需要用到其中一部分,但又要增加一部 分新功能,该怎么办?4.1 Facade Pattern: Key Fea ...
- 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...
- 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)
原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...
- 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)
原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...
随机推荐
- java访问mysql方法数据库
1.下载界面包mysql-connector-java-5.0.8-bin.jar 下载链接 2.程序 (1)载入驱动 (2)编程连接操作 (3)返回结果处理 编程演示样例 import java.s ...
- Wix学习整理(2)——HelloWorld安装添加UI
原文:Wix学习整理(2)--HelloWorld安装添加UI 在前一篇随笔Wix学习整理(1)——快速入门HelloWorld中,我们制作的安装包安装界面太简单,没有与用户进行交互的过程.下面我们修 ...
- 百度富文本编辑器UEditor1.3上传图片附件等
今天一直在整我的一个项目的编辑器上传图片,我用的是百度UEditor 1.3版本号的:如今已经有了1.4的了,只是还算比較新吧,可是官网上面没有上传图片这些的教程,而网上对于这方面的资料非常少啊,折腾 ...
- 【mysql】关于子查询的一个例子
假设表my_tbl包含三个字段a,b,c:现在需要查询表中列a的每个不同值下的列b为最小值的记录量. 比如表记录为: a b c 1 3 'cd' 2 3 'nhd' 1 5 'bg' ...
- CMS垃圾回收机制
详解CMS垃圾回收机制 原创不易,未经允许,不得转载~~~ 什么是CMS? Concurrent Mark Sweep. 看名字就知道,CMS是一款并发.使用标记-清除算法的gc. CMS是针对老 ...
- wampserver图标黄色
wampserver图标黄色(多个httpd.exe服务,以前装了apache) 服务--->httpd.exe右击这个服务打开文件位置就知道是不是wampserver的服务.如果不是就停掉这给 ...
- Java重写方法与初始化的隐患(转)
原文出处: Shawon 虽然文章标题是Java, 但几乎所有面向对象设计的语言都遵守这个初始化流程, 感谢廖祜秋liaohuqiu_秋百万指出, 之前忘记提这个了. 前言 drakeet写了个和Re ...
- OCP读书笔记(19) - 数据库空间管理
传输表空间:将linux下的数据库中的test表空间传输到windows平台下的数据库 在传输表空间前,先确定一下源库与目标数据库字符集一致: select * from nls_database_p ...
- 【spring源代码分析】--Bean的解析与注冊
接着上一节继续分析,DefaultBeanDefinitionDocumentReader的parseBeanDefinitions方法: protected void parseBeanDefini ...
- Hibernate4 : 持久化你的第一个类
由于目前我在学校的一个实验室跟老师学习Java EE开发,老师用的是Seam框架接活做项目,所以这一系列的文章将会向Seam方向写..路线大致应该是 : JSP --> Servlet --&g ...