File tree Expand file tree Collapse file tree 7 files changed +123
-0
lines changed
src/main/java/com/iluwatar Expand file tree Collapse file tree 7 files changed +123
-0
lines changed Original file line number Diff line number Diff line change 3737 <module >mediator</module >
3838 <module >memento</module >
3939 <module >observer</module >
40+ <module >state</module >
4041 </modules >
4142
4243<build >
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" ?>
2+ <project xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns =" http://maven.apache.org/POM/4.0.0"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" >
4+ <modelVersion >4.0.0</modelVersion >
5+ <parent >
6+ <groupId >com.iluwatar</groupId >
7+ <artifactId >java-design-patterns</artifactId >
8+ <version >1.0-SNAPSHOT</version >
9+ </parent >
10+ <groupId >com.iluwatar</groupId >
11+ <artifactId >state</artifactId >
12+ <version >1.0-SNAPSHOT</version >
13+ <name >state</name >
14+ <url >http://maven.apache.org</url >
15+ <dependencies >
16+ <dependency >
17+ <groupId >junit</groupId >
18+ <artifactId >junit</artifactId >
19+ <version >3.8.1</version >
20+ <scope >test</scope >
21+ </dependency >
22+ </dependencies >
23+ </project >
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class AngryState implements State {
4+
5+ private Mammoth mammoth ;
6+
7+ public AngryState (Mammoth mammoth ) {
8+ this .mammoth = mammoth ;
9+ }
10+
11+ @ Override
12+ public void observe () {
13+ System .out .println (String .format ("%s is furious!" , mammoth ));
14+ }
15+
16+ @ Override
17+ public void onEnterState () {
18+ System .out .println (String .format ("%s gets angry!" , mammoth ));
19+ }
20+
21+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class App
4+ {
5+ public static void main ( String [] args )
6+ {
7+
8+ Mammoth mammoth = new Mammoth ();
9+ mammoth .observe ();
10+ mammoth .timePasses ();
11+ mammoth .observe ();
12+ mammoth .timePasses ();
13+ mammoth .observe ();
14+
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class Mammoth {
4+
5+ private State state ;
6+
7+ public Mammoth () {
8+ state = new PeacefulState (this );
9+ }
10+
11+ public void timePasses () {
12+ if (state .getClass ().equals (PeacefulState .class )) {
13+ changeStateTo (new AngryState (this ));
14+ } else {
15+ changeStateTo (new PeacefulState (this ));
16+ }
17+ }
18+
19+ private void changeStateTo (State newState ) {
20+ this .state = newState ;
21+ this .state .onEnterState ();
22+ }
23+
24+ @ Override
25+ public String toString () {
26+ return "The mammoth" ;
27+ }
28+
29+ public void observe () {
30+ this .state .observe ();
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class PeacefulState implements State {
4+
5+ private Mammoth mammoth ;
6+
7+ public PeacefulState (Mammoth mammoth ) {
8+ this .mammoth = mammoth ;
9+ }
10+
11+ @ Override
12+ public void observe () {
13+ System .out .println (String .format ("%s is calm and peaceful." , mammoth ));
14+ }
15+
16+ @ Override
17+ public void onEnterState () {
18+ System .out .println (String .format ("%s calms down." , mammoth ));
19+ }
20+
21+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public interface State {
4+
5+ void onEnterState ();
6+
7+ void observe ();
8+
9+ }
You can’t perform that action at this time.
0 commit comments