设计模式:备忘录模式(Memento)

来源:互联网 发布:如何筛选excel数据求和 编辑:程序博客网 时间:2024/05/23 15:28

在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将对象恢复到原先保存的状态。

这里写图片描述

备忘录模式的角色: 
1. 原发器(Originator):负责创建一个备忘录,用以记录当前对象的内部状态,通过也可以使用它来利用备忘录回复内部状态。同时原发器还可以根据需要决定Memento存储Originator的那些内部状态。 
2. 备忘录(Memento):用于存储Originator的内部状态,并且可以防止Originator以外的对象访问Memento。在备忘录Memento中有两个接口,其中Caretaker只能看到备忘录中的窄接口,它只能将备忘录传递给其他对象。Originator可以看到宽接口,允许它访问返回到先前状态的所有数据。 
3. 负责人(Caretaker):负责保存好备忘录,不能对备忘录的内容进行操作和访问,只能够将备忘录传递给其他对象。

典型的备忘录代码:

public class Memento{    private String state;    public Memento(Oraginator o)    {        state = o.state;    }    public void setState(String state){        this.state = state;    }    public String getState()    {        return this.state;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

案例

大家一般都玩过游戏吧,就算没玩过游戏也见过室友、朋友玩过游戏吧。很多游戏中需要存档,保存当前的血条和魔法值,以防再挑战boss的时候die了可以重新读档。 
1 原发器Originator

public class Originator{    private int bloodValue;    private int magicValue;    public Originator(int bloodValue, int magicValue){        this.bloodValue = bloodValue;        this.magicValue = magicValue;    }    public int getBloodValue()    {        return bloodValue;    }    public void setBloodValue(int bloodValue)    {        this.bloodValue = bloodValue;    }    public int getMagicValue()    {        return magicValue;    }    public void setMagicValue(int magicValue)    {        this.magicValue = magicValue;    }    public void display()    {        System.out.println("用户当前状态:");        System.out.println("血量:"+getBloodValue()+";蓝量:"+getMagicValue());    }    public Memento saveMemento()    {        return new Memento(getBloodValue(),getMagicValue());    }    public void restoreMemento(Memento memento){        this.bloodValue = memento.getBloodValue();        this.magicValue = memento.getMagicValue();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

2 备忘录Memento

public class Memento{    private int bloodValue;    private int magicValue;    public int getBloodValue()    {        return bloodValue;    }    public void setBloodValue(int bloodValue)    {        this.bloodValue = bloodValue;    }    public int getMagicValue()    {        return magicValue;    }    public void setMagicValue(int magicValue)    {        this.magicValue = magicValue;    }    public Memento(int bloodValue, int magicValue)    {        this.bloodValue = bloodValue;        this.magicValue = magicValue;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

3 负责人Caretaker

public class Caretaker{    private Memento memento;    public Memento getMemento()    {        return memento;    }    public void setMemento(Memento memento)    {        this.memento = memento;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4 测试代码

        Originator originator = new Originator(100,100);        System.out.println("Before fighting BOSS...");        originator.display();        //存档        Caretaker caretaker = new Caretaker();        caretaker.setMemento(originator.saveMemento());        //Fighting        System.out.println("Fighting...");        originator.setBloodValue(20);        originator.setMagicValue(20);        originator.display();        //回复存档        System.out.println("Restore...");        originator.restoreMemento(caretaker.getMemento());        originator.display();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

输出结果:

Before fighting BOSS...用户当前状态:血量:100;蓝量:100Fighting...用户当前状态:血量:20;蓝量:20Restore...用户当前状态:血量:100;蓝量:100
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在备忘录模式中,最重要的就是备忘录Memento了。备忘录中存储的就是原发器的部分或者所有的状态信息,而这些状态信息是不能够被其它对象所访问的,也就是说我们是不可能在备忘录之外的对象来存储这些状态信息,如果暴漏了内部状态信息就违反了封装的原则,故备忘录是处理原发器外其它对象都是不可以访问的。

优缺点 
优点: 
1. 给用户提供了一种可以恢复状态的机制。可以是用户能够比较方便地回到某个历史的状态。 
2. 实现了信息的封装。使得用户不需要关心状态的保存细节。 
缺点: 
1. 消耗资源。如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存。

适用场景 
1. 需要保存一个对象在某一时刻的状态或部分状态 
2. 如果用一个接口来让其他对象得到这些状态,将会保留对象的实现细节并破坏对象的封装性,一个对象不希望外界直接访问其内部状态,通过负责人可以间接访问其内部状态。

JDK中的备忘录模式: 
java.util.Date(Date对象通过自身内部的一个long值来实现备忘录模式) 
java.io.Serializable

参考资料 
1. 23种设计模式 
2. 细数JDK里的设计模式 
3. 设计模式读书笔记—–备忘录模式

0 0
原创粉丝点击