Skip to content

Commit 703ebd3

Browse files
committed
Improved encapsulation of the Memento pattern by moving the
StarMementoInternal class to inner class of Star.
1 parent 72bc96f commit 703ebd3

File tree

7 files changed

+136
-137
lines changed

7 files changed

+136
-137
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ A programming idiom is a means of expressing a recurring construct in one or mor
353353
## <a name="memento">Memento</a> [&#8593;](#list-of-design-patterns)
354354
**Intent:** Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
355355

356-
![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/memento/etc/memento_1.png "Memento")
356+
![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/memento/etc/memento.png "Memento")
357357

358358
**Applicability:** Use the Memento pattern when
359359
* a snapshot of an object's state must be saved so that it can be restored to that state later, and

memento/etc/memento.jpg

-56.8 KB
Binary file not shown.

memento/etc/memento.png

22 KB
Loading

memento/etc/test.ucls renamed to memento/etc/memento.ucls

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<class-diagram version="1.1.7" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3-
associations="true" dependencies="false" nesting-relationships="true">
2+
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
44
<class id="1" language="java" name="com.iluwatar.Star" project="memento"
55
file="/memento/src/main/java/com/iluwatar/Star.java" binary="false" corner="BOTTOM_RIGHT">
6-
<position height="-1" width="-1" x="383" y="159"/>
6+
<position height="-1" width="-1" x="301" y="385"/>
77
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
88
sort-features="false" accessors="true" visibility="true">
99
<attributes public="true" package="true" protected="true" private="true" static="true"/>
1010
<operations public="true" package="true" protected="true" private="true" static="true"/>
1111
</display>
1212
</class>
13-
<class id="2" language="java" name="com.iluwatar.StarMementoInternal" project="memento"
14-
file="/memento/src/main/java/com/iluwatar/StarMementoInternal.java" binary="false" corner="BOTTOM_RIGHT">
15-
<position height="-1" width="-1" x="697" y="141"/>
13+
<enumeration id="2" language="java" name="com.iluwatar.StarType" project="memento"
14+
file="/memento/src/main/java/com/iluwatar/StarType.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="301" y="911"/>
1616
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
1717
sort-features="false" accessors="true" visibility="true">
1818
<attributes public="true" package="true" protected="true" private="true" static="true"/>
1919
<operations public="true" package="true" protected="true" private="true" static="true"/>
2020
</display>
21-
</class>
22-
<enumeration id="3" language="java" name="com.iluwatar.StarType" project="memento"
23-
file="/memento/src/main/java/com/iluwatar/StarType.java" binary="false" corner="BOTTOM_RIGHT">
24-
<position height="-1" width="-1" x="533" y="429"/>
21+
</enumeration>
22+
<interface id="3" language="java" name="com.iluwatar.StarMemento" project="memento"
23+
file="/memento/src/main/java/com/iluwatar/StarMemento.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="103" y="830"/>
2525
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
2626
sort-features="false" accessors="true" visibility="true">
2727
<attributes public="true" package="true" protected="true" private="true" static="true"/>
2828
<operations public="true" package="true" protected="true" private="true" static="true"/>
2929
</display>
30-
</enumeration>
31-
<interface id="4" language="java" name="com.iluwatar.StarMemento" project="memento"
32-
file="/memento/src/main/java/com/iluwatar/StarMemento.java" binary="false" corner="BOTTOM_RIGHT">
33-
<position height="-1" width="-1" x="536" y="-17"/>
30+
</interface>
31+
<class id="4" language="java" name="com.iluwatar.Star.StarMementoInternal" project="memento"
32+
file="/memento/src/main/java/com/iluwatar/Star.java" binary="false" corner="BOTTOM_RIGHT">
33+
<position height="-1" width="-1" x="103" y="639"/>
3434
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
3535
sort-features="false" accessors="true" visibility="true">
3636
<attributes public="true" package="true" protected="true" private="true" static="true"/>
3737
<operations public="true" package="true" protected="true" private="true" static="true"/>
3838
</display>
39-
</interface>
40-
<dependency id="5">
41-
<end type="SOURCE" refId="1"/>
42-
<end type="TARGET" refId="4"/>
43-
</dependency>
44-
<association id="6">
39+
</class>
40+
<association id="5">
4541
<end type="SOURCE" refId="1" navigable="false">
46-
<attribute id="7" name="type"/>
47-
<multiplicity id="8" minimum="0" maximum="1"/>
42+
<attribute id="6" name="type"/>
43+
<multiplicity id="7" minimum="0" maximum="1"/>
4844
</end>
49-
<end type="TARGET" refId="3" navigable="true"/>
45+
<end type="TARGET" refId="2" navigable="true"/>
5046
<display labels="true" multiplicity="true"/>
5147
</association>
52-
<association id="9">
53-
<end type="SOURCE" refId="2" navigable="false">
54-
<attribute id="10" name="type"/>
55-
<multiplicity id="11" minimum="0" maximum="1"/>
48+
<association id="8">
49+
<end type="SOURCE" refId="4" navigable="false">
50+
<attribute id="9" name="type"/>
51+
<multiplicity id="10" minimum="0" maximum="1"/>
5652
</end>
57-
<end type="TARGET" refId="3" navigable="true"/>
53+
<end type="TARGET" refId="2" navigable="true"/>
5854
<display labels="true" multiplicity="true"/>
5955
</association>
60-
<realization id="12">
61-
<end type="SOURCE" refId="2"/>
56+
<nesting id="11">
57+
<end type="SOURCE" refId="1"/>
6258
<end type="TARGET" refId="4"/>
59+
</nesting>
60+
<realization id="12">
61+
<end type="SOURCE" refId="4"/>
62+
<end type="TARGET" refId="3"/>
6363
</realization>
6464
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
6565
sort-features="false" accessors="true" visibility="true">

memento/etc/memento_1.png

-34 KB
Binary file not shown.
Lines changed: 105 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,105 @@
1-
package com.iluwatar;
2-
3-
/**
4-
*
5-
* Star uses "mementos" to store and restore state.
6-
*
7-
*/
8-
public class Star {
9-
10-
private StarType type;
11-
private int ageYears;
12-
private int massTons;
13-
14-
public Star(StarType startType, int startAge, int startMass) {
15-
this.type = startType;
16-
this.ageYears = startAge;
17-
this.massTons = startMass;
18-
}
19-
20-
public void timePasses() {
21-
ageYears *= 2;
22-
massTons *= 8;
23-
switch (type) {
24-
case RED_GIANT:
25-
type = StarType.WHITE_DWARF;
26-
break;
27-
case SUN:
28-
type = StarType.RED_GIANT;
29-
break;
30-
case SUPERNOVA:
31-
type = StarType.DEAD;
32-
break;
33-
case WHITE_DWARF:
34-
type = StarType.SUPERNOVA;
35-
break;
36-
case DEAD:
37-
ageYears *= 2;
38-
massTons = 0;
39-
break;
40-
default:
41-
break;
42-
}
43-
}
44-
45-
StarMemento getMemento() {
46-
47-
StarMementoInternal state = new StarMementoInternal();
48-
state.setAgeYears(ageYears);
49-
state.setMassTons(massTons);
50-
state.setType(type);
51-
return state;
52-
53-
}
54-
55-
void setMemento(StarMemento memento) {
56-
57-
StarMementoInternal state = (StarMementoInternal) memento;
58-
this.type = state.getType();
59-
this.ageYears = state.getAgeYears();
60-
this.massTons = state.getMassTons();
61-
62-
}
63-
64-
@Override
65-
public String toString() {
66-
return String.format("%s age: %d years mass: %d tons", type.toString(),
67-
ageYears, massTons);
68-
}
69-
}
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* Star uses "mementos" to store and restore state.
6+
*
7+
*/
8+
public class Star {
9+
10+
private StarType type;
11+
private int ageYears;
12+
private int massTons;
13+
14+
public Star(StarType startType, int startAge, int startMass) {
15+
this.type = startType;
16+
this.ageYears = startAge;
17+
this.massTons = startMass;
18+
}
19+
20+
public void timePasses() {
21+
ageYears *= 2;
22+
massTons *= 8;
23+
switch (type) {
24+
case RED_GIANT:
25+
type = StarType.WHITE_DWARF;
26+
break;
27+
case SUN:
28+
type = StarType.RED_GIANT;
29+
break;
30+
case SUPERNOVA:
31+
type = StarType.DEAD;
32+
break;
33+
case WHITE_DWARF:
34+
type = StarType.SUPERNOVA;
35+
break;
36+
case DEAD:
37+
ageYears *= 2;
38+
massTons = 0;
39+
break;
40+
default:
41+
break;
42+
}
43+
}
44+
45+
StarMemento getMemento() {
46+
47+
StarMementoInternal state = new StarMementoInternal();
48+
state.setAgeYears(ageYears);
49+
state.setMassTons(massTons);
50+
state.setType(type);
51+
return state;
52+
53+
}
54+
55+
void setMemento(StarMemento memento) {
56+
57+
StarMementoInternal state = (StarMementoInternal) memento;
58+
this.type = state.getType();
59+
this.ageYears = state.getAgeYears();
60+
this.massTons = state.getMassTons();
61+
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return String.format("%s age: %d years mass: %d tons", type.toString(),
67+
ageYears, massTons);
68+
}
69+
70+
/**
71+
*
72+
* StarMemento implementation
73+
*
74+
*/
75+
private static class StarMementoInternal implements StarMemento {
76+
77+
private StarType type;
78+
private int ageYears;
79+
private int massTons;
80+
81+
public StarType getType() {
82+
return type;
83+
}
84+
85+
public void setType(StarType type) {
86+
this.type = type;
87+
}
88+
89+
public int getAgeYears() {
90+
return ageYears;
91+
}
92+
93+
public void setAgeYears(int ageYears) {
94+
this.ageYears = ageYears;
95+
}
96+
97+
public int getMassTons() {
98+
return massTons;
99+
}
100+
101+
public void setMassTons(int massTons) {
102+
this.massTons = massTons;
103+
}
104+
}
105+
}

memento/src/main/java/com/iluwatar/StarMementoInternal.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)