设计模式之结构性模式---外观模式

来源:互联网 发布:淘宝二手iphone店铺 编辑:程序博客网 时间:2024/05/22 12:02

一、外观模式的特点

外观模式的作用是将底层的所有子系统统一成一个接口提供给客户端调用,客户端不必知道底层子系统联系只需要知道调用接口就能够实现功能。

列如做投资的时候不必直接去与股票打交道,通过直接够买上层基金能够赚到钱就行,而底部股票与股票之间的联系‘购买了那些股票,如何操作等我们不必知道。

二、外观模式的实现

1、外观模式的实现模型

2、外观模型代码实现

using System;class SubSystemOne{    public void MethodOne()    {        Console.WriteLine ("A subsystem method");    }}class SubSysetmTow{    public void MenthodTow()    {        Console.WriteLine ("The subsystem method 2");    }}class SubSysetmThree{    public void MenthodThree()    {        Console.WriteLine("Three subsystems method 3");    }}class SubSysetmFour{    public void SubStytemFour()    {        Console.WriteLine ("Four subsystem method");    }}class Pacade{    SubSystemOne one;    SubSysetmTow tow;    SubSysetmThree three;    SubSysetmFour four;    public Pacade()    {        one = new SubSystemOne ();        tow = new SubSysetmTow ();        three = new SubSysetmThree ();        four = new SubSysetmFour ();    }    public void MethodA()    {        Console.WriteLine ("A combination of");        one.MethodOne ();        tow.MenthodTow ();        three.MenthodThree ();        four.SubStytemFour ();    }    public void MenthodB()    {        Console.WriteLine ("Combination of two");        one.MethodOne ();        tow.MenthodTow ();    }}    class MainClass    {        public static void Main (string[] args)        {            Console.WriteLine ("Hello World!");        Pacade pacade = new Pacade ();        pacade.MethodA ();        pacade.MenthodB ();        Console.ReadKey ();        }    }

运行结果:

0 0