[Design Patterns] 02. Structural Patterns - Facade Pattern
前言
参考资源
只把常用的五星的掌握即可。
外观模式-Facade Pattern【学习难度:★☆☆☆☆,使用频率:★★★★★】
深入浅出外观模式(一):外观模式概述,外观模式结构与实现
深入浅出外观模式(二):外观模式应用实例(文件加密模块)
深入浅出外观模式(三):抽象外观类,外观模式效果与适用场景
系统越来越复杂,但不想让客户看到,对外仍然体现出“简洁”。
教学
一、基本原理
提供了一名“服务员”统一办理这些歌事情。

二、类图
类关系表示
类关系参见: https://www.cnblogs.com/jesse123/p/4279555.html
- Association:
将一个类的对象作为另一个类的属性;
- 菱形尾巴 + Association:
[Aggregation]:集合体,但也只是集合,弱关系 ----> has-a,老师和学生的关系,学生可以属于多位老师;
[Composition]:组合体,必不可少部分,强关系 ----> contain-a,皇帝和妃子的关系,妃子只能属于一位皇帝;
- 实虚结合 + 三角 (类) 箭头:
[Generalization]:实现继承;
[Realization]:实现接口;
- 虚化的 Association:
[Dependency]:仅使用了对方的 “方法”;
Aggregation 与 Composition
要点:
如果是“必要的”,那么就需要在类内“create”了呢。
如果只是“弱弱地使用”,那么“部分“ 可以在外部创建,再以参数形式传入 "整体” 使用。

三、外观模式
外观模式 UML

