当程序中需要大量对象时,如果都是用new关键字来分配内存,将会消耗大量的内存空间。
享元模式(Flyweight Pattern)通过尽量共享实例变量来避免new出实例。
当需要某个实例时,并不总是通过new关键字来生成实例,而是尽量公用已经存在的实例。这就是Flyweight模式的核心内容。
big0.txt(数字0)
....######......
..##......##....
..##......##....
..##......##....
..##......##....
..##......##....
....######......
................
big1.txt(数字1)
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................
big2.txt(数字2)
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................
big3.txt(数字3)
....######......
..##......##....
..........##....
......####......
..........##....
..##......##....
....######......
................
big4.txt(数字4)
........##......
......####......
....##..##......
..##....##......
..##########....
........##......
......######....
................
big5.txt(数字5)
..##########....
..##............
..##............
..########......
..........##....
..##......##....
....######......
................
big6.txt(数字6)
....######......
..##......##....
..##............
..########......
..##......##....
..##......##....
....######......
................
big7.txt(数字7)
..##########....
..##......##....
..........##....
........##......
......##........
......##........
......##........
................
big8.txt(数字8)
....######......
..##......##....
..##......##....
....######......
..##......##....
..##......##....
....######......
................
big9.txt(数字9)
....######......
..##......##....
..##......##....
....########....
..........##....
..##......##....
....######......
................
big-.txt(字符-)
................
................
................
................
..##########....
................
................
................
BigChar.java
package com.test.dp.Flyweight.Sample;
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("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.java
package com.test.dp.Flyweight.Sample;
import java.util.HashMap;
//表示生成和公用BigChar类实例的类
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.java
package com.test.dp.Flyweight.Sample;
//表示多个BigChar组成的“大型字符串”的类
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.java
package com.test.dp.Flyweight.Sample;
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();
}
}
执行结果:
略
总结:
应用实例:1、JAVA 中的 String,如果有则返回,如果没有则创建一个字符串保存在字符串缓存池里面。2、数据库的数据池。
优点:大大减少对象的创建,降低系统的内存,使效率提高。
缺点:提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱。
使用场景:1、系统有大量相似对象。2、需要缓冲池的场景。
注意事项:1、注意划分外部状态和内部状态,否则可能会引起线程安全问题。2、这些类必须有一个工厂对象加以控制。