設計模式之備忘錄模式筆記

來源:互聯網
上載者:User
  備忘錄模式應該是設計模式中最簡單的一個了,比如開會中找人來做備忘錄,就是這個意思。

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());
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.