码迷,mamicode.com
首页 > 编程语言 > 详细

python版本的备忘录模式

时间:2018-01-25 14:13:01      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:name   备忘录   .sh   set   log   car   ini   utf-8   mem   

# -*- coding:UTF-8 -*-
class Originator:
    def __init__(self):
        self.__state = None

    @property
    def state(self):
        return self.__state

    @state.setter
    def state(self, value):
        self.__state = value

    def create_memento(self):
        return Memento(self.__state)

    def set_memento(self, memento):
        self.__state = memento.state

    def show(self):
        print("State=%s" % self.__state)


class Memento:
    def __init__(self, state):
        self.__state = state

    @property
    def state(self):
        return self.__state


class Caretaker:
    def __init__(self):
        self.__memento = None

    @property
    def memento(self):
        return self.__memento

    @memento.setter
    def memento(self,value):
        self.__memento = value

        
if __name__=="__main__":
    o = Originator()
    o.state = "On"
    o.show()

    c = Caretaker()
    c.memento = o.create_memento()

    o.state = "Off"
    o.show()

    o.set_memento(c.memento)
    o.show()

 

python版本的备忘录模式

标签:name   备忘录   .sh   set   log   car   ini   utf-8   mem   

原文地址:https://www.cnblogs.com/gjinwei/p/8350770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!