Python设计模式之备忘录模式原理与用法详解
作者:Andy冉明 时间:2022-12-27 11:12:01
本文实例讲述了Python设计模式之备忘录模式原理与用法。分享给大家供大家参考,具体如下:
备忘录模式(Memento Pattern):不破坏封装性的前提下捕获一个对象的内部状态,并在该对象之外保存这个状态,这样已经后就可将该对象恢复到原先保存的状态
下面是一个备忘录模式的demo:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大话设计模式
设计模式——备忘录模式
备忘录模式(Memento Pattern):不破坏封装性的前提下捕获一个对象的内部状态,并在该对象之外保存这个状态,这样已经后就可将该对象恢复到原先保存的状态
"""
# 发起人类
class Originator(object):
def __init__(self, state):
self.state = state
def create_memento(self):
return Memento(self.state)
def set_memento(self, memento):
self.state = memento.state
def show(self):
print "当前状态 ", self.state
# 备忘录类
class Memento(object):
def __init__(self, state):
self.state = state
# 管理者类
class Caretaker(object):
def __init__(self,memento):
self.memento = memento
if __name__ == "__main__":
# 初始状态
originator = Originator(state='On')
originator.show()
# 备忘录
caretaker = Caretaker(originator.create_memento())
# 修改状态
originator.state = 'Off'
originator.show()
# 复原状态
originator.set_memento(caretaker.memento)
originator.show()
运行结果:
当前状态 On
当前状态 Off
当前状态 On
上面的类的设计如下图:
Originator(发起人):负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态,Originator可根据需要决定Memento存储Originator的那些内部状态
Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento
Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查
希望本文所述对大家Python程序设计有所帮助。
来源:https://www.cnblogs.com/onepiece-andy/p/python-memento-pattern.html
标签:Python,设计模式,备忘录模式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
golang简单位运算示例
2024-02-08 18:53:48
Ubuntu20.04环境安装tensorflow2的方法步骤
2023-07-04 06:41:21
![](https://img.aspxhome.com/file/2023/2/66412_0s.png)
js+html5实现手机九宫格密码解锁功能
2024-05-09 15:02:36
![](https://img.aspxhome.com/file/2023/2/126852_0s.jpg)
基于telepath库实现Python和JavaScript之间交换数据
2023-05-24 04:20:39
vue中的插槽详解
2024-05-03 15:08:50
![](https://img.aspxhome.com/file/2023/4/130224_0s.png)
基于Python实现射击小游戏的制作
2021-05-02 17:13:48
![](https://img.aspxhome.com/file/2023/1/131971_0s.jpg)
PyTorch梯度裁剪避免训练loss nan的操作
2022-02-16 10:56:23
Python I/O与进程的详细讲解
2022-11-27 14:07:15
![](https://img.aspxhome.com/file/2023/4/133524_0s.png)
Python接口自动化浅析logging封装及实战操作
2022-12-04 12:21:44
![](https://img.aspxhome.com/file/2023/7/91867_0s.jpg)
pandas中的ExcelWriter和ExcelFile的实现方法
2023-09-20 00:10:36
![](https://img.aspxhome.com/file/2023/3/124153_0s.png)
在Linux系统上部署Apache+Python+Django+MySQL环境
2024-01-14 00:51:15
![](https://img.aspxhome.com/file/2023/9/131449_0s.png)
python实现AES算法及AES-CFB8加解密源码
2022-06-14 01:24:01
SQL Server从安装到建库为新手寻找捷径
2009-01-13 13:22:00
MySQL安全策略(MySQL安全注意事项)
2024-01-22 19:57:25
利用Python如何画一颗心、小人发射爱心
2021-01-11 09:11:50
![](https://img.aspxhome.com/file/2023/7/95887_0s.png)
Mysql误删数据解决方案及kill语句原理
2024-01-26 20:22:15
![](https://img.aspxhome.com/file/2023/9/112359_0s.png)
Python自动化办公之Excel数据的写入
2022-07-10 09:29:34
![](https://img.aspxhome.com/file/2023/9/98929_0s.jpg)
Jquery多选下拉列表插件jquery multiselect功能介绍及使用
2024-04-22 12:59:41
![](https://img.aspxhome.com/file/2023/0/135980_0s.png)
ORACLE ORA-01653: unable to extend table 的错误处理方案(oracle报错)
2024-01-15 12:24:10
antd-日历组件,前后禁止选择,只能选中间一部分的实例
2024-04-27 15:56:35
![](https://img.aspxhome.com/file/2023/7/133087_0s.jpg)