jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  备忘录模式应该是设计模式中最简单的一个了,比如开会中找人来做备忘录,就是这个意思。

public class Cahier {
    private String name;

    private String content;

    private String persons;

 public void setName (String name) {
  this.name =  name;
 }
 public String getName () {
  return this.name;
 }
 public void setContent (String content) {
  this.content = content;
 }
 public String getContent () {
  return this.content;
 }
 public void setPersons (String persons) {
  this.persons =  persons;
 }
 public String getPersons () {
  return this.persons;
 }

    public Memento getMemento() {
  return new Memento(this);
 }

 public void setMemento(Memento memento) {
  this.name = memento.getName();
  this.content = memento.getContent ();
  this.persons = memento.getPersons ();
 }


public class Memento {
    private String name;

    private String content;

    private String persons;

    public Memento (Cahier cahier) {
  this.name = cahier.getName();
  this.content = cahier.getContent ();
  this.persons = cahier.getPersons ();
 }

 public void setName (String name) {
  this.name =  name;
 }
 public String getName () {
  return this.name;
 }
 public void setContent (String content) {
  this.content = content;
 }
 public String getContent () {
  return this.content;
 }
 public void setPersons (String persons) {
  this.persons =  persons;
 }
 public String getPersons () {
  return this.persons;
 }

public class Client {
    public static void main(String[] argv) {
  Cahier cahier = new Cahier();
  cahier.setName("公司销售会议");
        cahier.setContent("有关销售价格的会议内容");
        cahier.setPersons("总经理、销售处长");
        System.out.println("原来的内容" + cahier.getName() + " :" + cahier.getContent() +  " :" + cahier.getPersons());
        Memento memento = cahier.getMemento();
  //进行其它代码的处理
  cahier.setName("公司办公会议");
        cahier.setContent("有关员工稳定的会议内容");
        cahier.setPersons("董事长、总经理、人事副总");
        System.out.println("修改后的内容" + cahier.getName() + " :" + cahier.getContent() +  " :" + cahier.getPersons());
  //恢复原来的代码
  cahier.setMemento(memento);
        System.out.println("恢复到原来的内容" + cahier.getName() + " :" + cahier.getContent() +  " :" + cahier.getPersons());
    }
}
posted on 2009-08-06 22:11  jackyrong的世界  阅读(405)  评论(0编辑  收藏  举报