【图解设计模式】Flyweight模式

通过尽量共享实例来避免new出实例。

示例

将许多普通字符组合成为“大型字符”。

类图

mTBO1O.png

BigChar类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BigChar {
private char charname;
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类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.HashMap;

public class BigCharFactory {
private HashMap pool = new HashMap();
private static BigCharFactory singleton = new BigCharFactory();

private BigCharFactory() {}

public static BigCharFactory getInstance() {
return singleton;
}

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;
}
}

BigString类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class BigString {
private BigChar[] bigChars;

public BigString(String string) {
bigChars = new BigChar[string.length()];
BigCharFactory factory = BigCharFactory.getInstance();

for (int i = 0; i < string.length(); i++)
bigChars[i] = factory.getBigChar(string.charAt(i));
}

public void print() {
for (int i = 0; i < bigChars.length; i++)
bigChars[i].print();
}
}

Main类

1
2
3
4
5
6
7
8
9
10
11
12
13
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................
....######......
..##......##....
..........##....
......####......
..........##....
..##......##....
....######......
................

登场角色

Flyweight(轻量级)

按照通常方式编写程序会导致程序变重,所以如果能够共享实例会比较好,而Flyweight角色表示的就是那些实例会被共享的类。在示例程序中,由BigChar类扮演此角色。

FlyweightFactory(轻量级工厂)

FlyweightFactory角色是生成Flyweight角色的工厂。在工厂中生成Flyweight角色可以实现共享实例。在示例程序中,由BigCharFactory类扮演此角色。

Client(请求者)

Client角色使用FlyweightFactory角色来生成Flyweight角色。在示例程序中,由BigString类扮演此角色。

类图

m74D1J.png