Python设计模式(二十一)【备忘录模式】
来源:互联网 发布:短信轰炸机软件 编辑:程序博客网 时间:2024/06/05 15:51
相信是成功的起点,坚持是成功的终点。
# -*- coding: utf-8 -*-from copy import copy, deepcopydef memento(obj, deep=False): state = deepcopy(obj.__dict__) if deep else copy(obj.__dict__) def restore(): obj.__dict__.clear() obj.__dict__.update(state) return restoreclass Transaction: """一个事务守护. 这一点,事实上, 就是语法糖 around a memento closure. """ deep = False states = [] def __init__(self, deep, *targets): self.deep = deep self.targets = targets self.commit() def commit(self): self.states = [memento(target, self.deep) for target in self.targets] def rollback(self): for a_state in self.states: a_state()class Transactional(object): """ 添加事务语义方法。方法用@Transactional装饰,在异常时将回滚到进入状态。 """ def __init__(self, method): self.method = method def __get__(self, obj, T): def transaction(*args, **kwargs): state = memento(obj) try: return self.method(obj, *args, **kwargs) except Exception as e: state() raise e return transactionclass NumObj(object): def __init__(self, value): self.value = value def __repr__(self): return '<%s: %r>' % (self.__class__.__name__, self.value) def increment(self): self.value += 1 @Transactional def do_stuff(self): self.value = '1111' # <- 无效的值 self.increment() # <- 将失败并回滚if __name__ == '__main__': num_obj = NumObj(-1) print(num_obj) a_transaction = Transaction(True, num_obj) try: for i in range(3): num_obj.increment() print(num_obj) a_transaction.commit() print('-- committed') for i in range(3): num_obj.increment() print(num_obj) num_obj.value += 'x' # 将失败 print(num_obj) except Exception as e: a_transaction.rollback() print('-- rolled back') print(num_obj) print('-- now doing stuff ...') try: num_obj.do_stuff() except Exception as e: print('-> doing stuff failed!') import sys import traceback traceback.print_exc(file=sys.stdout) print(num_obj)
结果:
<NumObj: -1><NumObj: 0><NumObj: 1><NumObj: 2>-- committed<NumObj: 3><NumObj: 4><NumObj: 5>-- rolled back<NumObj: 2>-- now doing stuff ...-> doing stuff failed!Traceback (most recent call last): File "mementoTest.py", line 96, in <module> num_obj.do_stuff() File "mementoTest.py", line 51, in transaction raise e File "mementoTest.py", line 48, in transaction return self.method(obj, *args, **kwargs) File "mementoTest.py", line 69, in do_stuff self.increment() # <- 将失败并回滚 File "mementoTest.py", line 64, in increment self.value += 1TypeError: Can't convert 'int' object to str implicitly<NumObj: 2>
0 0
- Python设计模式(二十一)【备忘录模式】
- 设计模式学习笔记二十一(Memoento备忘录模式)
- (二十一)Java设计模式之备忘录模式
- Python设计模式-备忘录模式
- python设计模式(备忘录模式)
- 设计模式观后(c++还原之二十一 备忘录模式)
- python设计模式之备忘录
- (十一)备忘录模式
- python设计模式之备忘录模式
- 设计模式-备忘录模式-memento-python??
- java 开发模式之二十一:备忘录模式
- C++设计模式[二十一]访问者模式
- 设计模式(二十一)---解释器模式
- 设计模式(二十一)---组合模式
- 设计模式笔记二十一:状态模式
- 设计模式(二十一) 状态模式
- JAVA设计模式(二十一)----访问者模式
- 设计模式-----备忘录模式
- jquery
- 解决Ionic官方网站打开缓慢问题
- Django添加全文搜索功能入门篇
- iOS 用storyboard写项目添加引导页
- 安装cloudear后检查服务时,遇到的三个问题
- Python设计模式(二十一)【备忘录模式】
- raknet(1)--学习网站
- Linux(Ubuntu, Fedora, Vmware)使用笔记(2)
- bzoj 1050 [HAOI2006]旅行comf [最小生成树] [动点spfa] [LCT]
- Broadcast内部机制讲解 << 推荐
- win10系统下配置免安装MySQL,亲测有效
- Java(System类,currentTimeMillis())
- oracle中分组排序函数用法
- 通俗的理解Gradle(转自zhihu