原文:乐在其中设计模式(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)的更多相关文章

  1. 二十四种设计模式:外观模式(Facade Pattern)

    外观模式(Facade Pattern) 介绍为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.示例有一个Message实体类,某对象对它 ...

  2. python : 设计模式之外观模式(Facade Pattern)

    #为啥要用外观模式举例说明 这个例子很形象,直接从人家博客上贴过来的,参考链接在下面 不知道大家有没有比较过自己泡茶和去茶馆喝茶的区别,如果是自己泡茶需要自行准备茶叶.茶具和开水,如图1(A)所示,而 ...

  3. 【UE4 设计模式】外观模式 Facade Pattern

    概述 描述 外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.外观模式又称为门面模式,它是一 ...

  4. 设计模式系列之外观模式(Facade Pattern)——提供统一的入口

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  5. 使用C# (.NET Core) 实现适配器模式 (Adapter Pattern) 和外观模式 (Facade Pattern)

    本文的概念内容来自深入浅出设计模式一书 现实世界中的适配器(模式) 我带着一个国标插头的笔记本电脑, 来到欧洲, 想插入到欧洲标准的墙壁插座里面, 就需要用中间这个电源适配器. 面向对象的适配器 你有 ...

  6. 8.4 GOF设计模式三: 外观模式 Facade

    GOF设计模式三: 外观模式 Facade  “现有系统”功能强大.复杂,开发“新系统”需要用到其中一部分,但又要增加一部 分新功能,该怎么办?4.1 Facade Pattern: Key Fea ...

  7. 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

    原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...

  8. 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

    原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...

  9. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

随机推荐

  1. 巧妙使用Firebug插件,快速监控网站打开缓慢的原因

    原文 巧妙使用Firebug插件,快速监控网站打开缓慢的原因 很多用户会问,我的网站首页才50KB,打开网页用了近60秒才打开?如何解释? 用户抱怨服务器运行缓慢,w3wp.exe 出现 CPU 10 ...

  2. ABP中动态WebAPI原理解析

    ABP中动态WebAPI原理解析 动态WebAPI应该算是ABP中最Magic的功能之一了吧.开发人员无须定义继承自ApiController的类,只须重用Application Service中的类 ...

  3. jQuery Mobile 是创建移动 web 应用程序的框架。

    jQuery Mobile jQuery Mobile 是创建移动 web 应用程序的框架. jQuery Mobile 适用于所有流行的智能手机和平板电脑. jQuery Mobile 使用 HTM ...

  4. Windows下安装MySQLdb, Python操作MySQL数据库的增删改查

    这里的前提是windows上已经安装了MySQL数据库,且配置完成,能正常建表能操作. 在此基础上仅仅需安装MySQL-python-1.2.4b4.win32-py2.7.exe就ok了.仅仅有1M ...

  5. FUDCon - FedoraProject

    FUDCon - FedoraProject FUDCon: Fedora Users and Developers Conference FUD: An acronym for Fear, Unce ...

  6. 使用Nexus搭建企业maven仓库(二)

    先阅读<使用Nexus搭建企业maven仓库(一)> http://blog.csdn.net/ouyida3/article/details/40747545 1.官网眼下最新的版本号是 ...

  7. Cocos2d-x发展---更改父的颜色、透明度的子节点上

    标题手段:当我们改变父节点或透明时的颜色.默认是不会影响孩子的节点. 作为交换组看到朋友说可以通过设置相关的参数变化的子节点来实现属性的效果,看了看源代码,记录下来:        引擎版本号为:2. ...

  8. Android应用开发-小巫CSDN博客clientJsoup篇

    Android应用开发-小巫CSDN博客clientJsoup篇 距上一篇博客已经过去了两个星期,小巫也认为很抱歉,由于在忙着做另外一个项目,差点儿抽不出空来,这不小巫会把剩下的博文全部在国庆补上.本 ...

  9. Web监听器导图详解(转)

    阅读目录 Web监听器 监听器的分类 Servlet版本与Tomcat版本 getAttribute与getParameter的区别 参考 监听器是JAVA Web开发中很重要的内容,其中涉及到的知识 ...

  10. Android点滴---ViewHolder通用,优雅写法

    近期在做项目时,又要写 ViewHolder. 突然想到网上看看有没什么好的写法! 不知道你是不是也烦透了写那些没有技术含量的ViewHolder 看看这些.也许会有收获! 然后就找到了以下两篇文章( ...