【IT168 技术文章】
GOF:运用共享技术有效地支持大量细粒度的对象。
解释一下概念:也就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象。比如说(这里引用GOF书中的例子)一个文本系统,每个字母定一个对象,那么大小写字母一共就是52个,那么就要定义52个对象。如果有一个1M的文本,那么字母是何其的多,如果每个字母都定义一个对象那么内存早就爆了。那么如果要是每个字母都共享一个对象,那么就大大节约了资源。
在Flyweight模式中,由于要产生各种各样的对象,所以在Flyweight(享元)模式中常出现Factory模式。Flyweight的内部状态是用来共享的,Flyweight factory负责维护一个对象存储池(Flyweight Pool)来存放内部状态的对象。Flyweight模式是一个提高程序效率和性能的模式,会大大加快程序的运行速度.应用场合很多,下面举个例子:
先定义一个抽象的Flyweight类:
2
3 public abstract class Flyweight
4
5 ...
6
7 {
8
9 public abstract void operation();
10
11 }//end abstract class Flyweight
12
13
在实现一个具体类:
2
3 public class ConcreteFlyweight extends Flyweight
4
5 ...
6
7 {
8
9 private String string;
10
11 public ConcreteFlyweight(String str)
12
13 ...
14
15 {
16
17 string = str;
18
19 }//end ConcreteFlyweight(...)
20
21 public void operation()
22
23 ...
24
25 {
26
27 System.out.println("Concrete---Flyweight : " + string);
28
29 }//end operation()
30
31 }//end class ConcreteFlyweight
32
33
实现一个工厂方法类:
2
3 import java.util.Hashtable;
4
5 public class FlyweightFactory
6
7 ...
8
9 {
10
11 private Hashtable flyweights = new Hashtable();//----------------------------1
12
13 public FlyweightFactory() ...{}
14
15 public Flyweight getFlyWeight(Object obj)
16
17 ...
18
19 {
20
21 Flyweight flyweight = (Flyweight) flyweights.get(obj);//----------------2
22
23 if(flyweight == null) ...{//---------------------------------------------------3
24
25 //产生新的ConcreteFlyweight
26
27 flyweight = new ConcreteFlyweight((String)obj);
28
29 flyweights.put(obj, flyweight);//--------------------------------------5
30
31 }
32
33 return flyweight;//---------------------------------------------------------6
34
35 }//end GetFlyWeight(...)
36
37 public int getFlyweightSize()
38
39 ...
40
41 {
42
43 return flyweights.size();
44
45 }
46
47 }//end class FlyweightFactory
48
49
这个工厂方法类非常关键,这里详细解释一下:
在1处定义了一个Hashtable用来存储各个对象;在2处选出要实例化的对象,在6处将该对象返回,如果在Hashtable中没有要选择的对象,此时变量flyweight为null,产生一个新的flyweight存储在Hashtable中,并将该对象返回。
最后看看Flyweight的调用:
2
3 import java.util.Hashtable;
4
5 public class FlyweightPattern ...{
6
7 FlyweightFactory factory = new FlyweightFactory();
8
9 Flyweight fly1;
10
11 Flyweight fly2;
12
13 Flyweight fly3;
14
15 Flyweight fly4;
16
17 Flyweight fly5;
18
19 Flyweight fly6;
20
21 /** *//** Creates a new instance of FlyweightPattern */
22
23 public FlyweightPattern() ...{
24
25 fly1 = factory.getFlyWeight("Google");
26
27 fly2 = factory.getFlyWeight("Qutr");
28
29 fly3 = factory.getFlyWeight("Google");
30
31 fly4 = factory.getFlyWeight("Google");
32
33 fly5 = factory.getFlyWeight("Google");
34
35 fly6 = factory.getFlyWeight("Google");
36
37 }//end FlyweightPattern()
38
39 public void showFlyweight()
40
41 ...
42
43 {
44
45 fly1.operation();
46
47 fly2.operation();
48
49 fly3.operation();
50
51 fly4.operation();
52
53 fly5.operation();
54
55 fly6.operation();
56
57 int objSize = factory.getFlyweightSize();
58
59 System.out.println("objSize = " + objSize);
60
61 }//end showFlyweight()
62
63 public static void main(String[] args)
64
65 ...
66
67 {
68
69 System.out.println("The FlyWeight Pattern!");
70
71 FlyweightPattern fp = new FlyweightPattern();
72
73 fp.showFlyweight();
74
75 }//end main(...)
76
77 }//end class FlyweightPattern
78
79
下面是运行结果:
2
3 Concrete---Flyweight : Qutr
4
5 Concrete---Flyweight : Google
6
7 Concrete---Flyweight : Google
8
9 Concrete---Flyweight : Google
10
11 Concrete---Flyweight : Google
12
13 objSize = 2
14
15
我们定义了6个对象,其中有5个是相同的,按照Flyweight模式的定义“Google”应该共享一个对象,在实际的对象数中我们可以看出实际的对象却是只有2个。
下面给出一个简易的UML图:
总结:
Flyweight(享元)模式是如此的重要,因为它能帮你在一个复杂的系统中大量的节省内存空间。在GOF的书中举了文本处理的例子,我觉得非常恰当。那么,在Java中String这个类型比较特殊,为什么呢,看下面的例子:
2
3 String b = "hello";
4
5 if(a == b)
6
7 System.out.println("OK");
8
9 else
10
11 System.out.println("Error");
12
13
输出结果是:OK。稍有经验的人都可以看出if条件比较的是两a和b的地址,也可以说是内存空间。那么Sting的实现是不是使用了Flyweight模式呢,不得而知,到现在还没有研究过。