面板模式(Facade):為子系統中的一組介面提供一個一致的介面,此模式定義了一個高層介面,這個介面使得這一子系統更容易使用。
1、在設計初級階段,應該要有意識的將不同的兩個層分離。
2、層與層之間建立外觀Facade,為複雜的子系統提供一個簡單的介面,使得耦合大大降低。
3、在開發階段,子系統往往因為不斷的重構演化而變得越來越複雜。
4、增加 Facade可以提供一個簡單的介面,減少他們之間的依賴。
Facade代碼 1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Facade
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 Facade facade = new Facade();
12 facade.MethodA(); //由於Facade的作用,用戶端可以根本不知道SubSystemOne,SubSystemTwo等。
13 Console.ReadLine();
14 }
15 }
16 class Facade
17 {
18 SubSystemOne one;
19 SubSystemTwo two;
20 SubSystemThree three;
21 public Facade()
22 {
23 one = new SubSystemOne();
24 two = new SubSystemTwo();
25 three = new SubSystemThree();
26 }
27 public void MethodA()
28 {
29 Console.WriteLine("方法組A()----");
30 one.MethodOne();
31 two.MethodTwo();
32 three.MethodThree();
33 }
34 }
35 class SubSystemOne
36 {
37 public void MethodOne()
38 {
39 Console.WriteLine("子系統方法一");
40 }
41 }
42 class SubSystemTwo
43 {
44 public void MethodTwo()
45 {
46 Console.WriteLine("子系統方法二");
47 }
48 }
49 class SubSystemThree
50 {
51 public void MethodThree()
52 {
53 Console.WriteLine("子系統方法三");
54 }
55 }
56 }