Skip to content

Commit e9f1790

Browse files
committed
Implements visitor
1 parent dbc21a1 commit e9f1790

File tree

8 files changed

+159
-87
lines changed

8 files changed

+159
-87
lines changed

README.md

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

3-
## About
3+
## Visitor
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+
Visitor pattern is a way to perform operations of a determinated structure build outside the structure.
76

8-
The main branch is just a template for every other branch.
7+
For example, let's say we have a Museum, and we want to see the masterpieces. However, the arts don't come with an exhibit method, and you don't have the rights to change anything inside the classes.
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.
11-
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-
- [x] [Command](https://github.com/LBeghini/Java-Design-Patterns/tree/6-command)
18-
- [x] [Interpreter](https://github.com/LBeghini/Java-Design-Patterns/tree/9-interpreter)
19-
- [x] [Iterator](https://github.com/LBeghini/Java-Design-Patterns/tree/4-iterator)
20-
- [x] [Mediator](https://github.com/LBeghini/Java-Design-Patterns/tree/9-mediator)
21-
- [x] [Memento](https://github.com/LBeghini/Java-Design-Patterns/tree/5-memento)
22-
- [x] [Observer](https://github.com/LBeghini/Java-Design-Patterns/tree/5-observer)
23-
- [x] [State](https://github.com/LBeghini/Java-Design-Patterns/tree/3-state)
24-
- [x] [Strategy](https://github.com/LBeghini/Java-Design-Patterns/tree/6-strategy)
25-
- [x] [Template method](https://github.com/LBeghini/Java-Design-Patterns/tree/4-template-method)
26-
27-
### Creational patterns
28-
29-
- [ ] Abstract factory
30-
- [x] [Builder](https://github.com/LBeghini/Java-Design-Patterns/tree/1-builder)
31-
- [x] [Factory method](https://github.com/LBeghini/Java-Design-Patterns/tree/2-factory-method)
32-
- [x] [Prototype](https://github.com/LBeghini/Java-Design-Patterns/tree/2-prototype)
33-
- [x] [Singleton](https://github.com/LBeghini/Java-Design-Patterns/tree/1-singleton)
34-
35-
### Structural patterns
36-
37-
- [x] [Adapter](https://github.com/LBeghini/Java-Design-Patterns/tree/7-adapter)
38-
- [x] [Bridge](https://github.com/LBeghini/Java-Design-Patterns/tree/7-bridge)
39-
- [x] [Composite](https://github.com/LBeghini/Java-Design-Patterns/tree/8-composite)
40-
- [ ] Decorator
41-
- [x] [Facade](https://github.com/LBeghini/Java-Design-Patterns/tree/8-facade)
42-
- [x] [Flyweight](https://github.com/LBeghini/Java-Design-Patterns/tree/10-flyweight)
43-
- [x] [Proxy](https://github.com/LBeghini/Java-Design-Patterns/tree/10-proxy)
44-
45-
46-
## Technologies
47-
48-
- Java
49-
- JUnit
50-
- Maven
51-
52-
## Requirements
53-
54-
To run and edit the project, be sure to have installed in your computer the following softwares:
55-
- A code editor
56-
57-
After that, you'll need to clone this repo:
58-
59-
```bash
60-
git clone https://github.com/LBeghini/Java-Design-Patterns.git
61-
```
62-
63-
## Change branch
64-
65-
To change to a different branch, run the command:
66-
67-
```bash
68-
git checkout name-of-the-branch
69-
```
70-
71-
The branch names have the pattern:
72-
73-
```bash
74-
{number-of-the-week}-{pattern-name}
75-
```
76-
77-
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern
78-
79-
## Testing
80-
81-
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
82-
of the patterns.
83-
84-
You can run the tests using the maven wrapper:
85-
86-
```bash
87-
./mvnw test
88-
```
89-
90-
## :balance_scale: License
91-
92-
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE)
9+
So you use the Museum as a Visitor to get the exhibitions of the masterpieces.

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
<version>5.7.1</version>
3232
<scope>test</scope>
3333
</dependency>
34+
<dependency>
35+
<groupId>com.google.code.gson</groupId>
36+
<artifactId>gson</artifactId>
37+
<version>2.8.5</version>
38+
</dependency>
3439
</dependencies>
3540

3641
<build>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.language.programming.model;
2+
3+
public interface Art {
4+
5+
String accept(Visitor visitor);
6+
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.language.programming.model;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.language.programming.model.visitors.Painting;
6+
import com.language.programming.model.visitors.Sculpture;
7+
8+
public class Museum implements Visitor {
9+
10+
private static Museum instance = new Museum();
11+
12+
private Museum() {};
13+
14+
public static Museum getInstance() {
15+
return instance;
16+
}
17+
18+
public String exhibit(Art art) {
19+
return art.accept(this);
20+
}
21+
22+
@Override
23+
public String exhibit(Painting painting) {
24+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
25+
return gson.toJson(painting);
26+
}
27+
28+
@Override
29+
public String exhibit(Sculpture sculpture) {
30+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
31+
return gson.toJson(sculpture);
32+
}
33+
34+
35+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.language.programming.model;
2+
3+
import com.language.programming.model.visitors.Painting;
4+
import com.language.programming.model.visitors.Sculpture;
5+
6+
public interface Visitor {
7+
8+
String exhibit(Painting painting);
9+
String exhibit(Sculpture sculpture);
10+
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.language.programming.model.visitors;
2+
3+
import com.language.programming.model.Art;
4+
import com.language.programming.model.Visitor;
5+
6+
public class Painting implements Art {
7+
8+
private String name;
9+
private String artist;
10+
11+
public Painting(String name, String artist) {
12+
this.name = name;
13+
this.artist = artist;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getArtist() {
25+
return artist;
26+
}
27+
28+
public void setArtist(String artist) {
29+
this.artist = artist;
30+
}
31+
32+
@Override
33+
public String accept(Visitor visitor) {
34+
return visitor.exhibit(this);
35+
}
36+
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.language.programming.model.visitors;
2+
3+
import com.language.programming.model.Art;
4+
import com.language.programming.model.Visitor;
5+
6+
public class Sculpture implements Art{
7+
8+
private String name;
9+
private String artist;
10+
11+
public Sculpture(String name, String artist) {
12+
this.name = name;
13+
this.artist = artist;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getArtist() {
25+
return artist;
26+
}
27+
28+
public void setArtist(String artist) {
29+
this.artist = artist;
30+
}
31+
32+
@Override
33+
public String accept(Visitor visitor) {
34+
return visitor.exhibit(this);
35+
}
36+
37+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.visitors.*;
8+
9+
public class MuseumTest {
10+
11+
@Test
12+
public void shouldExhibitPainting() {
13+
Painting painting = new Painting("Mona Lisa", "Leonardo da Vinci");
14+
assertEquals("{\n \"name\": \"Mona Lisa\",\n \"artist\": \"Leonardo da Vinci\"\n}", Museum.getInstance().exhibit(painting));
15+
}
16+
17+
@Test
18+
public void shouldExhibitSculpture() {
19+
Sculpture sculpture = new Sculpture("The Thinker", "Auguste Rodin");
20+
assertEquals("{\n \"name\": \"The Thinker\",\n \"artist\": \"Auguste Rodin\"\n}", Museum.getInstance().exhibit(sculpture));
21+
}
22+
23+
}

0 commit comments

Comments
 (0)