19-备忘录(Memento)模式Ruby实现
Posted 架构设计模式
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了19-备忘录(Memento)模式Ruby实现相关的知识,希望对你有一定的参考价值。
摘要:本文用一个实例场景描述Gof 23设计模式中的备忘录(Memento)模式,并用Ruby程序代码给予实现,同时也给出实现代码的UML模型。
关键字:Gof 23 设计模式 备忘录模式 Ruby
Memento Pattern basedon Ruby
REN gang
Abstract:This paper gives a scene example to describe theuse of Memento Pattern of Gof 23 Design Pattern, and achieves the example by Ruby,at the same time, gives the UML model of realization of the example.
Key word: Gof 23; DesignPattern; Memento Pattern Ruby
1. 标准定义
备忘录(Memento)模式标准定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
2. 分析和说明
备忘录(Memento)模式属于对象行为型模式。备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捉住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态。
Memento结构如图1所示,其角色包括发起人(Originator)角色和负责人(Caretaker)角色。
图1 备忘录模式结构
发起人(Originator)角色,发起人角色有如下的责任:① 创建一个含有当前的内部状态的备记录对象。②使用备忘录对象存储其内部状态。
负责人(Caretaker)角色,负责人角色有如下的责任:① 负责保存备忘录对象。②不检查备忘录对象的内容。
3 应用场景举例
公司领导在每周周一都要召开项目会议,每次会议后都有会议纪要。会议纪要都要把现阶段项目情况汇总起来。这些项目情况就是备忘录,上面有时间戳这个标志。
在这里可以把Meeting类理解为发起人(Originator)角色。Caretaker类是负责人(Caretaker)角色。其实现类图如图3所示。Memento类与Caretaker类的关系是聚合关系,即Caretaker类拥有多个Memento类。
图2 备忘录模式类图
3.Ruby的实现程序代码
Ruby程序实现主要包括Meeting类,Caretaker类和Memento类等3个类。其关系如图2所示。下面分别列出这3个类的程序代码,最后列出测试代码并显示输出结果。
程序代码清单
#!/behavioralpatterns/memento
class Meeting
def initialize() @meetName = nil @meetType = nil @meetdate = nil @meetLeader = nil @meetParticipants = nil @decide = nil @meetContent = nil end
def getMeetName @meetName end
def setMeetName(meetName) @meetName = meetName end
def getMeetType() @meetType end
def setMeetType(meetType) @meetType = meetType end
def getMeetdate() @meetdate end
def setMeetdate(meetdate) @meetdate = meetdate; end
def getMeetLeader() @meetLeader end
def setMeetLeader(meetLeader) @meetLeader = meetLeader; end
def getMeetParticipants() @meetParticipants; end
def setMeetParticipants(meetParticipants) @meetParticipants = meetParticipants; end
def getDecide @decide; end
def setDecide(decide) @decide = decide; end
def getMeetContent @meetContent; end
def setMeetContent(meetContent) @meetContent = meetContent; end
def doDecide @meetContent = "会议名称:" + @meetName + ";"; @meetContent = @meetContent + "会议类型:" + @meetType + ";"; @meetContent = @meetContent + "会议时间:" + @meetdate + ";"; @meetContent = @meetContent + "会议主持人:" + @meetLeader + ";"; @meetContent = @meetContent + "会议参加者:" + @meetParticipants + ";"; @meetContent = @meetContent + "会议内容:" + @meetParticipants + ";"; end
def CreateMemento mo = Memento.new(@meetName, @meetType, @meetdate, @meetLeader, @meetParticipants, @decide, @meetContent); mo end
def recoveryMemento(mo) @meetName = mo.getMeetName() @meetType = mo.getMeetType() @meetdate = mo.getMeetdate() @meetLeader = mo.getMeetLeader() @meetParticipants = mo.getMeetParticipants() @decide = mo.getDecide() @meetContent = mo.getMeetContent() end
def showContent() puts @meetContent end
end
class Memento
def initialize(meetName, meetType, meetdate, meetLeader, meetParticipants, decide, meetContent) @meetName = meetName @meetType = meetType @meetdate = meetdate @meetLeader = meetLeader @meetParticipants = meetParticipants @decide = decide @meetContent = meetContent end
def showContent puts meetContent end
def getMeetName @meetName end
def setMeetName(meetName) @meetName = meetName end
def getMeetType @meetType; end
def setMeetType(meetType) @meetType = meetType end
def getMeetdate @meetdate end
def setMeetdate(meetdate) @meetdate = meetdate end
def getMeetLeader @meetLeader end
def setMeetLeader(meetLeader) @meetLeader = meetLeader end
def getMeetParticipants @meetParticipants end
def setMeetParticipants(meetParticipants) @meetParticipants = meetParticipants end
def getDecide @decide end
def setDecide(decide) @decide = decide end
def getMeetContent @meetContent end
def setMeetContent(meetContent) @meetContent = meetContent end end
class Caretaker
def initialize() @meetMementoMap = Hash.new end
def recoveryMemento(meetdate) @meetMementoMap[meetdate] end
def saveMemento(meetdate, memento) @meetMementoMap[meetdate] = memento end
def showContent @meetMementoMap.each do | meetdate, memento | puts "#{meetdate}: $#{memento.getMeetContent()}" puts end end end
# ————————main主程序应用————————
meet1 = Meeting.new meet1.setMeetName("meetName1"); meet1.setMeetType("meetType1"); meet1.setMeetdate("2009-01-01"); meet1.setMeetLeader("meetLeader1"); meet1.setMeetParticipants("meetParticipants1"); meet1.setDecide("decide1"); meet1.doDecide(); memento = meet1.CreateMemento();
caretaker = Caretaker.new caretaker.saveMemento(memento.getMeetdate(),memento);
meet2 = Meeting.new meet2.setMeetName("meetName2"); meet2.setMeetType("meetType2"); meet2.setMeetdate("2009-02-01"); meet2.setMeetLeader("meetLeader2"); meet2.setMeetParticipants("meetParticipants2"); meet2.setDecide("decide2"); meet2.doDecide(); memento = meet2.CreateMemento();
caretaker.saveMemento(memento.getMeetdate(),memento);
caretaker.showContent();
BEGIN { puts "———这是memento的输出开始———" }
END { puts "———这是memento的输出结束———" }
|
备忘录模式测试类输出结果如下所示:
———这是memento的输出开始——— 2009-01-01: $会议名称:meetName1;会议类型:meetType1;会议时间:2009-01-01;会议主持人:meetLeader1;会议参加者:meetParticipants1;会议内容:meetParticipants1;
2009-02-01: $会议名称:meetName2;会议类型:meetType2;会议时间:2009-02-01;会议主持人:meetLeader2;会议参加者:meetParticipants2;会议内容:meetParticipants2;
———这是memento的输出结束——— |
参考文献
[1] E.Gamma, R.Helm, R.Johnson, andVlissides. Design Patterns Elements of Reusable Object Oriented Software. Addison-Wesley,1995
[2] E.Gamma, R.Helm, R.Johnson, andVlissides.著,李英军等译,设计模式:可复用面向对象软件的基础,北京:机械工业出版社.2000.9.
[3] (日)高桥征义,(日)后藤裕藏 著,何文斯 译,Ruby基础教程(第4版),北京:人民邮电出版社.2014.09
[4] 许勇 等编著,Ruby on Rails 程序设计 深入剖析与范例应用,北京:清华大学出版社.2013.07
[5] (美)梅茨 著,张雪平,彭晓东译,面向对象设计实践指南:Ruby语言描述,北京:人民邮电出版社.2014.01
[6] Ruby官方网站:www.ruby-lang.org.
[7]Ruby教程http://www.runoob.com/ruby/ruby-tutorial.html.
以上是关于19-备忘录(Memento)模式Ruby实现的主要内容,如果未能解决你的问题,请参考以下文章