最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

輕松掌握Java備忘錄模式

 更新時間:2016年09月29日 10:24:32   作者:斷了聯(lián)系  
這篇文章主要幫助大家輕松掌握Java備忘錄模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下

定義:保存一個對象的某個狀態(tài),以便在適當?shù)臅r候恢復對象

特點:

    1、給用戶提供了一種可以恢復狀態(tài)的機制,可以使用戶能夠比較方便地回到某個歷史的狀態(tài)。

    2、實現(xiàn)了信息的封裝,使得用戶不需要關心狀態(tài)的保存細節(jié)。

企業(yè)級應用和常用框架中的應用:常見文本編輯器使用了該模式

實例:

注意:該實例中只有撤銷操作,沒有向前還原操作

/**
 * 目標對象:將要被備忘的對象
 */
class Word {

 private String content;
 private String image;
 private String table;
 public Word(String content, String image, String table) {
 super();
 this.content = content;
 this.image = image;
 this.table = table;
 }
 
 public WordMemento memento(){
 return new WordMemento(this);
 }
 
 public void recovery(WordMemento memento){
 this.content = memento.getContent();
 this.image = memento.getImage();
 this.table = memento.getTable();
 }
 
 public String getContent() {
 return content;
 }
 public void setContent(String content) {
 this.content = content;
 }
 public String getImage() {
 return image;
 }
 public void setImage(String image) {
 this.image = image;
 }
 public String getTable() {
 return table;
 }
 public void setTable(String table) {
 this.table = table;
 }
}

/**
 * 備忘錄對象
 */
class WordMemento{
 private String content;
 private String image;
 private String table;
 
 public WordMemento(Word word) {
 this.content = word.getContent();
 this.image = word.getImage();
 this.table = word.getTable();
 }
 public String getContent() {
 return content;
 }
 public void setContent(String content) {
 this.content = content;
 }
 public String getImage() {
 return image;
 }
 public void setImage(String image) {
 this.image = image;
 }
 public String getTable() {
 return table;
 }
 public void setTable(String table) {
 this.table = table;
 }
}
/**
 * 負責人對象:負責記錄備忘錄對象
 */
class CareTaker{

 private List<WordMemento> list = new ArrayList<>();
 private int index = 0;
 
 public void setMemento(WordMemento memento){
 list.add(memento);
 this.index = list.size();
 }
 
 public WordMemento getWordMemento(){
 if(index == 0){
  System.out.println("沒有可還原的內(nèi)容");
  return null;
 }
 WordMemento memento = list.get(index-1);
 list.remove(index-1);
 index--;
 return memento;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

乌海市| 宁明县| 潮州市| 天峨县| 金阳县| 逊克县| 临江市| 柘荣县| 白水县| 山阴县| 山西省| 富顺县| 古丈县| 萍乡市| 和平区| 大理市| 榆中县| 刚察县| 富顺县| 黄山市| 三河市| 礼泉县| 兴宁市| 三门县| 江口县| 北辰区| 星子县| 玉田县| 洞口县| 民和| 郑州市| 阳泉市| 孟津县| 石柱| 福州市| 新宾| 临安市| 固始县| 从江县| 锦屏县| 镇安县|