- 博客(25)
- 收藏
- 关注
原创 Python设计模式目录
设计模式分为3个模块,创建类型、结构类型和行为类型。创建类型关注于实例的创建,结构类型关注于不同类的之间的关系,行为类型关注于类之间的通信。目录创建类 1. 单例模式 2. 工厂模式 3. 抽象工厂模式 4. 建造者模式 5. 原型模式结构类 1. 适配器模式 2. 桥接模式 3. MVC模式 4. 修饰器模式 5. 外观模式 6. 享元模式 7. 代理模式...
2018-02-21 18:47:44 213
原创 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 387
原创 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 214
原创 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 216
原创 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 284
原创 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 152
原创 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 270
原创 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 196
原创 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 198
原创 Python解释器模式
解释器模式,给予一段字符串,对其 进行翻译构成语法树并计算结果。class Interpreter(object): def __init__(self, command): self.stack = command.split(' ') self.dynamic_stack = [] def calculate(self): ...
2018-02-21 16:40:16 548
原创 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 253
原创 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 203
原创 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 227
原创 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 186
原创 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 306
原创 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 185
原创 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 199
原创 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 1908
原创 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 188
原创 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 158
原创 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 368
原创 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 100
原创 Python抽象工厂模式
抽象工厂模式,一个抽象工厂可以生产同一系列的多个型号产品实例。class TypeB(object): def __init__(self): passclass TypeA(object): def __init__(self): passclass AbstractFactory(object): def __init...
2018-02-21 16:35:28 379
原创 Python工厂模式
工厂模式,一个工厂实例化一个指定类。class NeedToInstanceClass: def __init__(self): passclass Factory(object): def __init__(self): super().__init__() self.cls = NeedToInstanceClass...
2018-02-21 16:35:00 206
原创 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 122
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人