1,定義
外觀模式:為子系統(tǒng)的一組接口提供一個(gè)一致的界面捷兰,此模式定義了一個(gè)高層接口立叛,這個(gè)接口使得這一子系統(tǒng)更加容易使用
2,UML圖
外觀模式
3贡茅,基本代碼
namespace 外觀模式
{
class Program
{
static void Main(string[] args)
{
Facade facade = new Facade();
facade.MethodA();
facade.MethodB();
Console.Read();
}
}
class SubSystemOne
{
public void MethodOne()
{
Console.WriteLine(" 子系統(tǒng)方法一");
}
}
class SubSystemTwo
{
public void MethodTwo()
{
Console.WriteLine(" 子系統(tǒng)方法二");
}
}
class SubSystemThree
{
public void MethodThree()
{
Console.WriteLine(" 子系統(tǒng)方法三");
}
}
class SubSystemFour
{
public void MethodFour()
{
Console.WriteLine(" 子系統(tǒng)方法四");
}
}
class Facade
{
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();
four.MethodFour();
}
public void MethodB()
{
Console.WriteLine("\n方法組B() ---- ");
two.MethodTwo();
three.MethodThree();
}
}
}
4秘蛇,簡(jiǎn)單應(yīng)用
5,思考
1顶考,注意子類中沒有Facade的任何引用赁还。
2,在設(shè)計(jì)的初期階段驹沿,應(yīng)該有意識(shí)的將不同的層分離艘策,層與層之間建立facade
3,在開發(fā)階段渊季,子系統(tǒng)往往因?yàn)椴粩嗟闹貥?gòu)演化而變得越來越復(fù)雜朋蔫,添加外觀facade可以提供一個(gè)簡(jiǎn)單的接口,減少他們之間的依賴梭域,
4斑举,為新系統(tǒng)提供一個(gè)外觀facade類,來提供設(shè)計(jì)粗糙或高度復(fù)雜的遺留代碼的比較清晰簡(jiǎn)單的接口病涨,讓新系統(tǒng)與facade交互富玷,facade與遺留代碼交互復(fù)雜的工作
5,體現(xiàn)了依賴倒轉(zhuǎn)原則和迪米特法則