Skip to content

Commit 1aca2fa

Browse files
committed
Implements command
1 parent d7ad582 commit 1aca2fa

File tree

8 files changed

+176
-84
lines changed

8 files changed

+176
-84
lines changed

README.md

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

3-
## About
3+
## Command
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+
Command pattern stores requests as command objects that perform actions or undo it at a later time.
76

8-
The main branch is just a template for every other branch.
7+
An artist is a person that makes lots of mistakes before succeed on his artwork. It's useful for him to be able to keep track of his improvements and undo it if necessary.
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+
For example, if the artist is writing a book, he needs to be able to write, and undo it's last writing until he finds the best phrase to continue the history. So it can perform an action of write and cancel.
1110

12-
## Implemented design patterns
13-
14-
### Behavioural patterns
15-
16-
- [x] [Chain of responsibility](https://github.com/LBeghini/Java-Design-Patterns/tree/4-chain-of-responsibility)
17-
- [ ] Command
18-
- [x] [Iterator](https://github.com/LBeghini/Java-Design-Patterns/tree/4-iterator)
19-
- [x] [Memento](https://github.com/LBeghini/Java-Design-Patterns/tree/5-memento)
20-
- [x] [Observer](https://github.com/LBeghini/Java-Design-Patterns/tree/5-observer)
21-
- [x] [State](https://github.com/LBeghini/Java-Design-Patterns/tree/3-state)
22-
- [ ] Strategy
23-
- [x] [Template method](https://github.com/LBeghini/Java-Design-Patterns/tree/4-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
67-
```
68-
69-
The branch names have the pattern:
70-
71-
```bash
72-
{number-of-the-week}-{pattern-name}
73-
```
74-
75-
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern
76-
77-
## Testing
78-
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.
81-
82-
You can run the tests using the maven wrapper:
83-
84-
```bash
85-
./mvnw test
86-
```
87-
88-
## :balance_scale: License
89-
90-
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.language.programming.model;
2+
3+
public interface Action {
4+
5+
void execute();
6+
7+
void cancel();
8+
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.language.programming.model;
2+
3+
import java.util.Stack;
4+
5+
public class Artist {
6+
7+
private Stack<Action> actions = new Stack<>();
8+
9+
public void executeAction(Action action) {
10+
this.actions.add(action);
11+
action.execute();
12+
}
13+
14+
public void cancelLastAction() {
15+
Action lastAction = actions.pop();
16+
lastAction.cancel();
17+
}
18+
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.language.programming.model;
2+
3+
import com.language.programming.model.enumerations.Condition;
4+
5+
public class Book {
6+
7+
private Condition condition;
8+
9+
public Book() {
10+
this.condition = Condition.EMPTY;
11+
}
12+
13+
public String getCondition() {
14+
return this.condition.toString();
15+
}
16+
17+
public void write() {
18+
this.condition = Condition.PROGRESS;
19+
}
20+
21+
public void erase() {
22+
this.condition = Condition.REGRESS;
23+
}
24+
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.language.programming.model.actions;
2+
3+
import com.language.programming.model.Book;
4+
import com.language.programming.model.Action;
5+
6+
public class Erase implements Action {
7+
8+
private Book book;
9+
10+
public Erase(Book book) {
11+
this.book = book;
12+
}
13+
14+
@Override
15+
public void execute() {
16+
this.book.erase();
17+
}
18+
19+
@Override
20+
public void cancel() {
21+
this.book.write();
22+
}
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.language.programming.model.actions;
2+
3+
import com.language.programming.model.Book;
4+
import com.language.programming.model.Action;
5+
6+
public class Write implements Action {
7+
8+
private Book book;
9+
10+
public Write(Book book) {
11+
this.book = book;
12+
}
13+
14+
@Override
15+
public void execute() {
16+
this.book.write();
17+
}
18+
19+
@Override
20+
public void cancel() {
21+
this.book.erase();
22+
}
23+
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.language.programming.model.enumerations;
2+
3+
public enum Condition {
4+
PROGRESS("Writing..."), REGRESS("Erasing..."), EMPTY("Not yet written.");
5+
6+
private final String description;
7+
8+
private Condition(String description) {
9+
this.description = description;
10+
}
11+
12+
public boolean equalsName(String name) {
13+
return description.equals(name);
14+
}
15+
16+
public String toString() {
17+
return this.description;
18+
}
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.language.programming.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
import com.language.programming.model.actions.Erase;
8+
import com.language.programming.model.actions.Write;
9+
10+
import org.junit.jupiter.api.BeforeEach;
11+
12+
public class PoetTest {
13+
14+
Book book;
15+
Artist artist;
16+
17+
@BeforeEach
18+
void setUp() {
19+
book = new Book();
20+
artist = new Artist();
21+
22+
}
23+
24+
@Test
25+
public void shouldWriteBook() {
26+
Action write = new Write(book);
27+
artist.executeAction(write);
28+
29+
assertEquals("Writing...", book.getCondition());
30+
}
31+
32+
@Test
33+
public void shouldEraseBook() {
34+
Action erase = new Erase(book);
35+
artist.executeAction(erase);
36+
37+
assertEquals("Erasing...", book.getCondition());
38+
}
39+
40+
@Test
41+
public void shouldCancelEraseBook() {
42+
Action write = new Write(book);
43+
Action erase = new Erase(book);
44+
45+
artist.executeAction(write);
46+
artist.executeAction(erase);
47+
artist.cancelLastAction();
48+
49+
assertEquals("Writing...", book.getCondition());
50+
}
51+
52+
}

0 commit comments

Comments
 (0)