-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package flyweight; | ||
|
||
public interface Note { | ||
|
||
public String simbol(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |