\\ memento pattern (OOP)
Class Memento {
Public:
Function Final Store {
k->(this)
Group M {
type: CareTakerMemento
Private:
a=k
Public:
Function Restore() {
=.a
}
}
->(M)
}
Module Restore {
Error "abstract"
}
}
Class Originator as Memento {
Private:
state=1
Public:
module ChangeState {
.state+=100
}
module DisplayState {
Print "state=";.state
}
module Restore(p as *Originator) {
this=group(p)
}
}
Class CareTaker {
Private:
k=pointer()
Public:
Module DoSomething (&Originator as Originator){
Originator.ChangeState
Originator.DisplayState
.k<=Originator.Store()
Originator.ChangeState
Originator.DisplayState
}
Module RollBack (&Originator as Originator){
if .k is type CareTakerMemento then
Originator.Restore .k=>Restore()
Originator.DisplayState
End If
}
}
careTaker=CareTaker()
originator=Originator()
careTaker.DoSomething &originator
careTaker.RollBack &originator
Public:
Function Final Store {
k->(this)
Group M {
type: CareTakerMemento
Private:
a=k
Public:
Function Restore() {
=.a
}
}
->(M)
}
Module Restore {
Error "abstract"
}
}
Class Originator as Memento {
Private:
state=1
Public:
module ChangeState {
.state+=100
}
module DisplayState {
Print "state=";.state
}
module Restore(p as *Originator) {
this=group(p)
}
}
Class CareTaker {
Private:
k=pointer()
m=pointer()
Public:
Module DoSomething (&Originator as Originator){
.k<=Originator.Store()
Originator.ChangeState
Originator.DisplayState
}
Module RollBack (&Originator as Originator){
if .k is type CareTakerMemento then
Originator.Restore .k=>Restore()
Originator.DisplayState
if not .m is type null then
this<=group(.m)
end if
End If
}
Remove {
Print "Remove one"
}
class:
module CareTaker() {
if match("G") then
read .m as pointer
end if
}
}
careTaker->CareTaker()
originator=Originator()
originator.DisplayState
careTaker=>DoSomething &originator
careTaker->CareTaker(careTaker)
careTaker=>DoSomething &originator
careTaker->CareTaker(careTaker)
careTaker=>DoSomething &originator
careTaker=>RollBack &originator
careTaker=>RollBack &originator
careTaker=>RollBack &originator
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
You can feel free to write any suggestion, or idea on the subject.