设计模式之 - 外观模式 (Facade design pattern)

1. 模式意图: 

为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更容易使用。

2. 结构





3. 工厂方法模式C#实现

 

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

namespace Facade
{
    class Program
    {
        static void Main(string[] args)
        {
            Facade facade = new Facade();
            facade.MethodA();          //由于Facade的作用,客户端可以根本不知道SubSystemOne,SubSystemTwo等。
            Console.ReadLine();
        }
    }
    class Facade
    {
        SubSystemOne one;
        SubSystemTwo two;
        SubSystemThree three;
        public Facade()
        {
            one = new SubSystemOne();
            two = new SubSystemTwo();
            three = new SubSystemThree();
        }
        public void MethodA()
        {
            Console.WriteLine("方法组A()----");
            one.MethodOne();
            two.MethodTwo();
            three.MethodThree();
        }
    }
    class SubSystemOne
    {
        public void MethodOne()
        {
            Console.WriteLine("子系统方法一");
        }
    }
    class SubSystemTwo
    {
        public void MethodTwo()
        {
            Console.WriteLine("子系统方法二");
        }
    }
    class SubSystemThree
    {
        public void MethodThree()
        {
            Console.WriteLine("子系统方法三");
        }
    }
}


4. 应用场景 / 适用性

 

当你要为一个复杂子系统提供一个简单接口时; 
客户程序与抽象类的实现部分之间存在着很大的依赖性; 
你需要构建一个层次结构的子系统时,使用facade 模式定义子系统中每层的入口点. 


5. 外观模式的优点:

1. 它对客户屏蔽子系统组件,因而减少了客户处理对象的数目并使得子系统使用起来更方便。

2. 实现了子系统与客户之间的松耦合关系,而子系统内部的功能组件往往是紧耦合的。松耦合关系使得子系统的组件变化不会影响到它的客户。

3. 有利于简化系统在不同平台之间的移植过程,因为编译一个子系统一般不需要编译所有其他的子系统。

 

References:

http://zh.wikipedia.org/wiki/%E5%B7%A5%E5%8E%82%E6%96%B9%E6%B3%95

《大话设计模式》

google.com


posted @ 2013-07-18 19:27  坚固66  阅读(328)  评论(0编辑  收藏  举报