设计模式 外观模式

Gof定义

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

UML图

理解

  外观设计模式又叫门面设计模式,如果你在网上看到门面设计模式,其实都是一样的。感觉外观模式很简单,好像也没什么好讲的。在《大话设计模式》里举了这么一个例子,有一个股民在买股票,面对成千上万种类型的股票不知道怎么选择好,这时如果有一个股票的经理人,他有多年丰富的经验,你把钱给他,他帮你买股票,那么事情就简单多了。这里的那个多年丰富经验的股票经理人就是Facade(门面,外观)。我们通过直接跟股票经理人打交道,而具体要做的事情由股票经理人去处理就行。

Code Example

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Facade f = new Facade();
            f.MethodA();
            f.MethodB();

            Console.ReadLine();
        }
    }

    class Facade
    {
        SystemOne one;
        SystemTwo tow;
        SystemThree three;

        public Facade()
        {
            one = new SystemOne();
            tow = new SystemTwo();
            three = new SystemThree();
        }

        public void MethodA()
        {
            Console.WriteLine("买股票的方案一");
            one.MethodOne();
            three.MethodThree();
        }

        public void MethodB()
        {
            Console.WriteLine("买股票的方案二");
            tow.MethodTwo();
            three.MethodThree();
        }
    }


    class SystemOne
    {
        public void MethodOne()
        {
            Console.WriteLine("买1000股建设银行的");
        }
    }

    class SystemTwo
    {
        public void MethodTwo()
        {
            Console.WriteLine("买2000股中国石油的");
        }
    }

    class SystemThree
    {
        public void MethodThree()
        {
            Console.WriteLine("买100股腾讯的");
        }
    }

}
原文地址:https://www.cnblogs.com/cxeye/p/2694731.html