dreamstone 发表于 2013-2-5 01:34:22

Java中的模式 --

一、定义:备忘录(memento)模式又叫快照(snapshot)模式或者token模式,主要功能:
备忘录模式是用一个对象来存储另外一个对象的内部状态的快照,实现备忘录模式的关键点是在不破坏封装的
情况下,将一个对象的状态捕捉住,并外部化,存储起来,从而可以在合适的时候,把这个对象还原。
说明:备忘录模式适模式中比较好理解的一个,这里就不举例子,但是备忘录模式是模式中实现比较难,或者说
实现比较巧的,这里主要说说。
二、备忘录模式的实现
1,备忘录模式中的角色
发起人:创建含有内部状态的备忘录对象,并使用备忘录对象存储状态
负责人:负责人保存备忘录对象,但不检查备忘录对象的内容
备忘录:备忘录对象将发起人对象的内部状态存起来,并保正其内容不被发起人对象之外的对象像读取
注意:在备忘录的角色中,定义了他必须对不同的人提供不同的接口,对发起人提供宽接口,对其它任何人提供窄
接口。也许你说我都提供宽接口得了。对这也是备忘录的一种实现,叫做白箱备忘录,不过这种方法的封装没有设计
好,安全性不够好。
2,白箱备忘录的实现:
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee;"> 1http://www.agoit.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedBlock.gifpublic class Originatorhttp://www.agoit.com/Images/dot.gif{
 2http://www.agoit.com/Images/OutliningIndicators/InBlock.gif    private String state;
 3http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public Memento CreateMemento()http://www.agoit.com/Images/dot.gif{
 4http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        return new Memento(state);
 5http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
 6http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public void restoreMemento(Memento memento)http://www.agoit.com/Images/dot.gif{
 7http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        this.state = memento.getState();
 8http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
 9http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public String getState()http://www.agoit.com/Images/dot.gif{
10http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        return this.state;
11http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
12http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public void setState(String state)http://www.agoit.com/Images/dot.gif{
13http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        this.state=state;
14http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        System.out.println("Current state = " + this.state);
15http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
16http://www.agoit.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
17http://www.agoit.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedBlock.gifpublic class Mementohttp://www.agoit.com/Images/dot.gif{
18http://www.agoit.com/Images/OutliningIndicators/InBlock.gif    private String state;
19http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public Memento(String state)http://www.agoit.com/Images/dot.gif{
20http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        this.state = state;
21http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
22http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public String getState()http://www.agoit.com/Images/dot.gif{
23http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        return this.state;
24http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
25http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public void setState()http://www.agoit.com/Images/dot.gif{
26http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        this.state = state;
27http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
28http://www.agoit.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
29http://www.agoit.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedBlock.gifpublic class Caretakerhttp://www.agoit.com/Images/dot.gif{
30http://www.agoit.com/Images/OutliningIndicators/InBlock.gif    private Memento memento;
31http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public Memento retrieveMemento()http://www.agoit.com/Images/dot.gif{
32http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        return this.memento;
33http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
34http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public void saveMemento(Memento memento)http://www.agoit.com/Images/dot.gif{
35http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        this.memento = memento;
36http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
37http://www.agoit.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
38http://www.agoit.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedBlock.gifpublic class Clienthttp://www.agoit.com/Images/dot.gif{
39http://www.agoit.com/Images/OutliningIndicators/InBlock.gif    private static Originator o = new Originator();
40http://www.agoit.com/Images/OutliningIndicators/InBlock.gif    private static Caretaker c = new Caretaker();
41http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.agoit.com/Images/OutliningIndicators/ContractedSubBlock.gif    public static void main(Sting[] args)http://www.agoit.com/Images/dot.gif{
42http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        o.setState("ON");
43http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        c.saveMemento(o.createMemento());
44http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        o.setState("OFF");
45http://www.agoit.com/Images/OutliningIndicators/InBlock.gif        o.restoreMemento(c.retrieveMemento());
46http://www.agoit.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
47http://www.agoit.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: Java中的模式 --