python模式
1.创作模式
2.结构模式
3.行为模式
4.其他模式
迷心兔
我对你的感情大概就是,就算我们很久不说话,不聊天,或者哪天直接就没了任何联系,我还是会觉得你是我非常,非常重要的人...
展开
-
Python 单例模式【重写__new__方法,类方法,装饰器,元类】
Happiness is a way station between too much and too little.幸福是太多和太少之间的一站原创 2020-10-18 00:41:25 · 346 阅读 · 2 评论 -
状态模式
# 电脑状态class ComputerState(): name = "state" allowed = [] def switch(self, state): if state.name in self.allowed: print('当前:', self, ' => 切换到新状态', state.name) ...转载 2019-12-08 12:53:24 · 84 阅读 · 0 评论 -
行为模式:发布_订阅
The hard part isn’t making the decision. It’s living with it. 做出决定并不困难,困难的是接受决定转载 2019-11-03 17:33:23 · 82 阅读 · 0 评论 -
行为模式:观察者
# coding=utf-8from __future__ import print_function# 主题class Subject(object): def __init__(self): self._observers = [] def attach(self, observer): if observer not in self...转载 2019-11-03 17:08:41 · 71 阅读 · 0 评论 -
结构模式:对象将操作转移到其他对象
# coding=utf-8from __future__ import print_functionimport time# 销售经理class SalesManager: def talk(self): print("\t<销售经理>准备谈\n")# 秘书【充当代理】class Proxy: def __init__(self):...转载 2019-11-03 13:49:53 · 109 阅读 · 0 评论 -
结构模式:在MVC下的模型——》视图——》控制器(非严格关系)
# coding=utf-8class Model(object): # 作用:规范子类的行为或属性 def __iter__(self): # 作用:对象是可迭代的 raise NotImplementedError def get(self, item): """ 返回带有.items()调用方法的对象, ...转载 2019-11-03 13:24:14 · 110 阅读 · 0 评论 -
结构模式:提供集中的入口点,用于控制和管理请求处理
# coding=utf-8class MobileView(object): def show_index_page(self): print('显示移动手机索引页面')class TabletView(object): def show_index_page(self): print('显示平板电脑索引页面')# 响应请求处理cl...转载 2019-11-03 12:47:37 · 125 阅读 · 0 评论 -
结构模式:使用一个类作为API调用其它类
注意:没有去了解硬件的运行方式,胡乱翻译成中文# coding=utf-8from __future__ import print_functionclass CPU(object): def freeze(self): print("启动CPU处理器...") def jump(self, position): print(f"从 C...转载 2019-11-03 12:31:47 · 101 阅读 · 0 评论 -
结构模式:为父类添加装饰功能
# coding=utf-8from __future__ import print_function# 文字标签class TextTag(object): def __init__(self, text): self._text = text def render(self): return self._text# 文字加粗...转载 2019-11-03 12:04:04 · 55 阅读 · 0 评论 -
结构模式:统一单个对象合成结构构图让客户能明白
输出不能看明白,要是能树状排序就好了# coding=utf-8# 图形化class Graphic: # 显示 def render(self): raise NotImplementedError("你应该实现这个方法.")# 复合图形class CompositeGraphic(Graphic): def __init__(self):...转载 2019-11-03 11:41:38 · 89 阅读 · 0 评论 -
结构模式:将抽象与其实现分离
# coding=utf-8# 实现的绘图API 1class DrawingAPI1(object): def draw_circle(self, x, y, radius): print('API1.圆的比例 {}:{} 半径 {}'.format(x, y, radius))# 实现的绘图API 2class DrawingAPI2(object): ...转载 2019-11-03 11:29:00 · 306 阅读 · 0 评论 -
结构模式:使用适配器
# coding=utf-8class Dog(object): def __init__(self): self.name = "Dog" def bark(self): return "woof!"class Cat(object): def __init__(self): self.name = "Cat"...转载 2019-11-03 11:18:06 · 75 阅读 · 0 评论 -
结构模式:数据——》业务逻辑——》呈现分离(严格关系)
原因:把结构层次分开,避免以后扩展业务逻辑的不必要的麻烦# coding=utf-8class Data(object): """ 数据存储类 """ products = { 'milk' : {'price': 1.50, 'quantity': 10}, 'eggs' : {'price': 0.20, 'quanti...转载 2019-11-03 10:40:45 · 326 阅读 · 0 评论 -
创作模式:使用自己定义的对象池
# coding=utf-8try: import queueexcept ImportError: # python 2.x compatibility import Queue as queueclass ObjectPool(object): def __init__(self, queue, auto_get=False): self._...转载 2019-11-03 03:18:45 · 161 阅读 · 0 评论 -
创作模式:克隆原型创建新的对象
# coding=utf-8# 原型class Prototype(object): value = 'default' def clone(self, **kwargs): """ 克隆原型并更新内部属性字典 :param kwargs: :return: """ obj = ...转载 2019-11-03 10:00:23 · 109 阅读 · 0 评论 -
创作模式:懒汉式创建方法【类下的自己叫方法】
from __future__ import print_functionimport functoolsclass lazy_property(object): def __init__(self, function): self.function = function # print(function) # <function Perso...转载 2019-11-03 02:43:07 · 187 阅读 · 0 评论