乐在其中设计模式(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 ...
随机推荐
- poj 3270 更换使用
1.确定初始和目标状态. 明确.目标状态的排序状态. 2.得出置换群,.比如,数字是8 4 5 3 2 7,目标状态是2 3 4 5 7 8.能写为两个循环:(8 2 7)(4 3 5). 3.观察当 ...
- 外语学习强烈推荐Rosetta Stone
外语学习强烈推荐Rosetta Stone 外语学习强烈推荐Rosetta Stone
- projecteuler---->problem=9----Special Pythagorean triplet
title: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For e ...
- mysql 数据库备份ubuntu
安装 1 sudo apt-get update 2. sudo apt-get install mysql-server 3 sudo apt-get install mysql-client 4 ...
- FZU2177(dp)
传送门:ytaaa 题意:有n个***(不能调换顺序),可以组成x(x<n)个炸弹,每个炸弹的威力为该组的(max-min)^2,现在给出n个***的威力值,求能组成所有炸弹的最大威力和. 分析 ...
- 开发指南专题4:JEECG高速微云开发平台--JEECG开发环境的搭建
开发指南专题4:JEECG微云高速开发平台开发环境搭建 1. JEECG开发环境搭建 JEECG推荐的开发环境为Myeclipse8.5/Eclipse3.7+JDK1.6+Tomcat6.0 1.1 ...
- Linux中进行挂起(待机)的命令说明
/********************************************************************* * Author : Samson * Date ...
- 西南民大oj(矩阵快速幂)
我的名字不可能那么难记 时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 16 测试通过 : ...
- hdu1243(最长公共子序列变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...
- [WPF]使用Pack URI路径訪问二进制资源
一.路径格式定义 完整的URI定义为: pack://application,,,[/可选程序集名称;][可选版本;][目录名称/]文件名 缩略后的写法是: [目录名称/]文件名 二.在XAML代码中 ...