GOF:運(yùn)用共享技術(shù)有效地支持大量細(xì)粒度的對(duì)象。
解釋一下概念:也就是說在一個(gè)系統(tǒng)中如果有多個(gè)相同的對(duì)象,那么只共享一份就可以了,不必每個(gè)都去實(shí)例化一個(gè)對(duì)象。比如說(這里引用GOF書中的例子)一個(gè)文本系統(tǒng),每個(gè)字母定一個(gè)對(duì)象,那么大小寫字母一共就是52個(gè),那么就要定義52個(gè)對(duì)象。如果有一個(gè)1M的文本,那么字母是何其的多,如果每個(gè)字母都定義一個(gè)對(duì)象那么內(nèi)存早就爆了。那么如果要是每個(gè)字母都共享一個(gè)對(duì)象,那么就大大節(jié)約了資源。
在Flyweight模式中,由于要產(chǎn)生各種各樣的對(duì)象,所以在Flyweight(享元)模式中常出現(xiàn)Factory模式。Flyweight的內(nèi)部狀態(tài)是用來共享的,Flyweight factory負(fù)責(zé)維護(hù)一個(gè)對(duì)象存儲(chǔ)池(Flyweight Pool)來存放內(nèi)部狀態(tài)的對(duì)象。Flyweight模式是一個(gè)提高程序效率和性能的模式,會(huì)大大加快程序的運(yùn)行速度.應(yīng)用場合很多,下面舉個(gè)例子:
先定義一個(gè)抽象的Flyweight類:
在實(shí)現(xiàn)一個(gè)具體類:
實(shí)現(xiàn)一個(gè)工廠方法類:
下面是運(yùn)行結(jié)果:
下面給出一個(gè)簡易的UML圖:

解釋一下概念:也就是說在一個(gè)系統(tǒng)中如果有多個(gè)相同的對(duì)象,那么只共享一份就可以了,不必每個(gè)都去實(shí)例化一個(gè)對(duì)象。比如說(這里引用GOF書中的例子)一個(gè)文本系統(tǒng),每個(gè)字母定一個(gè)對(duì)象,那么大小寫字母一共就是52個(gè),那么就要定義52個(gè)對(duì)象。如果有一個(gè)1M的文本,那么字母是何其的多,如果每個(gè)字母都定義一個(gè)對(duì)象那么內(nèi)存早就爆了。那么如果要是每個(gè)字母都共享一個(gè)對(duì)象,那么就大大節(jié)約了資源。
在Flyweight模式中,由于要產(chǎn)生各種各樣的對(duì)象,所以在Flyweight(享元)模式中常出現(xiàn)Factory模式。Flyweight的內(nèi)部狀態(tài)是用來共享的,Flyweight factory負(fù)責(zé)維護(hù)一個(gè)對(duì)象存儲(chǔ)池(Flyweight Pool)來存放內(nèi)部狀態(tài)的對(duì)象。Flyweight模式是一個(gè)提高程序效率和性能的模式,會(huì)大大加快程序的運(yùn)行速度.應(yīng)用場合很多,下面舉個(gè)例子:
先定義一個(gè)抽象的Flyweight類:
1
package Flyweight;
2
3
public abstract class Flyweight
4

5
{
6
public abstract void operation();
7
}//end abstract class Flyweight

2

3

4


5

6

7

在實(shí)現(xiàn)一個(gè)具體類:
1
package Flyweight;
2
3
public class ConcreteFlyweight extends Flyweight
4

5
{
6
private String string;
7
public ConcreteFlyweight(String str)
8

9
{
10
string = str;
11
}//end ConcreteFlyweight(
)
12
13
public void operation()
14

15
{
16
System.out.println("Concrete---Flyweight : " + string);
17
}//end operation()
18
19
}//end class ConcreteFlyweight

2

3

4


5

6

7

8


9

10

11


12

13

14


15

16

17

18

19

實(shí)現(xiàn)一個(gè)工廠方法類:
1
package Flyweight;
2
import java.util.Hashtable;
3
4
public class FlyweightFactory
5

6
{
7
private Hashtable flyweights = new Hashtable();//----------------------------1
8
public FlyweightFactory()
{}
9
10
public Flyweight getFlyWeight(Object obj)
11

12
{
13
Flyweight flyweight = (Flyweight) flyweights.get(obj);//----------------2
14
15
if(flyweight == null)
{//---------------------------------------------------3
16
//產(chǎn)生新的ConcreteFlyweight
17
flyweight = new ConcreteFlyweight((String)obj);
18
flyweights.put(obj, flyweight);//--------------------------------------5
19
}
20
return flyweight;//---------------------------------------------------------6
21
}//end GetFlyWeight(
)
22
23
public int getFlyweightSize()
24

25
{
26
return flyweights.size();
27
}
28
}//end class FlyweightFactory

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

這個(gè)工廠方法類非常關(guān)鍵,這里詳細(xì)解釋一下:
在1處定義了一個(gè)Hashtable用來存儲(chǔ)各個(gè)對(duì)象;在2處選出要實(shí)例化的對(duì)象,在6處將該對(duì)象返回,如果在Hashtable中沒有要選擇的對(duì)象,此時(shí)變量flyweight為null,產(chǎn)生一個(gè)新的flyweight存儲(chǔ)在Hashtable中,并將該對(duì)象返回。
最后看看Flyweight的調(diào)用:
1
package Flyweight;
2
import java.util.Hashtable;
3
4
public class FlyweightPattern
{
5
FlyweightFactory factory = new FlyweightFactory();
6
Flyweight fly1;
7
Flyweight fly2;
8
Flyweight fly3;
9
Flyweight fly4;
10
Flyweight fly5;
11
Flyweight fly6;
12
13
/** *//** Creates a new instance of FlyweightPattern */
14
public FlyweightPattern()
{
15
fly1 = factory.getFlyWeight("Google");
16
fly2 = factory.getFlyWeight("Qutr");
17
fly3 = factory.getFlyWeight("Google");
18
fly4 = factory.getFlyWeight("Google");
19
fly5 = factory.getFlyWeight("Google");
20
fly6 = factory.getFlyWeight("Google");
21
}//end FlyweightPattern()
22
23
public void showFlyweight()
24

25
{
26
fly1.operation();
27
fly2.operation();
28
fly3.operation();
29
fly4.operation();
30
fly5.operation();
31
fly6.operation();
32
int objSize = factory.getFlyweightSize();
33
System.out.println("objSize = " + objSize);
34
}//end showFlyweight()
35
36
public static void main(String[] args)
37

38
{
39
System.out.println("The FlyWeight Pattern!");
40
FlyweightPattern fp = new FlyweightPattern();
41
fp.showFlyweight();
42
}//end main(
)
43
}//end class FlyweightPattern

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

下面是運(yùn)行結(jié)果:
1
Concrete---Flyweight : Google
2
Concrete---Flyweight : Qutr
3
Concrete---Flyweight : Google
4
Concrete---Flyweight : Google
5
Concrete---Flyweight : Google
6
Concrete---Flyweight : Google
7
objSize = 2
我們定義了6個(gè)對(duì)象,其中有5個(gè)是相同的,按照Flyweight模式的定義“Google”應(yīng)該共享一個(gè)對(duì)象,在實(shí)際的對(duì)象數(shù)中我們可以看出實(shí)際的對(duì)象卻是只有2個(gè)。
2

3

4

5

6

7

下面給出一個(gè)簡易的UML圖:
