Skip to content

Commit

Permalink
Add Flyweight
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Mar 19, 2020
1 parent c71c868 commit e5f8fa4
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ Project that implements some design patterns using Java 13.
- Decorator
- State
- Builder
- Observer
- Observer
- Factory
- Flyweight
29 changes: 29 additions & 0 deletions src/flyweight/Annotation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
###############################
# Flyweight #
###############################





##### Language - EN

- Flyweight helps us to create objects and pass on the same object, if I wanted to play a song with 5 "fa" notes I use the same object,
that way we save memory.

- Graphical applications generally make use of this pattern, since they have a lot of repeated objects.

- The JVM itself makes use of a Flyweight internally. When you declare an "int", and repeat the same "int" value in several places,
it always returns the same instance of that number. It is a good example of implementing the standard.



##### Language - PT-BR

- Flyweight nos ajuda a criar objetos e repassar o mesmo objeto, caso eu queria tocar uma música com 5 notas "fa" eu uso o mesmo objeto,
dessa forma economizamos memória.

- Aplicações gráficas geralmente fazem uso desse padrão, já que elas tem muito objeto repetido.

- A própria JVM faz uso de um Flyweight internamente. Quando você declara um "int", e repete o mesmo valor de "int" em vários lugares,
ela sempre devolve a mesma instância desse número. É um bom exemplo de implementação do padrão.
32 changes: 32 additions & 0 deletions src/flyweight/FlyweightMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package flyweight;

import java.util.Arrays;
import java.util.List;

public class FlyweightMain {

public static void main(String[] args) {
System.out.println("--------------------------------------------- Begin Test Flyweight");

MusicalNotes notes = new MusicalNotes();

// Note = "fa" -> it will be the same object, using Flyweight will always return the same object,
// so there is no need to instantiate many objects at once.
List<Note> music = Arrays.asList(
notes.getNote("do"),
notes.getNote("re"),
notes.getNote("mi"),
notes.getNote("fa"),
notes.getNote("fa"),
notes.getNote("fa"));

System.out.println("Array music: " + music);// Fa@ ..... will point to the same instantiated object

Piano piano = new Piano();

piano.play(music);//unmute your computer to listen to the music

System.out.println("--------------------------------------------- End Test Flyweight");
}

}
34 changes: 34 additions & 0 deletions src/flyweight/MusicalNotes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package flyweight;

import java.util.HashMap;
import java.util.Map;

import flyweight.notes.Do;
import flyweight.notes.Fa;
import flyweight.notes.La;
import flyweight.notes.Mi;
import flyweight.notes.Re;
import flyweight.notes.Si;
import flyweight.notes.Sol;

public class MusicalNotes {

private static Map<String, Note> notes = new HashMap<>();

// static constructor of the class, it is executed when the JVM loads that class. It runs only once.
static {
notes.put("do", new Do());
notes.put("re", new Re());
notes.put("mi", new Mi());
notes.put("fa", new Fa());
notes.put("sol", new Sol());
notes.put("la", new La());
notes.put("si", new Si());
}

// If I want to get a note, the method returns the same instance of that note, so there is no need to instantiate X objects of the same type.
public Note getNote(String name) {
return notes.get(name);
}

}
7 changes: 7 additions & 0 deletions src/flyweight/Note.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package flyweight;

public interface Note {

public String simbol();

}
22 changes: 22 additions & 0 deletions src/flyweight/Piano.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package flyweight;

import java.util.List;

import org.jfugue.player.Player;

public class Piano implements PlayMusic {

public void play(List<Note> music) {
Player player = new Player(); // For player -> Example String: "D D E F F"

StringBuilder musicInNote = new StringBuilder();
for (Note note : music) {
musicInNote.append(note.simbol() + " ");
}

System.out.println("Music Notes: " + musicInNote.toString());

player.play(musicInNote.toString());
}

}
9 changes: 9 additions & 0 deletions src/flyweight/PlayMusic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package flyweight;

import java.util.List;

public interface PlayMusic {

public void play(List<Note> music);

}
12 changes: 12 additions & 0 deletions src/flyweight/notes/Do.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package flyweight.notes;

import flyweight.Note;

public class Do implements Note {

@Override
public String simbol() {
return "C";
}

}
12 changes: 12 additions & 0 deletions src/flyweight/notes/Fa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package flyweight.notes;

import flyweight.Note;

public class Fa implements Note {

@Override
public String simbol() {
return "F";
}

}
12 changes: 12 additions & 0 deletions src/flyweight/notes/La.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package flyweight.notes;

import flyweight.Note;

public class La implements Note {

@Override
public String simbol() {
return "A";
}

}
12 changes: 12 additions & 0 deletions src/flyweight/notes/Mi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package flyweight.notes;

import flyweight.Note;

public class Mi implements Note {

@Override
public String simbol() {
return "E";
}

}
12 changes: 12 additions & 0 deletions src/flyweight/notes/Re.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package flyweight.notes;

import flyweight.Note;

public class Re implements Note {

@Override
public String simbol() {
return "D";
}

}
12 changes: 12 additions & 0 deletions src/flyweight/notes/Si.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package flyweight.notes;

import flyweight.Note;

public class Si implements Note {

@Override
public String simbol() {
return "B";
}

}
12 changes: 12 additions & 0 deletions src/flyweight/notes/Sol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package flyweight.notes;

import flyweight.Note;

public class Sol implements Note {

@Override
public String simbol() {
return "G";
}

}

0 comments on commit e5f8fa4

Please sign in to comment.