本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7591658
建立时间:2006年5月29日




[设计模式]Java中的模式 --- 双重接口的实现,备忘录模式
软件技术

lhwork 发表于 2006/12/19 9:56:41

一、定义:备忘录(memento)模式又叫快照(snapshot)模式或者token模式,主要功能:备忘录模式是用一个对象来存储另外一个对象的内部状态的快照,实现备忘录模式的关键点是在不破坏封装的情况下,将一个对象的状态捕捉住,并外部化,存储起来,从而可以在合适的时候,把这个对象还原。说明:备忘录模式适模式中比较好理解的一个,这里就不举例子,但是备忘录模式是模式中实现比较难,或者说实现比较巧的,这里主要说说。二、备忘录模式的实现1,备忘录模式中的角色发起人:创建含有内部状态的备忘录对象,并使用备忘录对象存储状态负责人:负责人保存备忘录对象,但不检查备忘录对象的内容备忘录:备忘录对象将发起人对象的内部状态存起来,并保正其内容不被发起人对象之外的对象像读取注意:在备忘录的角色中,定义了他必须对不同的人提供不同的接口,对发起人提供宽接口,对其它任何人提供窄接口。也许你说我都提供宽接口得了。对这也是备忘录的一种实现,叫做白箱备忘录,不过这种方法的封装没有设计好,安全性不够好。2,白箱备忘录的实现: 1500)this.width=500'>500)this.width=500'>public class Originator500)this.width=500'>{ 2500)this.width=500'>    private String state; 3500)this.width=500'>500)this.width=500'>    public Memento CreateMemento()500)this.width=500'>{ 4500)this.width=500'>        return new Memento(state); 5500)this.width=500'>    } 6500)this.width=500'>500)this.width=500'>    public void restoreMemento(Memento memento)500)this.width=500'>{ 7500)this.width=500'>        this.state = memento.getState(); 8500)this.width=500'>    } 9500)this.width=500'>500)this.width=500'>    public String getState()500)this.width=500'>{10500)this.width=500'>        return this.state;11500)this.width=500'>    }12500)this.width=500'>500)this.width=500'>    public void setState(String state)500)this.width=500'>{13500)this.width=500'>        this.state=state;14500)this.width=500'>        System.out.println("Current state = " + this.state);15500)this.width=500'>    }16500)this.width=500'>}17500)this.width=500'>500)this.width=500'>public class Memento500)this.width=500'>{18500)this.width=500'>    private String state;19500)this.width=500'>500)this.width=500'>    public Memento(String state)500)this.width=500'>{20500)this.width=500'>        this.state = state;21500)this.width=500'>    }22500)this.width=500'>500)this.width=500'>    public String getState()500)this.width=500'>{23500)this.width=500'>        return this.state;24500)this.width=500'>    }25500)this.width=500'>500)this.width=500'>    public void setState()500)this.width=500'>{26500)this.width=500'>        this.state = state;27500)this.width=500'>    }28500)this.width=500'>}29500)this.width=500'>500)this.width=500'>public class Caretaker500)this.width=500'>{30500)this.width=500'>    private Memento memento;31500)this.width=500'>500)this.width=500'>    public Memento retrieveMemento()500)this.width=500'>{32500)this.width=500'>        return this.memento;33500)this.width=500'>    }34500)this.width=500'>500)this.width=500'>    public void saveMemento(Memento memento)500)this.width=500'>{35500)this.width=500'>        this.memento = memento;36500)this.width=500'>    }37500)this.width=500'>}38500)this.width=500'>500)this.width=500'>public class Client500)this.width=500'>{39500)this.width=500'>    private static Originator o = new Originator();40500)this.width=500'>    private static Caretaker c = new Caretaker();41500)this.width=500'>500)this.width=500'>    public static void main(Sting[] args)500)this.width=500'>{42500)this.width=500'>        o.setState("ON");43500)this.width=500'>        c.saveMemento(o.createMemento());44500)this.width=500'>        o.setState("OFF");45500)this.width=500'>        o.restoreMemento(c.retrieveMemento());46500)this.width=500'>    }47500)this.width=500'>}白箱的优点:实现简单白箱的缺点:上边说了,破坏了封装,安全性有些问题。说明:这里白箱的实现只保存了一个状态,其实是可以保存多个状态的。3,双接口的实现,宽窄接口(黑箱)如何实现宽窄接口呢,内部类也许是个好方法。我们把备忘录类设计"成发起人"的内部类,但这样还有的问题是同一package中的其它类也能访问到,为了解决这个问题,我们可以把"备忘录"的方法设计成私有的方法,这样就可以保正封装,又保正发起人能访问到。实现如下:定义窄接口. 1500)this.width=500'>500)this.width=500'>public interface NarrowMemento500)this.width=500'>{ 2500)this.width=500'>    public void narrowMethod(); 3500)this.width=500'>} 4500)this.width=500'>500)this.width=500'>class Originator 500)this.width=500'>{ 5500)this.width=500'>    private String state; 6500)this.width=500'>    private NarrowMemento memento; 7500)this.width=500'>500)this.width=500'>    public Originator()500)this.width=500'>{ 8500)this.width=500'>    } 9500)this.width=500'>500)this.width=500'>    public NarrowMemento createMemento()500)this.width=500'>{10500)this.width=500'>        memento = new Memento(this.state);11500)this.width=500'>        return memento;12500)this.width=500'>    }13500)this.width=500'>500)this.width=500'>    public void restoreMemento(NarrowMemento memento)500)this.width=500'>{14500)this.width=500'>        Memento aMemento = (Memento)memento;15500)this.width=500'>        this.setState(aMemento.getState());16500)this.width=500'>    }17500)this.width=500'>500)this.width=500'>    public String getState()500)this.width=500'>{18500)this.width=500'>        return this.state;19500)this.width=500'>    }20500)this.width=500'>500)this.width=500'>    public void setState(String state)500)this.width=500'>{21500)this.width=500'>        this.state = state;22500)this.width=500'>    }23500)this.width=500'>    //内部类24500)this.width=500'>500)this.width=500'>    protected class Memento implements NarrowMemento500)this.width=500'>{25500)this.width=500'>        private String savedState;26500)this.width=500'>500)this.width=500'>        private Memento(String someState)500)this.width=500'>{27500)this.width=500'>            saveState = someState;28500)this.width=500'>        }29500)this.width=500'>500)this.width=500'>        private void setState(String someState)500)this.width=500'>{30500)this.width=500'>            saveState = someState;31500)this.width=500'>        }32500)this.width=500'>500)this.width=500'>        private String getState()500)this.width=500'>{33500)this.width=500'>            return saveState;34500)this.width=500'>        }35500)this.width=500'>500)this.width=500'>        public void narrowMethod()500)this.width=500'>{36500)this.width=500'>            System.out.println("this is narrow method");37500)this.width=500'>        }38500)this.width=500'>        39500)this.width=500'>    }40500)this.width=500'>500)this.width=500'>    public NarrowMemento getNarrowMemento()500)this.width=500'>{41500)this.width=500'>        return memento;42500)this.width=500'>    }43500)this.width=500'>}44500)this.width=500'>500)this.width=500'>public class Caretaker500)this.width=500'>{45500)this.width=500'>    private NarrowMemento memento;46500)this.width=500'>500)this.width=500'>    public NarrowMemento retrieveMemento()500)this.width=500'>{47500)this.width=500'>        return this.memento;48500)this.width=500'>    }49500)this.width=500'>500)this.width=500'>    public void saveMemento(NarrowMemento memento)500)this.width=500'>{50500)this.width=500'>        this.memento = memento;51500)this.width=500'>    }52500)this.width=500'>}53500)this.width=500'>500)this.width=500'>public class Client500)this.width=500'>{54500)this.width=500'>    private static Originator o = new Originator();55500)this.width=500'>    private static Caretaker c = new Caretaker();56500)this.width=500'>500)this.width=500'>    public static void main(String[] args)500)this.width=500'>{57500)this.width=500'>        //use wide interface58500)this.width=500'>        o.setState("On");59500)this.width=500'>        c.saveMemento(o.createMemento());60500)this.width=500'>        o.setState("Off");61500)this.width=500'>        o.restoreMemento(c.retrieveMemento());62500)this.width=500'>        //use narrow interface63500)this.width=500'>        NarrowMemento memento = o.getNarrowMemento();64500)this.width=500'>        memento.narrowMethod();65500)this.width=500'>        66500)this.width=500'>    }67500)this.width=500'>}ok,实现了对大多数人实现比较窄的接口,对Originator实现了宽接口.三,最后的一些说明:1,前边两个例子都是记录了单个状态(单check点),要实现多个状态点很容易,只须要把记录state的字符串换成一个list,然後添加,取得。如果须要随机须得状态点,也可以用map来存放.这样多个check点就实现了。2,一般情况下可以扩展负责人的功能,让负责人的功能更强大,从而让客户端的操做更少些。解放客户端。3,自述历史模式,这个就是把发起人,负责人写在一个类中,平时的应用中这种方法比较常见。


阅读全文(1950) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.109 second(s), page refreshed 144752236 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号