《精通Python设计模式》学习结构型之外观模式
这个我在工作中也有所应用的。
就是在真正的实现层上面,再封装一个函数的调用的。
这样就可以在内层函数作真正实现,
而外层调用函数对外开放,
隔离内外的变化性。

from enum import Enum
from abc import ABCMeta, abstractmethod
State = Enum('State', 'new running sleeping restart zombie')
class User:
pass
class Process:
pass
class File:
pass
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 = Stae.restart if restart else State.zobmie
def create_process(self, user, name):
print("trying to create process '{}' for user '{}'"
.format(name, user))
class WindowsServer:
pass
class NetworkServer:
pass
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()

《精通Python设计模式》学习结构型之外观模式的更多相关文章
- 【设计模式】结构型03外观模式(Facade Pattern)
[设计模式]结构型02装饰模式(Decorator Pattern) 意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 主要解决:降低访问 ...
- 《精通Python设计模式》学习结构型之MVC模式
这个就不需要多评论了, 哪个主流的PYTHON的WEB框架都有这些模式实现哈. quotes = ('A man is not complete until he is married. Then h ...
- C#设计模式学习笔记:(10)外观模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7772184.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲结构型设计模式的第五个模式--外 ...
- 【设计模式】结构型04桥接模式(Bridge Pattern)
学习地址:http://www.runoob.com/design-pattern/bridge-pattern.html 桥接模式(Bridge Pattern) 桥接模式(Bridge patte ...
- 设计模式学习-使用go实现外观模式
外观模式 定义 适用范围 代码实现 优点 缺点 关于接口粒度的思考 参考 外观模式 定义 外观模式也叫门面模式 外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接 ...
- 《精通Python设计模式》学习结构型之代理模式
这种模式,总会让人想到SRPING中的AOP, 不同语言有不同的实现方式吧. class SensitiveInfo: def __init__(self): self.users = ['nick' ...
- 设计模式学习之路——Facade 外观模式(结构型模式)
动机: 组件的客户和组件中各种复杂的子系统有了过多的耦合,随着外部客户程序和各子系统的演化,这种过多的耦合面临很多变化的挑战.如何简化外部客户程序和系统间的交互接口?如何将外部客户程序的演化和内部子系 ...
- 【设计模式】结构型05组合模式(Composite Pattern)
组合模式(Composite Pattern) 意图:将对象组合成树形结构以表示"部分-整体"的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 主要解决:它在我们 ...
- 【设计模式】结构型01代理模式(Proxy Pattern)
代理模式(Proxy Pattern) 定义:顾名思义,增加中间层,为其他对象提供一种代理以控制对这个对象的访问.核心在于代理二字. 1.和适配器模式的区别:适配器模式主要改变所考虑对象的接口,而代理 ...
随机推荐
- Linux最大文件句柄(文件描述符)限制和修改
转自:http://jameswxx.iteye.com/blog/2096461 写这个文章是为了以正视听,网上的文章人云亦云到简直令人发指.到底最大文件数被什么限制了?too many open ...
- Go_13:Go常用功能总结一阶段
1. go语言从键盘获取输入内容 <1. 最简单的办法是使用 fmt 包提供的 Scan 和 Sscan 开头的函数.请看以下程序: package main import "fmt& ...
- 个人常用Linux文件操作命令
本文只记录个人工作中最常用到的命令. 关机和用户 shutdown -h now 关机 reboot 重启 sync 非正常关机前执行,强制将内存缓冲区中的数据立即写入磁盘中 logout 注销 ...
- python 中__getitem__ 和 __iter__ 的区别
# -*- coding: utf-8 -*- class Library(object): def __init__(self): self.books = { 'title' : 'a', 'ti ...
- RAC的坑
http://www.cocoachina.com/industry/20140609/8737.html 1.对数组的观察 有了这些Category,大部分的Delegate都可以使用RAC来做.或 ...
- SPI、I2C、UART、I2S、GPIO、SDIO、CAN
总线,总线,总要陷进里面.这世界上的信号都一样,但是总线却成千上万,让人头疼. 总的来说,总线有三种:内部总线.系统总线和外部总线.内部总线是微机内部各外围芯片与处理器之间的总线,用于芯片一级的互连: ...
- 洛谷 p2066 机器分配(资源型)
机器分配 https://www.luogu.org/problem/show?pid=2066 题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定 ...
- Django 2.0.1 官方文档翻译: 文档目录 (Page 1)
Django documentation contents 翻译完成后会做标记. 文档按照官方提供的内容一页一页的进行翻译,有些内容涉及到其他节的内容,会慢慢补上.所有的翻译内容按自己的理解来写,尽量 ...
- python概念-Socket到底有多骚
Socket究竟是什么呢? 简单来说Socket就是用来完成客户端与服务器之间的通信 例如浏览器访问网页,例如网络游戏等一切基于客户端服务器来实现的C/S架构程序 Socket是基于互联网OSI七层协 ...
- Linux基础-yum软件包管理
任务目标:自定义yum仓库:createrepo,自定义repo文件,使用yum命令安装httpd软件包,卸载httpd软件包:yum –y remove 软件名 ,使用yum安装组件'KDE 桌面' ...