【编程思想】【设计模式】【结构模式Structural】门面模式/外观模式Facade
Python版
https://github.com/faif/python-patterns/blob/master/structural/facade.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- """
*What is this pattern about?
The Facade pattern is a way to provide a simpler unified interface to
a more complex system. It provides an easier way to access functions
of the underlying system by providing a single entry point.
This kind of abstraction is seen in many real life situations. For
example, we can turn on a computer by just pressing a button, but in
fact there are many procedures and operations done when that happens
(e.g., loading programs from disk to memory). In this case, the button
serves as an unified interface to all the underlying procedures to
turn on a computer. *What does this example do?
The code defines three classes (TC1, TC2, TC3) that represent complex
parts to be tested. Instead of testing each class separately, the
TestRunner class acts as the facade to run all tests with only one
call to the method runAll. By doing that, the client part only needs
to instantiate the class TestRunner and call the runAll method.
As seen in the example, the interface provided by the Facade pattern
is independent from the underlying implementation. Since the client
just calls the runAll method, we can modify the classes TC1, TC2 or
TC3 without impact on the way the client uses the system. *Where is the pattern used practically?
This pattern can be seen in the Python standard library when we use
the isdir function. Although a user simply uses this function to know
whether a path refers to a directory, the system makes a few
operations and calls other modules (e.g., os.stat) to give the result. *References:
https://sourcemaking.com/design_patterns/facade
https://fkromer.github.io/python-pattern-references/design/#facade
http://python-3-patterns-idioms-test.readthedocs.io/en/latest/ChangeInterface.html#facade *TL;DR80
Provides a simpler unified interface to a complex system.
""" from __future__ import print_function
import time SLEEP = 0.1 # Complex Parts
class TC1: def run(self):
print(u"###### In Test 1 ######")
time.sleep(SLEEP)
print(u"Setting up")
time.sleep(SLEEP)
print(u"Running test")
time.sleep(SLEEP)
print(u"Tearing down")
time.sleep(SLEEP)
print(u"Test Finished\n") class TC2: def run(self):
print(u"###### In Test 2 ######")
time.sleep(SLEEP)
print(u"Setting up")
time.sleep(SLEEP)
print(u"Running test")
time.sleep(SLEEP)
print(u"Tearing down")
time.sleep(SLEEP)
print(u"Test Finished\n") class TC3: def run(self):
print(u"###### In Test 3 ######")
time.sleep(SLEEP)
print(u"Setting up")
time.sleep(SLEEP)
print(u"Running test")
time.sleep(SLEEP)
print(u"Tearing down")
time.sleep(SLEEP)
print(u"Test Finished\n") # Facade
class TestRunner: def __init__(self):
self.tc1 = TC1()
self.tc2 = TC2()
self.tc3 = TC3()
self.tests = [self.tc1, self.tc2, self.tc3] def runAll(self):
[i.run() for i in self.tests] # Client
if __name__ == '__main__':
testrunner = TestRunner()
testrunner.runAll() ### OUTPUT ###
# ###### In Test 1 ######
# Setting up
# Running test
# Tearing down
# Test Finished
#
# ###### In Test 2 ######
# Setting up
# Running test
# Tearing down
# Test Finished
#
# ###### In Test 3 ######
# Setting up
# Running test
# Tearing down
# Test Finished
#
Python转载版
【编程思想】【设计模式】【结构模式Structural】门面模式/外观模式Facade的更多相关文章
- headfirst设计模式(8)—适配器模式与外观模式
前言 这一章主要讲2个模式,一个是,适配器模式(负责将一个类的接口适配成用户所期待的),另外一个是外观模式(为子系统提供一个共同的对外接口),看完的第一反应是,为什么要把它们两放在同一章,难道它们有什 ...
- 设计模式GOF23(结构型模式:代理模式,适配模式,桥接模式,组合模式,装饰模式,外观模式,享元模式)
结构型模式: – 分类: • 适配器模式.代理模式.桥接模式.装饰模式.组合模式.外观模式.享元模式 – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题. 结构 ...
- Android设计模式(九)--外观模式
问题:在Android中,Apk能够有微信,QQ为代表的插件式安装更新功能: 那么问题来了,主系统(姑且这么说)调用插件式安装的子系统.由子系统提供对外的訪问.属不属于一种外观模式呢? 先说设计模式: ...
- JAVA设计模式(DESIGN PATTERNS IN JAVA)读书摘要 第1部分接口型模式——第4章 外观(Facade)模式
外观模式就类似于一个工具包,一个类对应一个功能. 外观模式的意图是为子系统提供一个接口,便于它的使用. 书中给出的例子是画一个哑弹的飞行路径, 初始的类的设计是这样的,看下图, ShowFlight类 ...
- 大话设计模式C++达到-文章12章-外观模式
一.UML画画 关键词:添加Facade层. 二.概念 外观模式:为子系统中的一组接口提供一个一致的界面.此模式定义了一个高层接口,这个接口使得这一子系统更加easy使用. 三.说明 Q:外观模式在什 ...
- 《大话设计模式》ruby版代码:外观模式
需求: 股民买卖股票 初步代码: # -*- encoding: utf-8 -*- #股票1 class Stock1 def buy puts '股票1买入' end def sell puts ...
- javascript设计模式(张容铭)学习笔记 - 外观模式绑定事件
有一个需求要为document对象绑定click事件来是想隐藏提示框的交互功能,于是小白写了如下代码: document.onclick = function(e) { e.preventDefaul ...
- 【编程思想】【设计模式】【结构模式Structural】享元模式flyweight
Python版 https://github.com/faif/python-patterns/blob/master/structural/flyweight.py #!/usr/bin/env p ...
- 【java设计模式】【结构模式Structural Pattern】合成模式Composite Pattern
package com.tn.pattern; import java.util.Vector; public class Client { public static void main(Strin ...
- 《精通Python设计模式》学习结构型之外观模式
这个我在工作中也有所应用的. 就是在真正的实现层上面,再封装一个函数的调用的. 这样就可以在内层函数作真正实现, 而外层调用函数对外开放, 隔离内外的变化性. from enum import Enu ...
随机推荐
- (原创)WinForm中莫名其妙的小BUG——ComboBox 尺寸高度问题
一.前言 使用WinForm很久了,多多少少遇到一些小BUG. 这些小BUG影响并不严重,而且只要稍微设置一下就能正常使用,所以微软也一直没有修复这些小BUG. 本来并不足以写篇文章去记录,但是昨天遇 ...
- LeetCode -90. 子集 II C++ (回溯法)
class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) ...
- easyui comboBox的多选框之疑难杂症——逗号篇
提笔写正文之前,首先要再次提醒一下自己,因为总是记不住,以至大神同事们都开始用"嫌弃"的眼光看自己了-- 记得使用easyui中的combobox吗?效果是酱紫滴: ...
- b站个人直播年报【大爽歌作】 介绍与演示
大家好,我是大爽,一个b站UP主兼主播. 最近做了一个b站直播个人年报,该年报为代码文件生成. 且代码已打包到一个可视化工具中(exe)只需两步就可以获得自己的专属年报. 代码已上传到我的github ...
- 体验.NET Core使用IKVM对接Java
前言 与第三方对接最麻烦的是语言不同,因语言不同内置实现相关标准加密算法还是略微有所差异,对接单点登录场景再寻常不过,由于时间紧迫且对接方使用Java,所以留给我对接开发和联调的时间本就不多,于是乎, ...
- [loj3300]组合数问题
将$f(k)=\sum_{i=0}^{m}a_{i}k^{i}$转换为$f(k)=\sum_{i=0}^{m}b_{i}k^{\underline{i}}$,其中$k^{\underline{i}}= ...
- 一文详解MySQL的锁机制
一.表级锁.行级锁.页级锁 数据库锁定机制简单来说,就是数据库为了保证数据的一致性,而使各种共享资源在被并发访问变得有序所设计的一种规则. MySQL数据库由于其自身架构的特点,存在多种数据存储引擎, ...
- springboot项目中常遇到的问题-初学者最容易犯的错
1.在spring中有两个main方法 2.在idea中少提代码类了,或者某类中代码依赖关系没解决掉
- CF1550D Excellent Arrays
考虑每个数一定是这个形式: \(i + x,i - x\) 所以如果我们要构造一个最大的数组. 那一定是这样的形式: 有一半为\(i + x\),有一半\(i - x\) 那么我们发现每个数有这样一个 ...
- LOJ 2555 & 洛谷 P4602 [CTSC2018]混合果汁(二分+主席树)
LOJ 题目链接 & 洛谷题目链接 题意:商店里有 \(n\) 杯果汁,第 \(i\) 杯果汁有美味度 \(d_i\),单价为 \(p_i\) 元/升.最多可以添加 \(l_i\) 升.有 \ ...