はじめに:[ モードの概要 ] ---------- xingooによって
意思モード
このモードは、主に必要な他の機会に良好な回復で、オブジェクトを介して、オブジェクトの特定の状態を記録したいされています。
また、このような複数回記録することができますステータスなど多くの場所で拡張することができるモデルは、各文字は、拡張のためのスペース、シーンに完全にビジネスを持っています。
シナリオ
一度、オブジェクトの1つの状態を保存
直接の露出を避けるために2インターフェイス、ダメージのカプセル化
モード構造
発信者は、メモ、オブジェクト記録状態の開始剤であります
class Originator{
private String state;
public Memento ceateMemento() {
return new Memento(state);
}
public void restoreMemento(Memento memento) {
this.state = memento.getState();
}
public String getState(){
return this.state;
}
public void setState(String state){
this.state = state;
System.out.println("Current state = "+this.state);
}
}
メメントの覚書の役割は、通常、特定の状態を保持するために使用します
class Memento{
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
世話人の状態を復元するために適切なタイミングで担当覚書の頭、
class Caretaker{
private Memento memento;
public Memento retrieveMemento(){
return this.memento;
}
public void saveMemento(Memento memento){
this.memento = memento;
}
}
すべてのコード
package com.xingoo.test.design.memento;
class Originator{
private String state;
public Memento ceateMemento() {
return new Memento(state);
}
public void restoreMemento(Memento memento) {
this.state = memento.getState();
}
public String getState(){
return this.state;
}
public void setState(String state){
this.state = state;
System.out.println("Current state = "+this.state);
}
}
class Memento{
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
class Caretaker{
private Memento memento;
public Memento retrieveMemento(){
return this.memento;
}
public void saveMemento(Memento memento){
this.memento = memento;
}
}
public class Client {
private static Originator o = new Originator();
private static Caretaker c = new Caretaker();
public static void main(String[] args) {
o.setState("On");
//记录状态
c.saveMemento(o.ceateMemento());
//更改状态
o.setState("Off");
//更新状态
o.restoreMemento(c.retrieveMemento());
}
}
業績
Current state = On
Current state = Off
ライフデザインパターン
最近ベルゼブブ、非常に漫画ナンセンスをします読んで。しかし、それらのメイドを見て、それはこのメモモードのことを思い出します。
重要なものがあるの所有者は、自分自身を思い出させるために、指定した時刻に、覚えているメイドに引き渡されます。
以下の所有者は、それは親愛なるメアリーは映画を見に行く同行することで、非常に重要なことを持っているので、彼は、この情報を記録するためのノートブックを取得します。メイドは、ノートブックを取得し、楽しい時間を議論するために、事前に所有者を思い出させます。ここでメモノートブックのトップはメメントオブジェクトであり、このモデルは、所有者が覚書のイニシエータである、メイドが担当しています。
覚書はここに関与している[]、つまり、白いボックスに属している情報の覚書、創業者と頭を見ることができます。ブラックボックス[]もあり、内部メモオブジェクトは、このクラスを継承し、主に、この担当外の人は、実際の具体的な情報覚書を取得することはありません。
次のように次のような外観の特定の実装では、マスターコードは次のとおりです。
1 class Master{
2 private String info;
3 public String getInfo() {
4 return info;
5 }
6 public void setInfo(String info) {
7 this.info = info;
8 }
9 public Note createNote(String info){
10 return new Note(info);
11 }
12 public void action(Note note){
13 this.info = note.getInfo();
14 System.out.println("主人看到笔记,记起了 "+ this.info);
15 }
16 public void toDo(){
17 System.out.println("****主人正在..."+info);
18 }
19 }
次のようにメイドのコードは次のとおりです。
1 class Maid{
2 private Note note;
3 public Note readNote(){
4 System.out.println("女仆拿到笔记本");
5 return this.note;
6 }
7 public void writeNote(Note note){
8 System.out.println("女仆写笔记");
9 this.note = note;
10 }
11 }
次のように覚書コードは次のとおりです。
1 class Note{
2 private String info;
3 public Note(String info) {
4 this.info = info;
5 }
6 public void setInfo(String info){
7 this.info = info;
8 System.out.println("写笔记!");
9 }
10 public String getInfo(){
11 System.out.println("读笔记!");
12 return info;
13 }
14 }
すべてのコード:
1 package com.xingoo.test.design.memento;
2 class Note{
3 private String info;
4 public Note(String info) {
5 this.info = info;
6 }
7 public void setInfo(String info){
8 this.info = info;
9 System.out.println("写笔记!");
10 }
11 public String getInfo(){
12 System.out.println("读笔记!");
13 return info;
14 }
15 }
16 class Master{
17 private String info;
18 public String getInfo() {
19 return info;
20 }
21 public void setInfo(String info) {
22 this.info = info;
23 }
24 public Note createNote(String info){
25 return new Note(info);
26 }
27 public void action(Note note){
28 this.info = note.getInfo();
29 System.out.println("主人看到笔记,记起了 "+ this.info);
30 }
31 public void toDo(){
32 System.out.println("****主人正在..."+info);
33 }
34 }
35 class Maid{
36 private Note note;
37 public Note readNote(){
38 System.out.println("女仆拿到笔记本");
39 return this.note;
40 }
41 public void writeNote(Note note){
42 System.out.println("女仆写笔记");
43 this.note = note;
44 }
45 }
46 public class LifeWithMaid {
47 public static void main(String[] args) {
48 Master master = new Master();
49 Maid maid = new Maid();
50 //主人想起了要做的事情
51 maid.writeNote(master.createNote("晚上6点,配小丽看电影"));
52 //主人忙其他的事情
53 master.setInfo("睡觉吃饭打豆豆!");
54 master.toDo();//主人正在做什么?
55 //时间到了,女仆提醒主人
56 master.action(maid.readNote());
57 master.toDo();//主人正在做什么?
58 }
59 }
業績
女仆写笔记
****主人正在...睡觉吃饭打豆豆!
女仆拿到笔记本
读笔记!
主人看到笔记,记起了 晚上6点,配小丽看电影
****主人正在...晚上6点,配小丽看电影
ます。https://my.oschina.net/u/204616/blog/545487で再現