Skip to content

Commit 325c007

Browse files
committed
Implements flyweight
1 parent 4504cfd commit 325c007

File tree

5 files changed

+110
-87
lines changed

5 files changed

+110
-87
lines changed

README.md

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

3-
## About
3+
## Flyweight
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+
Fliweight pattern defines an object that minimizes memory use by sharing the same object in varios different locations.
76

8-
The main branch is just a template for every other branch.
9-
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] [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-
- [x] [Strategy](https://github.com/LBeghini/Java-Design-Patterns/tree/6-strategy)
23-
- [x] [Template method](https://github.com/LBeghini/Java-Design-Patterns/tree/4-template-method)
24-
- [x] [Mediator](https://github.com/LBeghini/Java-Design-Patterns/tree/9-mediator)
25-
- [x] [Interpreter](https://github.com/LBeghini/Java-Design-Patterns/tree/9-interpreter)
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-
- [ ] Flyweight
43-
- [ ] Proxy
44-
45-
## Technologies
46-
47-
- Java
48-
- JUnit
49-
- Maven
50-
51-
## Requirements
52-
53-
To run and edit the project, be sure to have installed in your computer the following softwares:
54-
- A code editor
55-
56-
After that, you'll need to clone this repo:
57-
58-
```bash
59-
git clone https://github.com/LBeghini/Java-Design-Patterns.git
60-
```
61-
62-
## Change branch
63-
64-
To change to a different branch, run the command:
65-
66-
```bash
67-
git checkout name-of-the-branch
68-
```
69-
70-
The branch names have the pattern:
71-
72-
```bash
73-
{number-of-the-week}-{pattern-name}
74-
```
75-
76-
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern
77-
78-
## Testing
79-
80-
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
81-
of the patterns.
82-
83-
You can run the tests using the maven wrapper:
84-
85-
```bash
86-
./mvnw test
87-
```
88-
89-
## :balance_scale: License
90-
91-
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE)
7+
For example, when creating a characters to a game, a pre defined color is something all characters might share. You can have a character with a red hair and blue eye and yellow skin, and have another character with yellow hair, red eye and blue skin. Even in different attributes, the color is the same, and doesn't need to be kept in memory for every character you create: just create one color (let's say blue) and them for all the objects that uses this color, call the same object.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.language.programming.model;
2+
3+
public class Character {
4+
5+
private String name;
6+
private Color hairColor;
7+
private Color skinColor;
8+
private Color eyeColor;
9+
10+
public Character(String name, String hairColor, String skinColor, String eyeColor) {
11+
this.name = name;
12+
this.hairColor = ColorFlyweight.getColor(hairColor);
13+
this.skinColor = ColorFlyweight.getColor(skinColor);
14+
this.eyeColor = ColorFlyweight.getColor(eyeColor);
15+
}
16+
17+
public String toString() {
18+
return "Character: \n" + " name: " + this.name + "\n hair color: " + this.hairColor + "\n skin color: "
19+
+ this.skinColor + "\n eye color: " + this.eyeColor;
20+
}
21+
22+
}
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+
public class Color {
4+
5+
private String name;
6+
7+
public Color(String name) {
8+
this.name = name;
9+
}
10+
11+
private String getColor() {
12+
return this.name;
13+
}
14+
15+
public String toString() {
16+
return this.name;
17+
}
18+
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.language.programming.model;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class ColorFlyweight {
7+
8+
private static Map<String, Color> colors = new HashMap<>();
9+
10+
public static Color getColor(String name) {
11+
Color color = colors.get(name);
12+
if (color == null) {
13+
color = new Color(name);
14+
colors.put(name, color);
15+
}
16+
return color;
17+
}
18+
19+
public static int getTotalColors() {
20+
return colors.size();
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.language.programming.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeAll;
8+
9+
public class HairColorFlyweightTest {
10+
11+
Character character1;
12+
Character character2;
13+
Character character3;
14+
Character character4;
15+
16+
@BeforeAll
17+
public void setUp() {
18+
Character character1 = new Character("Character1", "Brown", "Red", "Purple");
19+
Character character2 = new Character("Character2", "Red", "Brown", "Blue");
20+
Character character3 = new Character("Character3", "Green", "Yellow", "Gray");
21+
Character character4 = new Character("Character4", "Purple", "Gray", "Red");
22+
}
23+
24+
@Test
25+
public void shouldReturnCharacters() {
26+
27+
assertAll("CharacterList",
28+
() -> Assertions.assertEquals(character1,
29+
"Character:\n name: Character1\n hair color: Brown\n skin color: Red\n eye color: Purple"),
30+
() -> Assertions.assertEquals(character2,
31+
"Character:\n name: Character2\n hair color: Red\n skin color: Brown\n eye color: Blue"),
32+
() -> Assertions.assertEquals(character3,
33+
"Character:\n name: Character3\n hair color: Green\n skin color: Yellow\n eye color: Gray"),
34+
() -> Assertions.assertEquals(character4,
35+
"Character:\n name: Character1\n hair color: Purple\n skin color: Gray\n eye color: Red"));
36+
}
37+
38+
@Test
39+
public void shouldReturnNumberOfInputs() {
40+
41+
assertEquals(7, ColorFlyweight.getTotalColors());
42+
}
43+
44+
}

0 commit comments

Comments
 (0)