UML软件工程组织

 

 

Java设计模式研究之Flyweight模式
 
作者:不详 出处:网络
 

解释一下概念:也就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象。比如说(这里引用gof书中的例子)一个文本系统,每个字母定一个对象,那么大小写字母一共就是52个,那么就要定义52个对象。如果有一个1m的文本,那么字母是何其的多,如果每个字母都定义一个对象那么内存早就爆了。那么如果要是每个字母都共享一个对象,那么就大大节约了资源。

在flyweight模式中,由于要产生各种各样的对象,所以在flyweight(享元)模式中常出现factory模式。flyweight的内部状态是用来共享的,flyweight factory负责维护一个对象存储池(flyweight pool)来存放内部状态的对象。flyweight模式是一个提高程序效率和性能的模式,会大大加快程序的运行速度.应用场合很多,下面举个例子:

先定义一个抽象的flyweight类:

package flyweight;

public abstract class flyweight
...
{
 public abstract void operation();
}//end abstract class flyweight

在实现一个具体类:

package flyweight;

public class concreteflyweight extends flyweight
...
{
 private string string;
 public concreteflyweight(string str)
 ...
 {
  string = str;
 }//end concreteflyweight(...)

 public void operation()
 ...
 {
  system.out.println("concrete---flyweight : " + string);
 }//end operation()

}//end class concreteflyweight

实现一个工厂方法类:

package flyweight;
import java.util.hashtable;

public class flyweightfactory
...
{
 private hashtable flyweights = new hashtable();//----------------------------1
 public flyweightfactory() ...{}

 public flyweight getflyweight(object obj)
 ...
 {
  flyweight flyweight = (flyweight) flyweights.get(obj);//----------------2

  if(flyweight == null) ...{//---------------------------------------------------3
   //产生新的concreteflyweight
   flyweight = new concreteflyweight((string)obj);
   flyweights.put(obj, flyweight);//--------------------------------------5
  }
  return flyweight;//---------------------------------------------------------6
 }//end getflyweight(...)

 public int getflyweightsize()
 ...
 {
  return flyweights.size();
 }
}//end class flyweightfactory

这个工厂方法类非常关键,这里详细解释一下:

在1处定义了一个hashtable用来存储各个对象;在2处选出要实例化的对象,在6处将该对象返回,如果在hashtable中没有要选择的对象,此时变量flyweight为null,产生一个新的flyweight存储在hashtable中,并将该对象返回。

最后看看flyweight的调用:

package flyweight;
import java.util.hashtable;

public class flyweightpattern ...{
 flyweightfactory factory = new flyweightfactory();
 flyweight fly1;
 flyweight fly2;
 flyweight fly3;
 flyweight fly4;
 flyweight fly5;
 flyweight fly6;

 /** *//** creates a new instance of flyweightpattern */
 public flyweightpattern() ...{
  fly1 = factory.getflyweight("google");
  fly2 = factory.getflyweight("qutr");
  fly3 = factory.getflyweight("google");
  fly4 = factory.getflyweight("google");
  fly5 = factory.getflyweight("google");
  fly6 = factory.getflyweight("google");
 }//end flyweightpattern()

 public void showflyweight()
 ...
 {
  fly1.operation();
  fly2.operation();
  fly3.operation();
  fly4.operation();
  fly5.operation();
  fly6.operation();
  int objsize = factory.getflyweightsize();
  system.out.println("objsize = " + objsize);
 }//end showflyweight()

 public static void main(string[] args)
 ...
 {
  system.out.println("the flyweight pattern!");
  flyweightpattern fp = new flyweightpattern();
  fp.showflyweight();
 }//end main(...)
}//end class flyweightpattern

下面是运行结果:

concrete---flyweight : google
concrete---flyweight : qutr
concrete---flyweight : google
concrete---flyweight : google
concrete---flyweight : google
concrete---flyweight : google
objsize = 2

我们定义了6个对象,其中有5个是相同的,按照flyweight模式的定义“google”应该共享一个对象,在实际的对象数中我们可以看出实际的对象却是只有2个。

下面给出一个简易的uml图:

总结:

flyweight(享元)模式是如此的重要,因为它能帮你在一个复杂的系统中大量的节省内存空间。在gof的书中举了文本处理的例子,我觉得非常恰当。那么,在java中string这个类型比较特殊,为什么呢,看下面的例子:

string a = "hello";
string b = "hello";
if(a == b)
 system.out.println("ok");
else
 system.out.println("error");

输出结果是:ok。稍有经验的人都可以看出if条件比较的是两a和b的地址,也可以说是内存空间。那么sting的实现是不是使用了flyweight模式呢,不得而知,到现在还没有研究过。

 

组织简介 | 联系我们 |   Copyright 2002 ®  UML软件工程组织 京ICP备10020922号

京公海网安备110108001071号