JAVA设计模式之备忘录模式原理与用法详解
本文实例讲述了JAVA设计模式之备忘录模式。分享给大家供大家参考,具体如下:
备忘录模式:又叫做快照模式,指在不破坏封装性的前提下,获取到一个对象的内部状态,并在对象之外记录或保存这个状态。在有需要的时候可将该对象恢复到原先保存的状态。我们相当于把对象原始状备份保留,所以叫备忘录模式。
*模式角色对象组成:
1.发起者对象:负责创建一个备忘录来记录当前对象的内部状态,并可使用备忘录恢复内部状态。
2.备忘录对象:负责存储发起者对象的内部状态,并防止其他对象访问备忘录。
3.管理者对象:负责备忘录权限管理,不能对备忘录对象的内容进行访问或者操作。
优点:
1、备忘录模式可以把发起人内部信息对象屏蔽起来,从而可以保持封装的边界。
2、简化了发起人类。当发起人角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存储起来的备忘录将状态复原。
缺点:
1、如果状态需要完整地存储到备忘录对象中,那么在资源消耗上面备忘录对象比较昂贵。
2、当发起者对象的状态改变的时候,有可能这个协议无效。如果状态改变的成功率达不到要求,可以考虑采取“假如”协议模式。
/**
*备忘录对象类
*@description:
*@date2016-1-22上午11:15:59
*/
publicclassMemoBean{
privateintuseTime;//使用时间
privateStringdeviceName;//设备名称
privateintstateLevel;//状态
publicintgetUseTime(){
returnuseTime;
}
publicvoidsetUseTime(intuseTime){
this.useTime=useTime;
}
publicStringgetDeviceName(){
returndeviceName;
}
publicvoidsetDeviceName(StringdeviceName){
this.deviceName=deviceName;
}
publicintgetStateLevel(){
returnstateLevel;
}
publicvoidsetStateLevel(intstateLevel){
this.stateLevel=stateLevel;
}
}
/**
*备忘录管理对象
*@description:
*@date2016-1-22上午11:15:25
*/
publicclassMemoManager{
MemoBeanmemento;
publicMemoBeangetMemento(){
returnmemento;
}
publicvoidsetMemento(MemoBeanmemento){
this.memento=memento;
}
}
/**
*发起者对象
*@description:
*@date2016-1-22上午11:21:18
*/
publicclassMemoRole{
privateintuseTime;//使用时间
privateStringdeviceName;//设备名称
privateintstateLevel;//状态
publicMemoRole(StringdeviceName,intuseTime,intstateLevel){
super();
this.useTime=useTime;
this.deviceName=deviceName;
this.stateLevel=stateLevel;
}
publicMemoRole(){
}
publicintgetUseTime(){
returnuseTime;
}
publicvoidsetUseTime(intuseTime){
this.useTime=useTime;
}
publicStringgetDeviceName(){
returndeviceName;
}
publicvoidsetDeviceName(StringdeviceName){
this.deviceName=deviceName;
}
publicintgetStateLevel(){
returnstateLevel;
}
publicvoidsetStateLevel(intstateLevel){
this.stateLevel=stateLevel;
}
publicMemoBeancreateMemoObject(){
MemoBeanmemento=newMemoBean();
memento.setDeviceName(deviceName);
memento.setStateLevel(stateLevel);
memento.setUseTime(useTime);
returnmemento;
}
publicvoidsetMemento(MemoBeanmemento){
this.deviceName=memento.getDeviceName();
this.stateLevel=memento.getStateLevel();
this.useTime=memento.getUseTime();
}
/**
*获取对象当前状态
*@description:
*@authorldm
*@date2016-1-22下午12:15:09
*/
publicvoidgetCurrentState(){
System.out.println("当前设备名称:"+this.deviceName+"当前使用时间:"+this.useTime+"当前工作状态:"+this.stateLevel);
}
}
测试类
publicclassTest{
publicstaticvoidmain(String[]args){
//新建备忘录发起者对象
MemoRolerole=newMemoRole("发电机",0,1);
//新建备忘录管理者
MemoManagermanager=newMemoManager();
//角色初始状态
System.out.println("机器开始发电:");
role.getCurrentState();
//利用备忘录模式保存当前状态
System.out.println("---保存当前的机器状态---");
manager.setMemento(role.createMemoObject());
role.setDeviceName("发电机");
role.setStateLevel(5);
role.setUseTime(1000);
System.out.println("已经持续发电1000小时");
role.getCurrentState();
//恢复保存的角色状态
role.setMemento(manager.getMemento());
System.out.println("恢复后发电机当前状态:");
role.getCurrentState();
}
}
结果:
机器开始发电: 当前设备名称:发电机当前使用时间:0当前工作状态:1 —保存当前的机器状态— 已经持续发电N小时 当前设备名称:发电机当前使用时间:1000当前工作状态:5 恢复后发电机当前状态: 当前设备名称:发电机当前使用时间:0当前工作状态1
运行结果的最后一句表示回到了初始状态,起到了备份作用。
更多java相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。