備忘錄設計模式

來源:互聯網
上載者:User

我們談這樣那樣的模式,歸根到底是為了什麼呢?按照 網上 招聘單位的話是“良好的物件導向的編程思想”,是的,剛剛接觸設計模式的時候,我驚呼,設計模式是物件導向的最高境界,不錯,我為止發出了一聲又一聲的歎服,高,實在是高,這實在是提高自己邏輯思維,寫出高複用性,高維護性的代碼不可或缺的。

備忘錄模式,按照GOF 的話說,在不破話封裝的前提下,捕獲一個對象的內部狀態,並在該對象之外儲存該對象,談到不破化封裝的,其實在java 領域,有一個東西,反射,不管是 private 都能拿到,但是就是破壞了封裝麼,

public class Caretaker {
 private Memento mement;

 public Memento getMement() {
  return mement;
 }

 public void setMement(Memento mement) {
  this.mement = mement;
 }
}

 

public class Memento {
 private String name;

 public Memento(String name, String sex, int age) {
  super();
  this.name = name;
  this.sex = sex;
  this.age = age;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getSex() {
  return sex;
 }

 public void setSex(String sex) {
  this.sex = sex;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 private String sex;
 private int age;
}

public class Person {
private String name;
private String sex;
private int age;
public Person(String name, String sex, int age) {
 super();
 this.name = name;
 this.sex = sex;
 this.age = age;
}
public Person() {
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public String getSex() {
 return sex;
}
public void setSex(String sex) {
 this.sex = sex;
}
public int getAge() {
 return age;
}
public void setAge(int age) {
 this.age = age;
}
public void display(){
System.out.println("Person [name=" + name + ", sex=" + sex + ", age=" + age + "]");
}
//按照物件導向的設計原則,建立備份
public Memento createMemento(){
 return new Memento(name, sex, age);
}
//恢複,還原
public void setMemento(Memento mementto){
 this.age=mementto.getAge();
 this.name=mementto.getName();
 this.sex=mementto.getSex();
}
}

public class Person {
private String name;
private String sex;
private int age;
public Person(String name, String sex, int age) {
 super();
 this.name = name;
 this.sex = sex;
 this.age = age;
}
public Person() {
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public String getSex() {
 return sex;
}
public void setSex(String sex) {
 this.sex = sex;
}
public int getAge() {
 return age;
}
public void setAge(int age) {
 this.age = age;
}
public void display(){
System.out.println("Person [name=" + name + ", sex=" + sex + ", age=" + age + "]");
}
//按照物件導向的設計原則,建立備份
public Memento createMemento(){
 return new Memento(name, sex, age);
}
//恢複,還原
public void setMemento(Memento mementto){
 this.age=mementto.getAge();
 this.name=mementto.getName();
 this.sex=mementto.getSex();
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.