Skip to content

Commit 201cef9

Browse files
committed
state added
1 parent 8866cd4 commit 201cef9

File tree

13 files changed

+222
-0
lines changed

13 files changed

+222
-0
lines changed

state/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# state
2+
This is repository of http://androidcode.pl blog. It shows uses of State pattern in Android. It is a part of Design Patterns - State post in the blog.

state/example/Main.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Main {
2+
3+
public static vodi main() {
4+
//user create playlist
5+
List<Track> playlist = new ArrayList<>();
6+
playlist.add(new Track("Depeche Mode", "Enjoy the silence", 250, "dm01.mp3"));
7+
playlist.add(new Track("Abba", "Dancing Queen", 195, "abba07.mp3"));
8+
playlist.add(new Track("Ben Howard", "Keep your head up", 217, "bh04.mp3"));
9+
playlist.add(new Track("Of Monsters and Men", "Yellow Light", 184, "omm12.mp3"));
10+
11+
//user choose playlist to play right now
12+
Player player = new Player(playlist);
13+
14+
//player is initialized and ready to play the first song
15+
player.play(); //Enjoy the silence is playing (StopState)
16+
player.next(); //Dancing Queen is playing (PlayState)
17+
player.stop(); //Dancig Queen is pausing (StopState)
18+
player.previous(); //Enjoy the sielence is loaded again (StopState)
19+
player.play(); //Enjoy the silence is playing (PlayState)
20+
player.previous(); //Enojy the sielence is playing from the beginning (PlayState)
21+
}
22+
}

state/example/Player.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
public class Player {
2+
3+
private State state;
4+
private List<Track> tracks;
5+
private int currentTrack;
6+
//other fields like system or music controllers
7+
8+
public Player(List<Track> tracks){
9+
this.tracks = tracks;
10+
currentTrack = 0;
11+
currentTime = 0;
12+
state = new StopState();
13+
}
14+
15+
public void setState(State state){
16+
this.state = state;
17+
}
18+
19+
public State getState(){
20+
return state;
21+
}
22+
23+
public void play() {
24+
state.play(this);
25+
}
26+
27+
public void next() {
28+
state.next(this);
29+
}
30+
31+
public void previous() {
32+
state.previous(this);
33+
}
34+
35+
public void playTrack() {
36+
//play the currentTrack
37+
//change UI button
38+
}
39+
40+
public void stopTrack() {
41+
//stop the currentTrack
42+
//change UI button
43+
}
44+
45+
public void nextTrack() {
46+
//stop the CurrentTrack
47+
if(currentTrack < tracks.size() - 1)
48+
currentTrack++;
49+
else
50+
currentTrack = 0;
51+
//play the currentTrack
52+
//change UI info
53+
}
54+
55+
public void previousTrack() {
56+
if(currentTrack > 0)
57+
currentTrack = tracks.size() - 1;
58+
else
59+
currentTrack--;
60+
//play the currentTrack
61+
//change UI info
62+
}
63+
}

state/example/dummy/Track.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Track {
2+
3+
private String author;
4+
private String title;
5+
private int lengthSeconds;
6+
private String fileName;
7+
8+
public Track(String author, String title, int lengthSeconds, String fileName) {
9+
this.author = author;
10+
this.title = title;
11+
this.lengthSeconds = lengthSeconds;
12+
this.fileName = fileName;
13+
}
14+
15+
//setter, getter and other methods
16+
}

state/example/states/PlayState.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class PlayState implements State {
2+
3+
//do some things in every methods and run player's method or modify player if needed
4+
5+
@Override
6+
public void play(Player player) {
7+
player.stopTrack();
8+
player.setState(new StopState());
9+
}
10+
11+
@Override
12+
public void next(Player player) {
13+
player.nextTrack();
14+
}
15+
16+
@Override
17+
public void previous(Player player) {
18+
player.playTrack();
19+
}
20+
}

state/example/states/State.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface State {
2+
3+
void play(Player player);
4+
void next(Player player);
5+
void previous(Player player);
6+
}

state/example/states/StopState.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class StopState implements State {
2+
3+
//do some things in every methods and run player's method or modify player if needed
4+
5+
@Override
6+
public void play(Player player) {
7+
player.playTrack();
8+
player.setState(new PlayState());
9+
}
10+
11+
@Override
12+
public void next(Player player) {
13+
player.nextTrack();
14+
}
15+
16+
@Override
17+
public void previous(Player player) {
18+
player.previousTrack();
19+
}
20+
}

state/pattern/Context.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Context {
2+
3+
private State state;
4+
5+
public Context() {
6+
//set default state
7+
state = new State1(this);
8+
}
9+
10+
public void request() {
11+
state.handle(this);
12+
}
13+
14+
public void action1() {
15+
//do something specific for action1
16+
}
17+
18+
public void action2(boolean enabled) {
19+
//do something specific for action2
20+
}
21+
22+
public void setState(State state) {
23+
this.state = state;
24+
}
25+
26+
public State getState() {
27+
return state;
28+
}
29+
}

state/pattern/Main.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Main {
2+
3+
public static void main() {
4+
//start state is State1
5+
Context context = new Context();
6+
7+
//user request operation
8+
context.request(); //State1 handle the action
9+
//operation is done, state has changed
10+
11+
//user again request operation many times during the state is changing
12+
context.request(); //State2 handle the action
13+
context.request(); //State3 handle the action
14+
context.request(); //again State1 handle the action
15+
}
16+
}

state/pattern/states/State.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
interface State {
2+
3+
void handle(Context context);
4+
}

0 commit comments

Comments
 (0)