Skip to content

Commit f5b1178

Browse files
committed
Implements state template
1 parent f719499 commit f5b1178

File tree

8 files changed

+248
-73
lines changed

8 files changed

+248
-73
lines changed

README.md

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,39 @@
11
# ☕ Java Design Patterns
22

3-
## About
3+
## State
44

5-
This application is a simple Java Console Application that aims to implement design pattern examples to a Programmin Language Class. A set of design patterns will be developed
6-
each week, and they are divided in different [branches](https://github.com/LBeghini/Java-Design-Patterns/branches).
5+
State pattern is used when you need to control the state of an object.
76

8-
The main branch is just a template for every other branch.
7+
For example, if we want to create a keep track of the state of a music, that follows the diagram:
98

10-
Also, to make it easier to download the source code, [releases](https://github.com/LBeghini/Java-Design-Patterns/releases) are created related to the task of the week, giving a snapshot of the code for that specific implementation.
9+
<img src="./resources/StateDiagram.png" width="300px" />
1110

12-
## Implemented design patterns
11+
we would implement a class like below:
1312

14-
### Behavioural patterns
13+
```java
14+
public class Music {
15+
private String title;
16+
private String artist;
17+
private MusicState state;
1518

16-
- [ ] Chain of responsability
17-
- [ ] Command
18-
- [ ] Iterator
19-
- [ ] Memento
20-
- [ ] Observer
21-
- [ ] State
22-
- [ ] Strategy
23-
- [ ] Template method
24-
25-
### Creational patterns
26-
27-
- [ ] Abstract factory
28-
- [x] [Builder](https://github.com/LBeghini/Java-Design-Patterns/tree/1-builder)
29-
- [x] [Factory method](https://github.com/LBeghini/Java-Design-Patterns/tree/2-factory-method)
30-
- [x] [Prototype](https://github.com/LBeghini/Java-Design-Patterns/tree/2-prototype)
31-
- [x] [Singleton](https://github.com/LBeghini/Java-Design-Patterns/tree/1-singleton)
32-
33-
### Structural patterns
34-
35-
- [ ] Adapter
36-
- [ ] Bridge
37-
- [ ] Composite
38-
- [ ] Decorator
39-
- [ ] Facade
40-
- [ ] Flyweight
41-
- [ ] Mediator
42-
- [ ] Proxy
43-
44-
## Technologies
45-
46-
- Java
47-
- JUnit
48-
- Maven
49-
50-
## Requirements
51-
52-
To run and edit the project, be sure to have installed in your computer the following softwares:
53-
- A code editor
54-
55-
After that, you'll need to clone this repo:
56-
57-
```bash
58-
git clone https://github.com/LBeghini/Java-Design-Patterns.git
59-
```
60-
61-
## Change branch
62-
63-
To change to a different branch, run the command:
64-
65-
```bash
66-
git checkout name-of-the-branch
19+
// constructor, getters and setters were ommited for simplification
20+
}
6721
```
6822

69-
The branch names have the pattern:
70-
71-
```bash
72-
{number-of-the-week}-{pattern-name}
73-
```
23+
and create a MusicState interface:
7424

75-
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern
25+
```java
26+
public interface MusicState {
7627

77-
## Testing
28+
String getState();
7829

79-
This project has no aim to run any of the implemented classes, as the goal is the code itself. However, the classes will be tested to visualize the behaviour and implementation
80-
of the patterns.
30+
String play(Music music);
8131

82-
You can run the tests using the maven wrapper:
32+
String pause(Music music);
8333

84-
```bash
85-
./mvnw test
86-
```
34+
String skip(Music music);
8735

88-
## :balance_scale: License
36+
String stop(Music music);
37+
}
8938

90-
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE)
39+
```

resources/StateDiagram.png

11.5 KB
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.language.programming.model;
2+
3+
import com.language.programming.model.states.Play;
4+
5+
public class Music {
6+
private String title;
7+
private String artist;
8+
private MusicState state;
9+
10+
public Music(String title, String artist) {
11+
this.title = title;
12+
this.artist = artist;
13+
this.state = Play.getInstance();
14+
}
15+
16+
public String getTitle() {
17+
return title;
18+
}
19+
20+
public void setTitle(String title) {
21+
this.title = title;
22+
}
23+
24+
public String getArtist() {
25+
return artist;
26+
}
27+
28+
public void setArtist(String artist) {
29+
this.artist = artist;
30+
}
31+
32+
public MusicState getState() {
33+
return state;
34+
}
35+
36+
public void setArtist(MusicState state) {
37+
this.state = state;
38+
}
39+
40+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.language.programming.model;
2+
3+
public interface MusicState {
4+
5+
String getState();
6+
7+
String play(Music music);
8+
9+
String pause(Music music);
10+
11+
String skip(Music music);
12+
13+
String stop(Music music);
14+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.language.programming.model.states;
2+
3+
import com.language.programming.model.Music;
4+
import com.language.programming.model.MusicState;
5+
6+
public class Pause implements MusicState {
7+
8+
private static Pause instance = new Pause();
9+
10+
public static Pause getInstance() {
11+
return instance;
12+
}
13+
14+
@Override
15+
public String getState() {
16+
return "⏸";
17+
}
18+
19+
@Override
20+
public String play(Music music) {
21+
// TODO Auto-generated method stub
22+
return null;
23+
}
24+
25+
@Override
26+
public String pause(Music music) {
27+
// TODO Auto-generated method stub
28+
return null;
29+
}
30+
31+
@Override
32+
public String skip(Music music) {
33+
// TODO Auto-generated method stub
34+
return null;
35+
}
36+
37+
@Override
38+
public String stop(Music music) {
39+
// TODO Auto-generated method stub
40+
return null;
41+
}
42+
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.language.programming.model.states;
2+
3+
import com.language.programming.model.Music;
4+
import com.language.programming.model.MusicState;
5+
6+
public class Play implements MusicState {
7+
8+
private static Play instance = new Play();
9+
10+
public static Play getInstance() {
11+
return instance;
12+
}
13+
14+
@Override
15+
public String getState() {
16+
return "▶️";
17+
}
18+
19+
@Override
20+
public String play(Music music) {
21+
// TODO Auto-generated method stub
22+
return null;
23+
}
24+
25+
@Override
26+
public String pause(Music music) {
27+
// TODO Auto-generated method stub
28+
return null;
29+
}
30+
31+
@Override
32+
public String skip(Music music) {
33+
// TODO Auto-generated method stub
34+
return null;
35+
}
36+
37+
@Override
38+
public String stop(Music music) {
39+
// TODO Auto-generated method stub
40+
return null;
41+
}
42+
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.language.programming.model.states;
2+
3+
import com.language.programming.model.Music;
4+
import com.language.programming.model.MusicState;
5+
6+
public class Skip implements MusicState {
7+
8+
private static Skip instance = new Skip();
9+
10+
public static Skip getInstance() {
11+
return instance;
12+
}
13+
14+
@Override
15+
public String getState() {
16+
return "⏭️";
17+
}
18+
19+
@Override
20+
public String play(Music music) {
21+
// TODO Auto-generated method stub
22+
return null;
23+
}
24+
25+
@Override
26+
public String pause(Music music) {
27+
// TODO Auto-generated method stub
28+
return null;
29+
}
30+
31+
@Override
32+
public String skip(Music music) {
33+
// TODO Auto-generated method stub
34+
return null;
35+
}
36+
37+
@Override
38+
public String stop(Music music) {
39+
// TODO Auto-generated method stub
40+
return null;
41+
}
42+
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.language.programming.model.states;
2+
3+
import com.language.programming.model.Music;
4+
import com.language.programming.model.MusicState;
5+
6+
public class Stop implements MusicState {
7+
8+
private static Stop instance = new Stop();
9+
10+
public static Stop getInstance() {
11+
return instance;
12+
}
13+
14+
@Override
15+
public String getState() {
16+
return "⏹️";
17+
}
18+
19+
@Override
20+
public String play(Music music) {
21+
// TODO Auto-generated method stub
22+
return null;
23+
}
24+
25+
@Override
26+
public String pause(Music music) {
27+
// TODO Auto-generated method stub
28+
return null;
29+
}
30+
31+
@Override
32+
public String skip(Music music) {
33+
// TODO Auto-generated method stub
34+
return null;
35+
}
36+
37+
@Override
38+
public String stop(Music music) {
39+
// TODO Auto-generated method stub
40+
return null;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)