![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Python设计模式
meet_future
这个作者很懒,什么都没留下…
展开
-
Python单例模式
单例模式,多次实例化只返回同一个类实例。class SimpleOne(object): __one = None def __init__(self): super().__init__() def __new__(cls, *args, **kwargs): if not cls.__one: cls....原创 2018-02-21 16:32:58 · 126 阅读 · 0 评论 -
Python命令模式
命令模式,同一事件监听者不同,反应自然不同。class Command(object): def __init__(self, listener): self.listener = listener def invoke(self): self.listener.do()class ListenerA(object): def...原创 2018-02-21 16:40:00 · 258 阅读 · 0 评论 -
Python解释器模式
解释器模式,给予一段字符串,对其 进行翻译构成语法树并计算结果。class Interpreter(object): def __init__(self, command): self.stack = command.split(' ') self.dynamic_stack = [] def calculate(self): ...原创 2018-02-21 16:40:16 · 551 阅读 · 0 评论 -
Python观察者模式
观察者模式,被观察物自身属性通知观察者。class Obj(object): def __init__(self, value): super().__init__() self.observers = [] self.dynamic_value = value def add_observer(self, observer...原创 2018-02-21 16:40:34 · 206 阅读 · 0 评论 -
Python状态模式
状态模式,就像某一个人在儿童、青年、老年所展现的状态是不同的。class Obj(object): def __init__(self, status): self.status = status def do(self): which_do = {"low": self.low_do, "middl...原创 2018-02-21 16:40:49 · 200 阅读 · 0 评论 -
Python策略模式
策略模式,同一问题有多种不同的解法,即不同策略,一个物体可以动态地对策略进行更换。class Stragety(object): def __init__(self, name): self.name = name def do(self): print(self.name)class Question(object): de...原创 2018-02-21 16:41:07 · 282 阅读 · 0 评论 -
Python模板方法模式
模板方法模式,在父类中确定步骤的执行过程,子类无法更改执行过程顺序。class Template(object): def __init__(self): super().__init__() def do(self): self.do_first() self.do_second() self.do_thr...原创 2018-02-21 16:41:22 · 158 阅读 · 0 评论 -
Python迭代器模式
迭代器模式,不一次性直接返回结果,调用一次方法,返回一个。class Iterator(object): def __init__(self, iterator_list): super().__init__() self.list = iterator_list self.current_index = 0 self...原创 2018-02-21 16:41:39 · 296 阅读 · 0 评论 -
Python中介者模式
中介者模式,中介者提供多人联系沟通的平台。class Mediator(object): def __init__(self): self.online_dct = {} def register(self, obj): self.online_dct[str(obj)] = obj def send(self, from_nam...原创 2018-02-21 16:41:58 · 220 阅读 · 0 评论 -
Python备忘录模式
备忘录模式,类似于撤销功能,提供保存并恢复之前状态的功能。class Momento(object): def __init__(self): super().__init__() self.dct = {} def set_status(self, dct): self.dct = {} for key i...原创 2018-02-21 16:42:14 · 217 阅读 · 0 评论 -
Python访问者模式
访问者模式,一个物体针对不同的访问者,所展现的行为是不同的。class Visitor(object): def __init__(self, type_): self.type_ = type_class Obj(object): def __init__(self, visitor): self.visitor_type = vis...原创 2018-02-21 16:42:31 · 393 阅读 · 0 评论 -
Python设计模式目录
设计模式分为3个模块,创建类型、结构类型和行为类型。创建类型关注于实例的创建,结构类型关注于不同类的之间的关系,行为类型关注于类之间的通信。目录创建类 1. 单例模式 2. 工厂模式 3. 抽象工厂模式 4. 建造者模式 5. 原型模式结构类 1. 适配器模式 2. 桥接模式 3. MVC模式 4. 修饰器模式 5. 外观模式 6. 享元模式 7. 代理模式...原创 2018-02-21 18:47:44 · 217 阅读 · 0 评论 -
Python责任链模式
责任链模式,子对象都有一个父对象,请求从一级一级从下向顶级父对象传递。class One(object): def __init__(self, name, next=None): super().__init__() self.name = name self.next = next def do(self): ...原创 2018-02-21 16:39:43 · 206 阅读 · 0 评论 -
Python享元模式
享元模式,对可以共享的属性对象进行共享,无法共享的属性独立存储。class Obj: def __init__(self, value): self.content = value def __str__(self): return self.contentclass Share(object): def __init__(s...原创 2018-02-21 16:38:31 · 188 阅读 · 0 评论 -
Python工厂模式
工厂模式,一个工厂实例化一个指定类。class NeedToInstanceClass: def __init__(self): passclass Factory(object): def __init__(self): super().__init__() self.cls = NeedToInstanceClass...原创 2018-02-21 16:35:00 · 209 阅读 · 0 评论 -
Python抽象工厂模式
抽象工厂模式,一个抽象工厂可以生产同一系列的多个型号产品实例。class TypeB(object): def __init__(self): passclass TypeA(object): def __init__(self): passclass AbstractFactory(object): def __init...原创 2018-02-21 16:35:28 · 383 阅读 · 0 评论 -
Python建造者模式
建造者模式,实例的参数初始化由建造类方法完成。class Instance(object): def __init__(self, builder): super().__init__() self.url = builder.url() self.proxy = builder.proxy()class Builder(obj...原创 2018-02-21 16:35:51 · 102 阅读 · 0 评论 -
Python原型模式
原型模式,实例提供clone方法,获取与当前相同的实例,并允许设置新的参数。class ProtoType(object): def __init__(self): super().__init__() self.setting_a = "a" self.setting_b = "b" def clone(self, **k...原创 2018-02-21 16:36:12 · 375 阅读 · 0 评论 -
Python适配器模式
适配器模式,适配不同方法的类。class ClassB(object): def __init__(self): pass def need_method(self): passclass ClassA(object): def __init__(self): pass def need_method...原创 2018-02-21 16:36:39 · 162 阅读 · 0 评论 -
Python桥接模式
桥接模式,一个类包含不同种类属性,将不同种类的属性分别转为不同的类。class Type(object): def __init__(self, type_): self.type = type_ def set_type(self, type_): self.type = type_class Color(object): ...原创 2018-02-21 16:36:58 · 189 阅读 · 0 评论 -
Python MVC模式
MVC模式,将代码分为数据模型、视图、控制逻辑三层。class Model(object): def __init__(self): self.data = [] def add(self, one): self.data.append(one) def delete(self, n): self.data.rem...原创 2018-02-21 16:37:18 · 1919 阅读 · 0 评论 -
Python修饰器模式
修饰器模式,可以对一个函数、类添加一层封装。def decorator_name(f): def wrap(*args, **kwargs): print(f.__name__) print(args) print(kwargs) f(*args, **kwargs) return wrap@decor...原创 2018-02-21 16:37:38 · 202 阅读 · 0 评论 -
Python外观模式
外观模式,对一些类进行组合并可能添加新的简易方法,效果类似一个导航条。class ModuleA(object): def __init__(self): pass def do_work(self): print("module a")class ModuleB(object): def __init__(self): ...原创 2018-02-21 16:37:56 · 188 阅读 · 0 评论 -
Python代理模式
代理模式,在调用真实方法之前后分别执行所需的操作。class RealRequest(object): def __init__(self): super().__init__() def request(self): print("request")class Proxy(object): def __init__(self...原创 2018-02-21 16:38:13 · 315 阅读 · 0 评论 -
Python组合模式
组合模式,像树的枝和叶一样进行组合。class Branch(object): def __init__(self): super().__init__() self.branch = [] self.leaf = [] def add(self, obj): if type(obj) == Branch:...原创 2018-02-21 16:39:27 · 233 阅读 · 0 评论