惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
U
Unit 42
博客园 - 【当耐特】
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
有赞技术团队
有赞技术团队
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
美团技术团队
雷峰网
雷峰网
小众软件
小众软件
G
Google Developers Blog
GbyAI
GbyAI
Jina AI
Jina AI
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
F
Fortinet All Blogs
Vercel News
Vercel News

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(7) Facade 外观模式
Tristan(GuoZhijian) · 2008-03-09 · via 博客园 - Tristan(GuoZhijian)

VS 2008

客户端代码需要与多个子系统进行交互,通常完成一个功能需要一系列复杂的请求。此时,可以提供一个外观类简化客户端代码的请求。
外观模式为子系统提供不能的访问接口,并隐藏子系统的内部实现。

1. 模式UML图

2. 应用

    一个网站系统中,SiteInfo类封装了网站的一般信息,VersionInfo类封装了网站的版本信息。
    客户代码需要同时得到网站信息包括版本信息。根据实际情况,SiteInfo类和VersionInfo类可能提供很多各式各样的访问接口。而目前客户代码只需要取得少量信息,于是,为了避免让客户代码与SiteInfo类和VersionInfo类直接打交道。使用外观模式,提供一个SiteFacade。

SiteInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Facade.BLL {
    
public class SiteInfo {

        
public string GetSiteName() {
            
return "遇见未来";
        }

    }

}

VersionInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Facade.BLL {
    
public class VersionInfo {

        
public string GetCurrentVersion() {
            
return "1.0.0";
        }

    }

}

SiteFacade.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Facade.BLL {
    
public class SiteFacade {
        
private SiteInfo siteInfo;
        
private VersionInfo versionInfo;

        
private SiteInfo GetSiteInfo() {
            
if (siteInfo == null{
                siteInfo 
= new SiteInfo();
            }

            
return siteInfo;
        }

        
private VersionInfo GetVersionInfo() {
            
if (versionInfo == null{
                versionInfo 
= new VersionInfo();
            }

            
return versionInfo;
        }


        
public string GetInfo() {
            
return this.GetSiteInfo().GetSiteName() + "," + this.GetVersionInfo().GetCurrentVersion();
        }

    }

}

Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Facade.BLL;

namespace DesignPattern.Facade {
    
class Program {
        
static void Main(string[] args) {

            
string info = new SiteFacade().GetInfo();
            Console.WriteLine(info);
        }

    }

}

Output