外观模式

介绍

外观模式(Facde):属于结构型模式,又称作门面模式。为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用

结构图


四个子系统的类

        class SubSystemOne
        {
            public void MethodOne()
            {
                Console.WriteLine("子系统方法一");
            }
        }

        class SubSystemTwo
        {
            public void MethodTwo()
            {
                Console.WriteLine("子系统方法二");
            }
        }

        class SubSystemThree
        {
            public void MethodThree()
            {
                Console.WriteLine("子系统方法三");
            }
        }

        class SubSystemFour
        {
            public void MethodFour()
            {
                Console.WriteLine("子系统方法四");
            }
        }

外观类

class Facde
        {
            SubSystemOne one;
            SubSystemTwo two;
            SubSystemThree three;
            SubSystemFour four;

            public Facade()
            {
                one =new SubSystemOne();
                two=new SubSystemTwo();
                three=new SubSystemThree();
                four=new SubSystemFour();
            }
            public void MethodA()
            {
                Console.WriteLine("\n方法组A()--- ");
                one.MethodOne();
                two.MethodTwo();
                three.MethodThree();
                four.MethodFour();
            }
            public void MethodB()
            {
                Console.WriteLine("\n方法组B()--- ");
                 three.MethodThree();
                 four.MethodFour();
            }

        }

客户端

 static void Main(string[] args)
        {
            Facde facade =new Facde();

            facade.MethodA();
            facade.MethodB();

            Console.Read();
        }

以大话设计模式中的例子为例

代码结构图

基金类

class Fund
        {
            Stock1 gu1;
            Stock2 gu2;
            Stock3 gu3;
            NationalDebt1 nd1;
            Realty1 rt1;
            public Fund()
            {
                gu1 = new Stock1();
                gu2 = new Stock2();
                gu3 = new Stock3();
                nd1 = new NationalDebt1();
                rt1 = new Realty1();
            }
            public void BuyFund()
            {
                gu1.Buy();
                gu2.Buy();
                gu3.Buy();
                nd1.Buy();
                rt1.Buy();
            }
            public void SellFund()
            {
                gu1.Sell();
                gu2.Sell();
                gu3.Sell();
                nd1.Sell();
                rt1.Sell();
            }
        }

 五个子系统类

 //股票1
        class Stock1
        {
            //卖股票
            public void Sell()
            {
                Console.WriteLine("股票1卖出");
            }
            //买股票
            public void Buy()
            {
                Console.WriteLine("股票1买入");
            }
        }
        //股票2
        class Stock2
        {
            //卖股票
            public void Sell()
            {
                Console.WriteLine("股票2卖出");
            }
            //买股票
            public void Buy()
            {
                Console.WriteLine("股票2买入");
            }
        }
        //股票3
        class Stock3
        {
            //卖股票
            public void Sell()
            {
                Console.WriteLine("股票3卖出");
            }
            //买股票
            public void Buy()
            {
                Console.WriteLine("股票3买入");
            }
        }
        //国债
        class NationalDebt1
        {
            //卖国债
            public void Sell()
            {
                Console.WriteLine("国债卖出");
            }
            //买股票
            public void Buy()
            {
                Console.WriteLine("国债买入");
            }
        }
        //房地产1
        class Realty1
        {
            //卖股票
            public void Sell()
            {
                Console.WriteLine("房地产1卖出");
            }
            //买股票
            public void Buy()
            {
                Console.WriteLine("房地产1买入");
            }
        }

客户端代码

static void Main(string[] args)
        {
            Fund jijin = new Fund();
            //基金购买
            jijin.BuyFund();
            //基金赎回
            Console.Read();
        }

显示图


这样客户就不需要直接与股票打交道 了,而是与专业基金经理打交道,降低了客户与股票的耦合度。客户只需要关注基金的涨幅即可,而不需要观众N多的股票。


优点: 

1.对客户屏蔽了其子系统组件,因而减少了客户处理对象的数目,并使得子系统实用
起来更方便 
2.它实现了子系统与客户之间的松耦合关系,而子系统内部的功能组件往往是紧耦合
的。松耦合关系使得子系统的组件变化不会影响到它的客户。 Facade模式有助于建立层次结构系统,也有助于对对象之间的依赖关系分层。 Facade模式还可以消除复杂的循环依赖关系,这一点在客户程序与子系统是分别实现的时候尤为重要。 
3.如果应用需要,它并不限制它们实用子系统类。因此你可以在系统易用性和通用性之间进行选择。 

缺点:

限制了客户的自由,减少了可变性。

用途:

a) 为一个复杂子系统提供一个简单接口.

b) 提高子系统的独立性.

c) 在层次化结构中,可以使用Facade模式定义系统中每一层的入口。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值