外观模式 Code
要点:虽然使用了其他类(SubSystem),但属于“类的属性”,并不是在方法内。
class SubSystemA
{
public void MethodA()
{
//业务实现代码
}
} class SubSystemB
{
public void MethodB()
{
//业务实现代码
}
} class SubSystemC
{
public void MethodC()
{
//业务实现代码
}
} class Facade
{
# 类只是作为了属性,所以属于 Association
private SubSystemA obj1 = new SubSystemA();
private SubSystemB obj2 = new SubSystemB();
private SubSystemC obj3 = new SubSystemC(); public void Method()
{
obj1.MethodA();
obj2.MethodB();
obj3.MethodC();
}
} class Program
{
static void Main(string[] args)
{
Facade facade = new Facade();
facade.Method();
}
}
Facade Pattern in Python
一、虚基类
abc.ABCMeta 是一个metaclass,用于在Python程序中创建抽象基类。
@abstractmethod 装饰器:其实就是模仿“虚函数”。
详见:[Advanced Python] 15 - Metaclass for ORM
class Server(metaclass=ABCMeta):
@abstractmethod
def __init__(self):
pass
def __str__(self):
return self.name
@abstractmethod
def boot(self):
pass
@abstractmethod
def kill(self, restart=True):
pass
钩子类,实际操作的细节实现。
class FileServer(Server):
def __init__(self):
'''初始化文件服务进程要求的操作'''
self.name = 'FileServer'
self.state = State.new
def boot(self):
print('booting the {}'.format(self))
'''启动文件服务进程要求的操作'''
self.state = State.running
def kill(self, restart=True):
print('Killing {}'.format(self))
'''杀死文件服务进程要求的操作'''
self.state = State.restart if restart else State.zombie
def create_file(self, user, name, permissions):
'''检查访问权限的有效性、用户权限,等等'''
print("trying to create the file '{}' for user '{}' with permissions {}".format(name, user, permissions))
class ProcessServer(Server):
def __init__(self):
'''初始化进程服务进程要求的操作'''
self.name = 'ProcessServer'
self.state = State.new
def boot(self):
print('booting the {}'.format(self))
'''启动进程服务进程要求的操作'''
self.state = State.running
def kill(self, restart=True):
print('Killing {}'.format(self))
'''杀死进程服务进程要求的操作'''
self.state = State.restart if restart else State.zombie
def create_process(self, user, name):
'''检查用户权限、生成PID,等等'''
print("trying to create the process '{}' for user '{}'".format(name, user))
在外观类中,初始化的部分,挂上类钩子;对外API函数,调用这些类的函数钩子。
class OperatingSystem:
'''外观'''
def __init__(self):
self.fs = FileServer()
self.ps = ProcessServer()
def start(self):
[i.boot() for i in (self.fs, self.ps)]
def create_file(self, user, name, permissions):
return self.fs.create_file(user, name, permissions)
def create_process(self, user, name):
return self.ps.create_process(user, name)
def main():
os = OperatingSystem()
# 以下都是内部比较繁琐的实现
os.start()
os.create_file('foo', 'hello', '-rw-r-r')
os.create_process('bar', 'ls /tmp')
if __name__ == '__main__':
main()
End.
[Design Patterns] 02. Structural Patterns - Facade Pattern的更多相关文章
- javascript / PHP [Design Patterns - Facade Pattern]
This pattern involves a single class which provides simplified methods required by client and delega ...
- 乐在其中设计模式(C#) - 外观模式(Facade Pattern)
原文:乐在其中设计模式(C#) - 外观模式(Facade Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 外观模式(Facade Pattern) 作者:webabcd 介绍 ...
- 【设计模式】结构型03外观模式(Facade Pattern)
[设计模式]结构型02装饰模式(Decorator Pattern) 意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 主要解决:降低访问 ...
- 设计模式(八): 从“小弟”中来类比"外观模式"(Facade Pattern)
在此先容我拿“小弟”这个词来扯一下淡.什么是小弟呢,所谓小弟就是可以帮你做一些琐碎的事情,在此我们就拿“小弟”来类比“外观模式”.在上面一篇博文我们完整的介绍了“适配器模式”,接下来我们将要在这篇博客 ...
- 深入浅出设计模式——外观模式(Facade Pattern)
模式动机引入外观角色之后,用户只需要直接与外观角色交互,用户与子系统之间的复杂关系由外观角色来实现,从而降低了系统的耦合度. 模式定义外观模式(Facade Pattern):外部与一个子系统的通信必 ...
- 二十四种设计模式:外观模式(Facade Pattern)
外观模式(Facade Pattern) 介绍为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.示例有一个Message实体类,某对象对它 ...
- 使用C# (.NET Core) 实现适配器模式 (Adapter Pattern) 和外观模式 (Facade Pattern)
本文的概念内容来自深入浅出设计模式一书 现实世界中的适配器(模式) 我带着一个国标插头的笔记本电脑, 来到欧洲, 想插入到欧洲标准的墙壁插座里面, 就需要用中间这个电源适配器. 面向对象的适配器 你有 ...
- C#设计模式之十外观模式(Facade Pattern)【结构型】
一.引言 快12点半了,要开始今天的写作了.很快,转眼设计模式已经写了十个了,今天我们要讲[结构型]设计模式的第五个模式,该模式是[外观模式],英文名称是:Facade Pattern.我们先从名字上 ...
- 外观模式Facade pattern
http://www.runoob.com/design-pattern/facade-pattern.html 外观模式 外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一 ...
随机推荐
- curl请求https资源的时候出现400
在nginx上配置了一个新的域名, 习惯性地用curl请求看看有没有配置错误 因为是https的, 所以 $curl 'https://test.test.com/' -x 127.0.0.1:443 ...
- Cloudera Certified Associate Administrator案例之Install篇
Cloudera Certified Associate Administrator案例之Install篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.创建主机模板(为了给主 ...
- Linux跑脚本用sh和./有什么区别?
一个很有意思的例子: sh是一个shell.运行sh a.sh,表示我使用sh来解释这个脚本:如果我直接运行./a.sh,首先你会查找脚本第一行是否指定了解释器,如果没指定,那么就用当前系统默认的sh ...
- Mybatis分页方法
使用方法https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md 使用 Maven 在 pom ...
- danci3
permit 英 [pə'mɪt] 美 [pɚ'mɪt] vi. 许可:允许 vt. 许可:允许 n. 许可证,执照 encapsulate 英 [ɪn'kæpsjʊleɪt; en-] 美 [ɪn' ...
- 3、Python的IDE之Jupyter的使用
一.Jupyter介绍 Jupyter Notebook 的本质是一个 Web 应用程序,便于创建和共享文学化程序文档,支持实时代码,数学方程,可视化和 markdown.用途包括:数据清理和转换,数 ...
- 图书检索系统C版本
原创,转载请注明出处! 程序具有一下功能窗口界面1,Input输入(读入文件,所有的文件都读入)2,Output输出(检验是否读取正确,从结构体数组读入)3,Length统计(此文件里有110本图书) ...
- learning java NIO 之 CharBuffer
import java.nio.CharBuffer; public class BufferTest { public static void main(String[] args) { CharB ...
- C++对象内存布局,this指针,对象作为参数,作为返回值
class TestClass { public: void setNum(int num) { m_num1 = num; } int getNum() { return m_num1; } pri ...
- 安利一个github上面的一个神级库thefuck,Linux命令敲错了,没关系,自动纠正你的命令
没错就是这么神奇,名字相当噶性,thefuck.当你命令输入错误不要怕,直接来一句fuck,自动纠正你输入的命令. 在你输入错误的命令的时候,忍俊不禁的想来一句fuck,没错你不仅可以嘴上说,命令里面 ...