当前位置:首页 > 设计模式 > 正文内容

备忘录模式

canca19年前 (2007-03-26)设计模式420

//备忘录模式
//CopyRight(C) CAnca software Office.2006
//Created by CAnca.

public class MementoMode{
 public static void main(String[] args){
  studentInfo student = new studentInfo();
  student.setName("CAnca");
  student.setNumber(11);
  student.setScore(80.9F);
  
  //Enter
  crlf();
  
  //Store internal state
  ProspectMemory pm = new ProspectMemory();
  pm.setMemento(student.saveMemento());
  
  //Continue changing originator
  student.setName("Jacky");
  student.setNumber(32);
  student.setScore(60.5F);
  
  crlf();
  
  //Restore saved state
  student.RestoreMemento(pm.getMemento());
  
 }
 
 private static void crlf(){
  System.out.println();
 }
}
 
// "Originator"
class studentInfo{
 private String name;
 private int number;
 private float score;
 
 public String getName(){
  return this.name;
 }
 public void setName(String name){
  this.name = name;
  System.out.println("name:" + name);
 }
 
 public int getNumber(){
  return this.number;
 }
 public void setNumber(int number){
  this.number = number;
  System.out.println("number:" + number);
 }
 
 public float getScore(){
  return this.score;
 }
 public void setScore(float score){
  this.score = score;
  System.out.println("score:" + score);
 }
 
 public Memento saveMemento(){
  return new Memento(name,number,score);  
 }
 public void RestoreMemento(Memento m){
  this.setName(m.getName());
  this.setNumber(m.getNumber());
  this.setScore(m.getScore());
 }
}

//"Memento"
class Memento{
 private String name;
 private int number;
 private float score;
 
 //Constructor
 public Memento(String name,int number,float score){
  this.name = name;
  this.number = number;
  this.score = score;
 }
 
 public String getName(){
  return this.name;
 }
 public void setName(String name){
  this.name = name;
 }
 
 public int getNumber(){
  return this.number;
 }
 public void setNumber(int number){
  this.number = number;
 }
 
 public float getScore(){
  return this.score;
 }
 public void setScore(float score){
  this.score = score;
 }
}

// "Caretaker" 
class ProspectMemory{
 private Memento memento;
 
 public void setMemento(Memento memento){
  this.memento = memento;
 }
 public Memento getMemento(){
  return this.memento;
 }
}

 

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/657.html

标签: 设计模式
分享给朋友:
返回列表

上一篇:组合模式

下一篇:策略模式

“备忘录模式” 的相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ public static void main(String[] args){  ...

工厂方法模式

//工厂方法模式:现实例子//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. import java.util.*; public class Real_World_Example{ public static void...

建造者模式

//建造者模式//CopyRight(C)CAnca Software Office.//Created by CAnca. import java.util.*; public class BuilderMode{ public static void main(String[] arg...

桥接模式

//桥接模式//CopyRight(C) CAnca Software Office 2006.//Created by CAnca. import java.util.*; public class BridgeMode{ public static void main(String[]...

外观模式

//外观模式//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. public class FacadeMode{ public static void main(String[] args){  Fa...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。