您的位置:首页 > 其它

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

2016-10-20 10:47 99 查看

一、外观模式的特点

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

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



二、外观模式的实现

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<
4000
/span>[] args)
{
Console.WriteLine ("Hello World!");
Pacade pacade = new Pacade ();
pacade.MethodA ();
pacade.MenthodB ();
Console.ReadKey ();
}
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: