File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ package MomenoDesignPattern ;
2
+
3
+ public class SaveState {
4
+
5
+ Object savedState ;
6
+
7
+ public void save (SomeIndependentClass sic )
8
+ {
9
+ savedState = sic ;
10
+ }
11
+
12
+ public Object restore ()
13
+ {
14
+ return savedState ;
15
+ }
16
+
17
+
18
+ }
Original file line number Diff line number Diff line change
1
+ package MomenoDesignPattern ;
2
+
3
+ public class SomeIndependentClass {
4
+
5
+ int counter ;
6
+ private SaveState saveState ;
7
+
8
+ public SomeIndependentClass (SaveState saveState )
9
+ {
10
+ this .saveState = saveState ;
11
+ }
12
+
13
+ public void run ()
14
+ {
15
+ for (int i =0 ;i <10 ;i ++)
16
+ {
17
+ counter ++;
18
+ saveState .save (this );
19
+ }
20
+ }
21
+
22
+ public int getCounterValue ()
23
+ {
24
+ return counter ;
25
+ }
26
+
27
+ }
Original file line number Diff line number Diff line change
1
+ package MomenoDesignPattern ;
2
+
3
+ public class Test {
4
+
5
+ public static void main (String [] args )
6
+ {
7
+ SaveState state = new SaveState ();
8
+
9
+ SomeIndependentClass sic = new SomeIndependentClass (state );
10
+ sic .run ();
11
+ sic .run ();
12
+ sic .run ();
13
+
14
+ SomeIndependentClass s1 = (SomeIndependentClass )state .restore ();
15
+ System .out .println (s1 .getCounterValue ());
16
+ }
17
+
18
+ }
You can’t perform that action at this time.
0 commit comments