Memento

パターンの目的

p.303, line.3-4 カプセル化を破壊せずに、オブジェクトの内部状態を据えて外面化しておき、オブジェクトを後にこの状態に戻すことができるようにする。

GoF パターンのクラス図

structure-08-05:memento

MixJuice 版 Memento (改善)

解決される GoF パターンの問題点

情報隠蔽の問題点

p.307,line.8-10 4.narrowインターフェイスとwideインターフェイスを定義する。言語によっては、OriginatorオブジェクトだけがMementoオブジェクトの状態にアクセスできるようにするのは難しいかもしれない。

p.307 「Memento クラスには 2つのインタフェースがある。すなわち、Originator オブジェクトのための wide インタフェースと、他のオブジェクトのための narrow インタフェースである。」

p.287, line.5-6, Mementos have two interfaces: a wide one for originators and a narrow one for other objects.

対策

名前空間分離によって解決できる。

結果

OriginatorオブジェクトだけがMementoオブジェクトの状態にアクセスできるようになる。

構造

structure-08-09:memento

module m {
  define class Originator {
    define abstract void setMemento(Memento m);
    define abstract Memento createMemento();
  }

  define class Memento {}
}

module m.implementation extends m {
  class Originator {
    int state;
    void setMemento(Memento m) {...}
    Memento createMemento() {...}
  }

  class Memento {
    int state;
  }
}

MixJuice によるデザインパターン改善カタログ


田中 哲 <akr@m17n.org>, 一杉 裕志 <y-ichisugi@aist.go.jp>