《精通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.和适配器模式的区别:适配器模式主要改变所考虑对象的接口,而代理 ...
随机推荐
- 十、Shell基础
一.shell概述 1.Shell是什么 shell是一个命令行解释器,他为用户提供了一个向linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动.挂起.停止甚至编写一些程序 ...
- Python【读写Json文件】
indent=10:缩进10个空格
- List保存在ViewState
private List<SYSUAO> UserRoleList { get { return ViewState["UserRoleList"] as List&l ...
- supervisor自启动
supervisor自启动 其实自启动,也就是在主机开启的时候,执行了sudo supervisord -c /etc/supervisord.conf: 创建/usr/lib/systemd/sys ...
- 跨域ajax请求携带cookie
通过withCredentials 方式: getCompanySubject: function () { return axios.get(this.apiUrls.getCompanySubje ...
- Lua 错误 收集
不存在的变量或者变量没有定义,提示错误 // :: [error] #: * lua entry thread aborted: runtime error: /opt/openresty/nginx ...
- Linux_用户管理&权限管理
2017年1月11日, 星期三 Linux_用户管理&权限管理 1. Linux用户管理&权限管理 终端的概念: tty 查看登录的终端 类型 user group oth ...
- Codeforces刷题计划
Codeforces刷题计划 已完成:-- / -- [Codeforces370E]370E - Summer Reading:构造:(给定某些数,在空白处填数,要求不下降,并且相邻差值<=1 ...
- Throwable、Error、Exception、RuntimeException 区别
1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception. 2.Error是Throwable 的子类,用于指示合理的应用 ...
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...