Home 外观模式
Post
Cancel

外观模式

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");
    }
}
This post is licensed under CC BY 4.0 by the author.

装饰器模式

享元模式