Skip to content

Commit 2fc5134

Browse files
committed
add new State Pattern example
1 parent 6499298 commit 2fc5134

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.state.example3;
7+
8+
public class AddPointState extends GameState
9+
{
10+
public AddPointState(MyGame myGame)
11+
{
12+
super(myGame);
13+
}
14+
15+
@Override
16+
public void getPoints()
17+
{
18+
myGame.setPoints(myGame.points() + 1);
19+
System.out.println("Added 1 point");
20+
}
21+
22+
@Override
23+
public String getState()
24+
{
25+
return "Add Point State";
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.state.example3;
7+
8+
public abstract class GameState
9+
{
10+
MyGame myGame;
11+
12+
public GameState(MyGame myGame)
13+
{
14+
this.myGame = myGame;
15+
}
16+
17+
public abstract void getPoints();
18+
19+
public abstract String getState();
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.state.example3;
7+
8+
public class LosePointState extends GameState
9+
{
10+
public LosePointState(MyGame myGame)
11+
{
12+
super(myGame);
13+
}
14+
15+
@Override
16+
public void getPoints()
17+
{
18+
myGame.setPoints(myGame.points() - 1);
19+
System.out.println("Lose 1 point");
20+
}
21+
22+
@Override
23+
public String getState()
24+
{
25+
return "Lose Point State";
26+
}
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.state.example3;
7+
8+
import java.util.stream.IntStream;
9+
10+
public class MyGame
11+
{
12+
private int points;
13+
14+
private GameState gameState;
15+
16+
private AddPointState ADD_POINT = new AddPointState(this);
17+
private LosePointState LOSE_POINT = new LosePointState(this);
18+
19+
public MyGame(int points)
20+
{
21+
this.points = points;
22+
}
23+
24+
public int points()
25+
{
26+
return points;
27+
}
28+
29+
public void setPoints(int points)
30+
{
31+
this.points = points;
32+
}
33+
34+
public void test()
35+
{
36+
gameState = ADD_POINT;
37+
IntStream.range(0, 3).forEach(i -> gameState.getPoints());
38+
System.out.println(String.format("Current state: %s, point: %d", gameState.getState(), points));
39+
gameState = LOSE_POINT;
40+
IntStream.range(0, 2).forEach(i -> gameState.getPoints());
41+
System.out.println(String.format("Current state: %s, point: %d", gameState.getState(), points));
42+
}
43+
44+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.state.example3;
7+
8+
public class StatePatternDemo
9+
{
10+
public static void main(String[] args)
11+
{
12+
MyGame myGame = new MyGame(0);
13+
myGame.test();
14+
}
15+
}

0 commit comments

Comments
 (0)