设计模式-享元模式

享元模式

定义

享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。它提供了减少对象数量从而改善应用所需的对象结构的方式。

享元模式尝试重用现有的同类对象,如果未找到匹配的对象,则创建新对象。

类图

享元模式类图

Flyweight(轻量级)

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

FlyweightFactory(轻量级工厂)

FlyweightFactory是生成Flyweight的工厂,在工厂中生成Flyweight可以实现共享实例

Client(请求者)

使用FlyweightFactory来生成Flyweight。

示例

类图

享元模式示例类图

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

1
2
3
4
5
6
7
8
9
10
11
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();
}
}

big-.txt

1
2
3
4
5
6
7
8
9
................
................
................
................
..##########....
................
................
................

big0.txt

1
2
3
4
5
6
7
8
9
....######......
..##......##....
..##......##....
..##......##....
..##......##....
..##......##....
....######......
................

big1.txt

1
2
3
4
5
6
7
8
9
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................

big2.txt

1
2
3
4
5
6
7
8
9
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................

big3.txt

1
2
3
4
5
6
7
8
9
....######......
..##......##....
..........##....
......####......
..........##....
..##......##....
....######......
................

big4.txt

1
2
3
4
5
6
7
8
9
........##......
......####......
....##..##......
..##....##......
..##########....
........##......
......######....
................

big5.txt

1
2
3
4
5
6
7
8
9
..##########....
..##............
..##............
..########......
..........##....
..##......##....
....######......
................

big6.txt

1
2
3
4
5
6
7
8
9
....######......
..##......##....
..##............
..########......
..##......##....
..##......##....
....######......
................

big7.txt

1
2
3
4
5
6
7
8
9
..##########....
..##......##....
..........##....
........##......
......##........
......##........
......##........
................

big8.txt

1
2
3
4
5
6
7
8
9
....######......
..##......##....
..##......##....
....######......
..##......##....
..##......##....
....######......
................

big9.txt

1
2
3
4
5
6
7
8
9
....######......
..##......##....
..##......##....
....########....
..........##....
..##......##....
....######......
................

0%