该文章是阅读《图解设计模式》的学习笔记。书本链接https://www.ituring.com.cn/book/1811
Flyweight模式中的“轻量级”是指程序所使用的内存大小,使用内存多的对象就是“重”对象,使用内存少的对象就是“轻”对象。而该模式就是通过尽量共享实例来避免new出新实例。来减轻程序使用的内存大小。下面程序通过通过共享0-9这几个字符串图案来输出一个几位数的字符串。假如需要输出123123这个数字字符串,可以共享使用1、2和3这三个数字字符串图案分别共享使用两次。
0到9的数字字符串图案,文件分别命名为big0.txt到big9.txt:
....######......
..##......##....
..##......##....
..##......##....
..##......##....
..##......##....
....######......
................
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................
....######......
..##......##....
..........##....
......####......
..........##....
..##......##....
....######......
................
........##......
......####......
....##..##......
..##....##......
..##########....
........##......
......######....
................
..##########....
..##............
..##............
..########......
..........##....
..##......##....
....######......
................
....######......
..##......##....
..##............
..########......
..##......##....
..##......##....
....######......
................
..##########....
..##......##....
..........##....
........##......
......##........
......##........
......##........
................
....######......
..##......##....
..##......##....
....######......
..##......##....
..##......##....
....######......
................
....######......
..##......##....
..##......##....
....########....
..........##....
..##......##....
....######......
................
命名为big-.txt:
................
................
................
................
..##########....
................
................
................
BigChar类代码:
package com.wen.Flyweight;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BigChar {
// 字符名字
private char charname;
// 大型字符对应的字符串(由'#' '.' '\n'组成)
private String fontdata;
// 构造函数
public BigChar(char charname) {
this.charname = charname;
try {
BufferedReader reader = new BufferedReader(
new FileReader("src/com/wen/Flyweight/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 + "?";
}
}
// 显示大型字符
public void print() {
System.out.print(fontdata);
}
}
BigCharFactory类代码:
package com.wen.Flyweight;
import java.util.HashMap;
public class BigCharFactory {
// 管理已经生成的BigChar的实例
private HashMap pool = new HashMap();
// Singleton模式
private static BigCharFactory singleton = new BigCharFactory();
// 构造函数
private BigCharFactory() {
}
// 获取唯一的实例
public static BigCharFactory getInstance() {
return singleton;
}
// 生成(共享)BigChar类的实例
public synchronized BigChar getBigChar(char charname) {
BigChar bc = (BigChar)pool.get("" + charname);
if (bc == null) {
bc = new BigChar(charname); // 生成BigChar的实例
pool.put("" + charname, bc);
}
return bc;
}
}
BigString类代码:
package com.wen.Flyweight;
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();
}
}
}
Main程序入口类代码:
package com.wen.Flyweight;
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]);
BigString bs = new BigString("123123");
bs.print();
}
}
程序运行结果:
模式中的角色:
FlyWeight(轻量级):表示那些实例会被共享的类,共享实例可以让程序变“轻”。如上面代码中的BigChar类。
FlyweightFactory(轻量级工厂):生成Flyweight角色的工厂,该工厂生产的FlyWeight角色可以实现共享实例。如上面代码中的FlyWeightFactory类。
Client(请求者):负责使用Flyweight来生成Flyweight角色。如上面代码中的BigString类。