外觀模式

📢 本文由 gemini-3-flash-preview 翻譯

Facade Pattern 物件結構型模式

意圖

為子系統中的一組介面提供一個一致的界面,Facade 模式定義了一個高層介面,這個介面使得這一子系統更加容易使用。

結構

外觀模式

其中:

  • Facade 知道哪些子系統類別負責處理請求;將用戶的請求代理給適當的子系統物件。
  • Subsystem classes 實作子系統的功能;處理由 Facade 物件指派的任務;沒有 Facade 的任何相關資訊,即沒有指向 Facade 的指標。

適用性

Facade 模式適用於:

  • 要為一個複雜子系統提供一個簡單介面時,子系統往往因為不斷演化而變得越來越複雜。大多數模式使用時都會產生更多更小的類別,這使得子系統更具有可重用性,也更容易對子系統進行客製化,但也給那些不需要客製化子系統的用戶帶來一些使用上的困難。Facade 可以提供一個簡單的預設視圖,這一視圖對大多數用戶來說已經足夠,而那些需要更多客製化性的用戶可以越過 Facade 層。
  • 用戶程式與抽象類別的實作部分之間存在著很大的相依性。引入 Facade 將這個子系統與用戶以及其他的子系統分離,可以提高子系統的獨立性和可移植性。
  • 當需要構建一個階層結構的子系統時,使用 Facade 模式定義子系統中每層的進入點。如果子系統之間是相互相依的,則可以讓它們僅通過 Facade 進行通訊,從而簡化了它們之間的相依關係。

例子 1

以醫院為例,就醫時患者需要與醫院不同的職能部門互動,完成掛號、門診、取藥等操作。為簡化就醫流程,可以設置一個接待員的職位,代患者完成上述就醫步驟,患者只需要與接待員互動即可。

Facade-醫院例子-類圖

Java 程式碼

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.*;

interface Patient {
    public String getName();
}

interface Disposer {
    public void dispose(Patient patient);
}

class Registry implements Disposer { // 掛號
    public void dispose(Patient patient) {
        System.out.println("I am registering..." + patient.getName());
    }
}

class Doctor implements Disposer { // 醫生門診
    public void dispose(Patient patient) {
        System.out.println("I am diagnosing..." + patient.getName());
    }
}

class Pharmacy implements Disposer { // 取藥
    public void dispose(Patient patient) {
        System.out.println("I am medicine... " + patient.getName());
    }
}

class Facade {
    private Patient patient;

    public Facade(Patient patient) {
        this.patient = patient;
    }

    void dispose() {
        Registry registry = new Registry();
        Doctor doctor = new Doctor();
        Pharmacy pharmacy = new Pharmacy();

        registry.dispose(patient);
        doctor.dispose(patient);
        pharmacy.dispose(patient);
    }
}

class ConcretePatient implements Patient {
    private String name;

    public ConcretePatient(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class FacadeTest {
    public static void main(String[] args) {
        Patient patient = new ConcretePatient("zst");
        Facade f = new Facade(patient);
        
        f.dispose();
    }
}

C++ 程式碼

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>

using namespace std;

class Patient {
public:
    virtual string getName() = 0;
};

class Disposer {
public:
    virtual void dispose(Patient *patient) = 0;
};

class Registry : public Disposer { // 掛號
public:
    void dispose(Patient *patient) {
        cout << "I am registering...." << patient->getName() << endl;
    }
};

class Doctor : public Disposer { // 醫生門診
public:
    void dispose(Patient *patient) {
        cout << "I am diagnosing...." << patient->getName() << endl;
    }
};

class Pharmacy : public Disposer { // 取藥
public:
    void dispose(Patient *patient) {
        cout << "I am giving medicine...." << patient->getName() << endl;
    }
};

class Facade {
private:
    Patient *patient;

public:
    Facade(Patient *patient) { this->patient = patient; }

    void dispose() {
        Registry *registry = new Registry();
        Doctor *doctor = new Doctor();
        Pharmacy *pharmacy = new Pharmacy();

        registry->dispose(patient);
        doctor->dispose(patient);
        pharmacy->dispose(patient);
    }
};

class ConcretePatient : public Patient {
private:
    string name;

public:
    ConcretePatient(string name) { this->name = name; }

    string getName() { return name; }
};

int main() {
    Patient *patient = new ConcretePatient("zst");
    Facade *f = new Facade(patient);

    f->dispose();

    return 0;
}

例子 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class FacadePattern {
    public static void main(String[] args) {
        Facade f = new Facade();

        f.methodA();
    }
}

class Facade{
    SubSystemOne s1;
    SubSystemTwo s2;
    SubSystemThree s3;

    public Facade(){
        s1 = new SubSystemOne();
        s2 = new SubSystemTwo();
        s3 = new SubSystemThree();
    }

    public void methodA(){
        s1.Operation();
    }

    public void methodB(){
        s2.Operation();
    }

    public void methodC(){
        s3.Operation();
    }
}

class SubSystemOne{
    public void Operation(){
        System.out.println("SubSystemOne");
    }
}

class SubSystemTwo{
    public void Operation(){
        System.out.println("SubSystemTwo");
    }
}

class SubSystemThree{
    public void Operation(){
        System.out.println("SubSystemThree");
    }
}