Improved encapsulation of the Memento pattern by moving the
StarMementoInternal class to inner class of Star.
This commit is contained in:
parent
72bc96f81b
commit
703ebd3e20
@ -353,7 +353,7 @@ A programming idiom is a means of expressing a recurring construct in one or mor
|
|||||||
## <a name="memento">Memento</a> [↑](#list-of-design-patterns)
|
## <a name="memento">Memento</a> [↑](#list-of-design-patterns)
|
||||||
**Intent:** Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
|
**Intent:** Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Applicability:** Use the Memento pattern when
|
**Applicability:** Use the Memento pattern when
|
||||||
* a snapshot of an object's state must be saved so that it can be restored to that state later, and
|
* a snapshot of an object's state must be saved so that it can be restored to that state later, and
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 57 KiB |
BIN
memento/etc/memento.png
Normal file
BIN
memento/etc/memento.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
@ -1,65 +1,65 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<class-diagram version="1.1.7" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
|
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||||
associations="true" dependencies="false" nesting-relationships="true">
|
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||||
<class id="1" language="java" name="com.iluwatar.Star" project="memento"
|
<class id="1" language="java" name="com.iluwatar.Star" project="memento"
|
||||||
file="/memento/src/main/java/com/iluwatar/Star.java" binary="false" corner="BOTTOM_RIGHT">
|
file="/memento/src/main/java/com/iluwatar/Star.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
<position height="-1" width="-1" x="383" y="159"/>
|
<position height="-1" width="-1" x="301" y="385"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
sort-features="false" accessors="true" visibility="true">
|
sort-features="false" accessors="true" visibility="true">
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
</display>
|
</display>
|
||||||
</class>
|
</class>
|
||||||
<class id="2" language="java" name="com.iluwatar.StarMementoInternal" project="memento"
|
<enumeration id="2" language="java" name="com.iluwatar.StarType" project="memento"
|
||||||
file="/memento/src/main/java/com/iluwatar/StarMementoInternal.java" binary="false" corner="BOTTOM_RIGHT">
|
|
||||||
<position height="-1" width="-1" x="697" y="141"/>
|
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
|
||||||
sort-features="false" accessors="true" visibility="true">
|
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
|
||||||
</display>
|
|
||||||
</class>
|
|
||||||
<enumeration id="3" language="java" name="com.iluwatar.StarType" project="memento"
|
|
||||||
file="/memento/src/main/java/com/iluwatar/StarType.java" binary="false" corner="BOTTOM_RIGHT">
|
file="/memento/src/main/java/com/iluwatar/StarType.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
<position height="-1" width="-1" x="533" y="429"/>
|
<position height="-1" width="-1" x="301" y="911"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
sort-features="false" accessors="true" visibility="true">
|
sort-features="false" accessors="true" visibility="true">
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
</display>
|
</display>
|
||||||
</enumeration>
|
</enumeration>
|
||||||
<interface id="4" language="java" name="com.iluwatar.StarMemento" project="memento"
|
<interface id="3" language="java" name="com.iluwatar.StarMemento" project="memento"
|
||||||
file="/memento/src/main/java/com/iluwatar/StarMemento.java" binary="false" corner="BOTTOM_RIGHT">
|
file="/memento/src/main/java/com/iluwatar/StarMemento.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
<position height="-1" width="-1" x="536" y="-17"/>
|
<position height="-1" width="-1" x="103" y="830"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
sort-features="false" accessors="true" visibility="true">
|
sort-features="false" accessors="true" visibility="true">
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
</display>
|
</display>
|
||||||
</interface>
|
</interface>
|
||||||
<dependency id="5">
|
<class id="4" language="java" name="com.iluwatar.Star.StarMementoInternal" project="memento"
|
||||||
|
file="/memento/src/main/java/com/iluwatar/Star.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="103" y="639"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<association id="5">
|
||||||
|
<end type="SOURCE" refId="1" navigable="false">
|
||||||
|
<attribute id="6" name="type"/>
|
||||||
|
<multiplicity id="7" minimum="0" maximum="1"/>
|
||||||
|
</end>
|
||||||
|
<end type="TARGET" refId="2" navigable="true"/>
|
||||||
|
<display labels="true" multiplicity="true"/>
|
||||||
|
</association>
|
||||||
|
<association id="8">
|
||||||
|
<end type="SOURCE" refId="4" navigable="false">
|
||||||
|
<attribute id="9" name="type"/>
|
||||||
|
<multiplicity id="10" minimum="0" maximum="1"/>
|
||||||
|
</end>
|
||||||
|
<end type="TARGET" refId="2" navigable="true"/>
|
||||||
|
<display labels="true" multiplicity="true"/>
|
||||||
|
</association>
|
||||||
|
<nesting id="11">
|
||||||
<end type="SOURCE" refId="1"/>
|
<end type="SOURCE" refId="1"/>
|
||||||
<end type="TARGET" refId="4"/>
|
<end type="TARGET" refId="4"/>
|
||||||
</dependency>
|
</nesting>
|
||||||
<association id="6">
|
|
||||||
<end type="SOURCE" refId="1" navigable="false">
|
|
||||||
<attribute id="7" name="type"/>
|
|
||||||
<multiplicity id="8" minimum="0" maximum="1"/>
|
|
||||||
</end>
|
|
||||||
<end type="TARGET" refId="3" navigable="true"/>
|
|
||||||
<display labels="true" multiplicity="true"/>
|
|
||||||
</association>
|
|
||||||
<association id="9">
|
|
||||||
<end type="SOURCE" refId="2" navigable="false">
|
|
||||||
<attribute id="10" name="type"/>
|
|
||||||
<multiplicity id="11" minimum="0" maximum="1"/>
|
|
||||||
</end>
|
|
||||||
<end type="TARGET" refId="3" navigable="true"/>
|
|
||||||
<display labels="true" multiplicity="true"/>
|
|
||||||
</association>
|
|
||||||
<realization id="12">
|
<realization id="12">
|
||||||
<end type="SOURCE" refId="2"/>
|
<end type="SOURCE" refId="4"/>
|
||||||
<end type="TARGET" refId="4"/>
|
<end type="TARGET" refId="3"/>
|
||||||
</realization>
|
</realization>
|
||||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
sort-features="false" accessors="true" visibility="true">
|
sort-features="false" accessors="true" visibility="true">
|
Binary file not shown.
Before Width: | Height: | Size: 34 KiB |
@ -66,4 +66,40 @@ public class Star {
|
|||||||
return String.format("%s age: %d years mass: %d tons", type.toString(),
|
return String.format("%s age: %d years mass: %d tons", type.toString(),
|
||||||
ageYears, massTons);
|
ageYears, massTons);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* StarMemento implementation
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static class StarMementoInternal implements StarMemento {
|
||||||
|
|
||||||
|
private StarType type;
|
||||||
|
private int ageYears;
|
||||||
|
private int massTons;
|
||||||
|
|
||||||
|
public StarType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(StarType type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAgeYears() {
|
||||||
|
return ageYears;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAgeYears(int ageYears) {
|
||||||
|
this.ageYears = ageYears;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMassTons() {
|
||||||
|
return massTons;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMassTons(int massTons) {
|
||||||
|
this.massTons = massTons;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
package com.iluwatar;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Internal interface to memento.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class StarMementoInternal implements StarMemento {
|
|
||||||
|
|
||||||
private StarType type;
|
|
||||||
private int ageYears;
|
|
||||||
private int massTons;
|
|
||||||
|
|
||||||
public StarType getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setType(StarType type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getAgeYears() {
|
|
||||||
return ageYears;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAgeYears(int ageYears) {
|
|
||||||
this.ageYears = ageYears;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getMassTons() {
|
|
||||||
return massTons;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMassTons(int massTons) {
|
|
||||||
this.massTons = massTons;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user