松垮垮 松垮垮
首页
  • GPU并行编程
  • 图形学
  • 归并算法
  • 计算机视觉
  • css
  • html
  • JavaScript
  • vue
  • 压缩命令
  • cmdline
  • Docker
  • ftrace跟踪技术
  • gcov代码覆盖率测试
  • GDB
  • git
  • kgdb
  • linux操作
  • markdown
  • systemtap
  • valgrind
  • 设计模式
  • 分布式
  • 操作系统
  • 数据库
  • 服务器
  • 网络
  • C++
  • c语言
  • go
  • JSON
  • Makefile
  • matlab
  • OpenGL
  • python
  • shell
  • 正则表达式
  • 汇编
  • GPU并行编程
  • mysql
  • nginx
  • redis
  • 网络
  • 计算机视觉
  • 进程管理
  • linux调试
  • 【Python】:re.error bad escape i at position 4
  • 搭建ai知识助手
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

松垮垮

c++后端开发工程师
首页
  • GPU并行编程
  • 图形学
  • 归并算法
  • 计算机视觉
  • css
  • html
  • JavaScript
  • vue
  • 压缩命令
  • cmdline
  • Docker
  • ftrace跟踪技术
  • gcov代码覆盖率测试
  • GDB
  • git
  • kgdb
  • linux操作
  • markdown
  • systemtap
  • valgrind
  • 设计模式
  • 分布式
  • 操作系统
  • 数据库
  • 服务器
  • 网络
  • C++
  • c语言
  • go
  • JSON
  • Makefile
  • matlab
  • OpenGL
  • python
  • shell
  • 正则表达式
  • 汇编
  • GPU并行编程
  • mysql
  • nginx
  • redis
  • 网络
  • 计算机视觉
  • 进程管理
  • linux调试
  • 【Python】:re.error bad escape i at position 4
  • 搭建ai知识助手
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 压缩命令
  • cmdline
  • Docker
  • ftrace跟踪技术
  • gcov代码覆盖率测试
  • GDB
  • git
  • kgdb
  • linux操作
  • markdown
  • systemtap
  • valgrind
  • 设计模式

    • Adapter(适配器模式、包装模式)
    • Bridge桥接模式
    • Builder建造者模式
    • Chain
    • Command命令模式
    • Composite组合模式
    • Decorator装饰者
    • Façade门面模式(外观模式)
    • Flyweight享元模式
    • Interpreter解释器模式
    • Iterator迭代器
    • Mediator中介者模式(仲裁者模式)
    • Memento备忘录模式
    • Observer观察者模式
    • Prototype原型
    • Proxy代理模式
    • Singleton单例模式
    • State状态模式
    • Strategy战略
    • Template
    • Visitor访问者模式
    • •Abstract
    • •Factory
    • 依赖注入(控制反转IoC)
    • 概论
    • 设计模式
  • 工具和开发
  • 设计模式
songkuakua
2025-02-15

Façade门面模式(外观模式)

# Façade门面模式(外观模式)

Owner: -QVQ-

结构型的软件设计模式,它提供了统一的接口去访问多个子系统的接口

举个例子,一个餐馆里有许多角色,每个角色就是一个子系统,餐馆就是总系统,客人来餐馆只需要按要求点餐,不需要管餐馆是怎么运作的。

例如:socket函数

优点:

  • 简洁易使用。为复杂的模块和系统提供了一个简单的接口,简易化操作。
  • 保证了子系统独立性。子系统间独立性良好,彼此间一般不受影响,如何使用由门面决定。
  • 保证了系统稳定性。当直接使用子系统,可能会出现无法预知的异常时,门面模式可通过高层接口规范子系统接口的调用,且有效阻隔子系统和客户端间的交互,进而增强系统鲁棒性。
  • 隐秘性好。门面将子系统的具体细节都封装了起来。

缺点:

  1. 不符合开闭原则。添加新系统要对门面进行修改。
  2. 对开发者要求高。开发者需要了解子系统间的业务逻辑关系,这样才能确保封装的高层接口是有效且稳定的。

代码:

//Facade.h
/****************************************************/
#pragma once
#include <iostream>
#include <list>
#include <vector>
#include <string>
 
using namespace std;
 
// 厨师
class Cook
{
public:
	// 炒菜
	void cookMeal(vector<string> menu) {
		for (int i = 0; i < menu.size(); ++i) {
			cout << "正在炒:" << menu[i] << endl;
		}
	}
};
 
// 服务员
class Waiter
{
public:
	// 点菜
	void orderDishes(vector<string> menu) {
		for (int i = 0; i < menu.size(); ++i) {
			cout << "点菜:" << menu[i] << endl;
		}
	}
 
	// 收拾
	void clear() {
		cout << "打扫卫生。" << endl;
	}
 
};
 
// 前台
class Reception
{
public:
	// 欢迎
	void welcome() {
		cout << "欢迎光临!" << endl;
	}
 
	// 买单
	void bill() {
		cout << "买单完成,欢迎下次再来!" << endl;
	}
 
};
 
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

门面就像操作系统一样封装了对用户的使用


// 门面
class Facade
{
public:
	// 构造函数
	Facade() {
		m_cook = new Cook();
		m_waiter = new Waiter();
		m_reception = new Reception();
	}
 
	// 析构函数
	~Facade() {
		if (m_cook != nullptr) {
			delete m_cook;
			m_cook = nullptr;
		}
		if (m_waiter != nullptr) {
			delete m_waiter;
			m_waiter = nullptr;
		}
		if (m_reception != nullptr) {
			delete m_reception;
			m_reception = nullptr;
		}
	}
 
	// 经营
	void manage(vector<string> menu) {
		// 欢迎
		m_reception->welcome();
 
		// 服务员点菜
		m_waiter->orderDishes(menu);
 
		// 厨师炒菜
		m_cook->cookMeal(menu);
 
		// 客人用餐
		cout << "客人用餐中。" << endl;
 
		// 买单
		m_reception->bill();
 
		// 打扫卫生
		m_waiter->clear();
	}
 
private:
	Cook *m_cook;
	Waiter *m_waiter;
	Reception *m_reception;
};
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

用户:

Facade *facade = new Facade();
	// 餐馆运营
	vector<string> menu = { "红烧肉","土豆丝","酸菜鱼" };
	facade->manage(menu);
1
2
3
4
上次更新: 2025/02/21, 14:57:10
Decorator装饰者
Flyweight享元模式

← Decorator装饰者 Flyweight享元模式→

最近更新
01
搭建ai知识助手
02-23
02
边缘检测
02-15
03
css
02-15
更多文章>
Theme by Vdoing | Copyright © 2025-2025 松垮垮 | MIT License | 蜀ICP备2025120453号 | 川公网安备51011202000997号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 纯净模式