【编程思想】【设计模式】【结构模式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 ...
随机推荐
- 【Java】 List和Array转换
List转Array toArray 首先展示初学者容易犯的错误示例 List<String> strList = new ArrayList<>(); strList.add ...
- maven控制台出现乱码
maven默认环境为GBK,只需要改如下即可: 在IDEA中,打开File | Settings | Build, Execution, Deployment | Build Tools | Mave ...
- uni-app视频组件设置圆角
无法实现,建议写个image在中间位置加个播放按钮,点击播放跳转新页面只需要在跳转参数里面把视频链接加上,在onLoad里面获取视频链接,自动播放视频,很多app目前都是这样做的,关闭页面后视频会自动 ...
- Spark中资源调度和任务调度
Spark比MR快的原因 1.Spark基于内存的计算 2.粗粒度资源调度 3.DAG有向无环图:可以根据宽窄依赖划分出可以并行计算的task 细粒度资源调度 MR是属于细粒度资源调度 优点:每个ta ...
- 【IDEA】IDEA项目没有被SVN管理问题
解决方法 VCS-Enable Version Control Integration
- python一对一教程:Computational Problems for Physics chapter 1 Code Listings
作者自我介绍:大爽歌, b站小UP主 ,直播编程+红警三 ,python1对1辅导老师 . 本博客为一对一辅导学生python代码的教案, 获得学生允许公开. 具体辅导内容为<Computati ...
- 基于echarts 24种数据可视化展示,填充数据就可用,动手能力强的还可以DIY(演示地址+下载地址)
前言 我们先跟随百度百科了解一下什么是"数据可视化 [1]". 数据可视化,是关于数据视觉表现形式的科学技术研究. 其中,这种数据的视觉表现形式被定义为,一种以某种概要形式抽提出来 ...
- 卸载.net 5.0后使用dotnet提示Found .NET Core SDK
之前安装了预览版本的vs2019后试了下,然后卸载了.但发现控制台执行dotnet相关命令提示Found .NET Core SDK, but did not find dotnet.dll at [ ...
- vue项目中使用 SheetJS / js-xlsx 导出文件
1. npm install xlsx 2. 在App.vue 中引入xlsx import * as XLSX from 'xlsx'; // 数据导出导入所需要的依赖 3. 使用xlsx 3 ...
- 【NOI导刊200908模拟试题02 题4】【二分+Dijkstra】 收费站
Description 在某个遥远的国家里,有n个城市.编号外1,2,3,-,n. 这个国家的政府修建了m条双向的通路.每条公路连接着两个城市.沿着某条公路,开车从一个城市到另一个城市,需要花费一定的 ...