We will summarize the ** Flyweight pattern ** in the GoF design pattern.
――The English word Flyweight means ** flyweight **. Represents the lightest weight class in boxing. --The weight in the Flyweight pattern is the memory usage. --The Flyweight pattern is a method that ** shares instances as much as possible to reduce memory usage **. --The GoF design patterns are classified as ** structural design patterns **.
A program that reads large text from a file and displays it.
A class that represents large letters.
BigChar.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BigChar {
//Character name
private char charname;
//A character string that represents a large character('#' '.' '\n'Column)
private String fontdata;
public BigChar(char charname) {
this.charname = charname;
try {
BufferedReader reader = new BufferedReader(
new FileReader("big" + charname + ".txt"));
String line;
StringBuffer buf = new StringBuffer();
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append("\n");
}
reader.close();
this.fontdata = buf.toString();
} catch (IOException e) {
this.fontdata = charname + "?";
}
}
//Display large characters
public void print() {
System.out.print(fontdata);
}
}
A class that is created while sharing an instance of BigChar.
BigCharFactory.java
import java.util.HashMap;
public class BigCharFactory {
//Manage instances of BigChar that you have already created
private HashMap pool = new HashMap();
private static BigCharFactory singleton = new BigCharFactory();
private BigCharFactory() {
}
//Get only one instance
public static BigCharFactory getInstance() {
return singleton;
}
//BigChar instantiation(share)
public synchronized BigChar getBigChar(char charname) {
BigChar bc = (BigChar) pool.get("" + charname);
if (bc == null) {
bc = new BigChar(charname);
pool.put("" + charname, bc);
}
return bc;
}
}
It is a class that represents a "large character string" created by collecting BigChar.
BigString.java
public class BigString {
private BigChar[] bigchars;
public BigString(String string) {
bigchars = new BigChar[string.length()];
BigCharFactory factory = BigCharFactory.getInstance();
for (int i = 0; i < bigchars.length; i++) {
bigchars[i] = factory.getBigChar(string.charAt(i));
}
}
public void print() {
for (int i = 0; i < bigchars.length; i++) {
bigchars[i].print();
}
}
}
This class performs the main processing.
Main.java
public class Main {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java Main digits");
System.out.println("Example: java Main 1212123");
System.exit(0);
}
BigString bs = new BigString(args[0]);
bs.print();
}
}
Please refer to the following txt file. https://github.com/i-tanaka730/design_pattern/tree/master/src/Flyweight
..##########....
..##......##....
..........##....
........##......
......##........
......##........
......##........
................
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................
..##########....
..##......##....
..........##....
........##......
......##........
......##........
......##........
................
..##########....
..##............
..##............
..########......
..........##....
..##......##....
....######......
................。
Sharing an instance reduces memory usage because you don't have to renew each time. More generally, sharing an instance can reduce the amount of ** resources ** required to create an instance. A resource is a resource on a computer, and memory is a type of resource. Time is also a resource. If it takes a certain amount of time to new an instance, you can reduce the number of new instances by sharing the instance using the Flyweight pattern. This can speed up the program.
-** GoF design pattern summary **
This article and sample program were created based on the following books.
-** Introduction to design patterns learned in Java language **
It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.
Recommended Posts