`

Design Pattern: Flyweight 模式

阅读更多

Flyweight(享元)模式定义:
避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类).

为什么使用?
面向对象语言的原则就是一切都是对象,但是如果真正使用起来,有时对象数可能显得很庞大,比如,字处理软件,如果以每个文字都作为一个对象,几千个字,对象数就是几千,无疑耗费内存,那么我们还是要"求同存异",找出这些对象群的共同点,设计一个元类,封装可以被共享的类,另外,还有一些特性是取决于应用(context),是不可共享的,这也Flyweight中两个重要概念内部状态intrinsic和外部状态extrinsic之分.

Flyweight在牛津字典中的解释是"boxer of the lightest class"。意思是特轻量级拳击手?其实应该是取"the lightest class"这部份的解释,一个特轻量级类别,这个类别所产生的物件可以共用在每一个场合(context),并依场合资讯表现物件外观。

在书中所举出的例子是文档编辑器中的字元物件,若每个字元物件会包括字元、大小、字型等等不同的资讯,想想一篇文章中可能出现多少字元,如果我们为每一个字元都使用一个物件来完整描述有关于它的讯息,那么一篇文字中将会耗用多少的记忆体?!字元本身应可以共享,而大小、字型等等不同的资讯再分别设定。

考虑数量多且性质相近的物件时,将该物件的资讯分为两个部份:内部状态(intrinsic)与外部状态(extrinsic)。以上例来说,字元属于内部状态,而大小、字型等等不同的资讯属于外部状态。

更详细一些来说明,内部状态是物件可共享的讯息部份,例如在绘制一个英文字串时,重覆的字元部份为内部状态,像是 "ABC is BAC",其中A、B、C的字元资讯部份不必直接储存于字元物件中,它是属于可以共享的部份,可以将这些可以重复使用的字元储存在Flyweight Pool中。

外部状态是物件依赖的一个场景(context),例如绘制字元时的字型资讯、位置资讯等等,绘制一个字元时,先从Flyweight Pool中找出共享的Flyweight,然后从场景中查找对应的绘制资讯(字型、大小、位置等)。

其实任何学过Java的人就一定使用过Java中运用Flyweight模式的好处,要知道,如果您在程式中使用下面的方式来宣告,则实际上是指向同一个字串物件:

String str1 = "flyweight";
String str2 = "flyweight";
System.out.println(str1 == str2);

 
程式的执行结果会显示True,在Java中,会维护一个String Pool,对于一些可以共享的字串物件,会先在String Pool中查找是否存在相同的String内容(字元相同),如果有就直接传回,而不是直接创造一个新的String物件,以减少记忆体的耗用。

再来个一看例子,String的intern()方法,我们来看看它的API说明的节录:

Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.


这段话其实已说明了Flyweight模式的运作方式,用个实例来说明会更清楚:

  • Main.java
public class Main { 
    public static void main(String[] args) { 
        String str1 = "fly"; 
        String str2 = "weight"; 
        String str3 = "flyweight"; 
        String str4; 

        str4 = str1 + str2; 
        System.out.println(str3 == str4); 

        str4 = (str1 + str2).intern(); 
        System.out.println(str3 == str4); 
    } 
}


在程式中第一次比较str3与str4物件是否为同一物件时,您知道结果会是false,而intern()方法会先检查 String Pool中是否存在字元部份相同的字串物件,如果有的话就传回,由于程式中之前已经有"flyweight"字串物件,intern()在String Pool中发现了它,所以直接传回,这时再进行比较,str3与str4所指向的其实是同一物件,所以结果会是true。

说白点,就是先捏一个的原始模型,然后随着不同场合和环境,再产生各具特征的具体模型,很显然,在这里需要产生不同的新对象,所以Flyweight模式中常出现Factory模式.Flyweight的内部状态是用来共享的,Flyweight factory负责维护一个Flyweight pool(模式池)来存放内部状态的对象.

Flyweight模式是一个提高程序效率和性能的模式,会大大加快程序的运行速度.应用场合很多:比如你要从一个数据库中读取一系列字符串,这些字符串中有许多是重复的,那么我们可以将这些字符串储存在Flyweight池(pool)中.

如何使用?


我们先从Flyweight抽象接口开始:


public interface Flyweight
{
  public void operation( ExtrinsicState state );
}

//用于本模式的抽象数据类型(自行设计)
public interface ExtrinsicState { }
 

下面是接口的具体实现(ConcreteFlyweight) ,并为内部状态增加内存空间, ConcreteFlyweight必须是可共享的,它保存的任何状态都必须是内部(intrinsic),也就是说,ConcreteFlyweight必须和它的应用环境场合无关.

public class ConcreteFlyweight implements Flyweight {
  private IntrinsicState state;
  
  public void operation( ExtrinsicState state )
  {
      //具体操作
  }
}
 

当然,并不是所有的Flyweight具体实现子类都需要被共享的,所以还有另外一种不共享的ConcreteFlyweight:

public class UnsharedConcreteFlyweight implements Flyweight {

  public void operation( ExtrinsicState state ) { }

}
 

Flyweight factory负责维护一个Flyweight池(存放内部状态),当客户端请求一个共享Flyweight时,这个factory首先搜索池中是否已经有可适用的,如果有,factory只是简单返回送出这个对象,否则,创建一个新的对象,加入到池中,再返回送出这个对象.池


public class FlyweightFactory {
  //Flyweight pool
  private Hashtable flyweights = new Hashtable();

  public Flyweight getFlyweight( Object key ) {


    Flyweight flyweight = (Flyweight) flyweights.get(key);


    if( flyweight == null ) {
      //产生新的ConcreteFlyweight
      flyweight = new ConcreteFlyweight();
      flyweights.put( key, flyweight );
    }


    return flyweight;
  }
}
 

至此,Flyweight模式的基本框架已经就绪,我们看看如何调用:

FlyweightFactory factory = new FlyweightFactory();
Flyweight fly1 = factory.getFlyweight( "Fred" );
Flyweight fly2 = factory.getFlyweight( "Wilma" );
......


从调用上看,好象是个纯粹的Factory使用,但奥妙就在于Factory的内部设计上.

Flyweight模式在XML等数据源中应用
我们上面已经提到,当大量从数据源中读取字符串,其中肯定有重复的,那么我们使用Flyweight模式可以提高效率,以唱片CD为例,在一个XML文件中,存放了多个CD的资料.

每个CD有三个字段:
1.出片日期(year)
2.歌唱者姓名等信息(artist)
3.唱片曲目 (title)

其中,歌唱者姓名有可能重复,也就是说,可能有同一个演唱者的多个不同时期 不同曲目的CD.我们将"歌唱者姓名"作为可共享的ConcreteFlyweight.其他两个字段作为UnsharedConcreteFlyweight.

首先看看数据源XML文件的内容:


<?xml version="1.0"?>
<collection>


<cd>
<title>Another Green World</title>
<year>1978</year>
<artist>Eno, Brian</artist>
</cd>


<cd>
<title>Greatest Hits</title>
<year>1950</year>
<artist>Holiday, Billie</artist>
</cd>


<cd>
<title>Taking Tiger Mountain (by strategy)</title>
<year>1977</year>
<artist>Eno, Brian</artist>
</cd>


.......

</collection>

 


虽然上面举例CD只有3张,CD可看成是大量重复的小类,因为其中成分只有三个字段,而且有重复的(歌唱者姓名).

CD就是类似上面接口 Flyweight:


public class CD {

  private String title;
  private int year;
  private Artist artist;

  public String getTitle() {  return title; }
  public int getYear() {    return year;  }
  public Artist getArtist() {    return artist;  }

  public void setTitle(String t){    title = t;}
  public void setYear(int y){year = y;}
  public void setArtist(Artist a){artist = a;}

}
 

将"歌唱者姓名"作为可共享的ConcreteFlyweight:

public class Artist {


  //内部状态
  private String name;

  // note that Artist is immutable.
  String getName(){return name;}


  Artist(String n){
    name = n;
  }

}

 

再看看Flyweight factory,专门用来制造上面的可共享的ConcreteFlyweight:Artist

public class ArtistFactory {

  Hashtable pool = new Hashtable();

  Artist getArtist(String key){


    Artist result;
    result = (Artist)pool.get(key);
    ////产生新的Artist
    if(result == null) {
      result = new Artist(key);
      pool.put(key,result);
      
    }
    return result;
  }

}

 

当你有几千张甚至更多CD时,Flyweight模式将节省更多空间,共享的flyweight越多,空间节省也就越大.

 

 

分享到:
评论

相关推荐

    36种最新设计模式整理

    Design Pattern: Flyweight 模式 46 Design Pattern: Proxy 模式(一) 48 Design Pattern: Proxy 模式(二) 49 Design Pattern: Chain of Responsibility 模式 53 Design Pattern: Command 模式 59 Design Pattern...

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    单例模式源码java-DesignPattern:在个人自学阶段的23种设计模式代码的全部实现,全部使用Java编写,其中还包括各个设计模式在

    DesignPattern 在个人自学阶段的23种设计模式代码的全部实现,全部使用Java编写,其中还包括各个设计模式在源码中的使用,每种设计模式都举了一个简单的小例子来进行实现,并加以注释 包名解释 一、DesignPattern ...

    designPatterns:设计模式

    design pattern 设计模式 创建型设计模式 创建型模式设计到将对象实例化,这类模式都提供了一个方法,将客户从所需要的实例化对象中解耦。 原型模式 Prototype 结构型设计模式 结构型模式可以让你把类或者对象组合到...

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    设计模式 design pattern

    4.6 Flyweight(享元)—对象结构型 模式 128 4.7 Proxy(代理)—对象结构型 模式 137 4.8 结构型模式的讨论 144 4.8.1 Adapter与Bridge 144 4.8.2 Composite、Decorator与Proxy 145 第5章 行为模式 147 5.1 CHAIN ...

    uu-design-pattern:23种设计模式案例

    |- singleton 单例模式案例 |- structural(结构型模式) |- facade 外观模式案例 |- decorator 装饰器模式案例 |- adapter 适配器模式案例 |- flyweight 享元模式案例 |- composite 组合模式案例

    design-pattern-java.pdf

    扩展系统功能——装饰模式(三) 扩展系统功能——装饰模式(四) 外观模式-Facade Pattern 深入浅出外观模式(一) 深入浅出外观模式(二) 深入浅出外观模式(三) 享元模式-Flyweight Pattern 实现对象的复用——...

    [源代码] 《易学 设计模式》 随书源代码

    第15章 源源不断:享元模式 (Flyweight) 第16章 按部就班:模板方法模式 (TemplateMethod) 第17章 风吹草动:观察者模式 (Observer) 第18章 变化多端:状态模式 (State) 第19章 明修栈道,暗度陈仓:策略模式 ...

    C#设计模式-吕震宇

    设计模式(14)-Flyweight Pattern C#设计模式(13)-Proxy Pattern C#设计模式(12)-Decorator Pattern C#设计模式(11)-Composite Pattern C#设计模式(10)-Adapter Pattern C#设计模式(9)-Prototype ...

    design-patterns:设计模式

    享元模式(flyweight pattern) 代理模式(代理模式) 行为模式(行为模式) 责任链模式(责任链模式) 命令模式 拦截器模式(解释器模式) 迭代器模式(迭代器模式) 调停者模式(调解员模式) 备忘录模式(memento...

    Java.Design.Patterns.1537192353

    FLYWEIGHT PATTERN TEMPLATE METHOD PATTERN MEDIATOR PATTERN CHAIN OF RESPONSIBILITY ... OBSERVER PATTERN STRATEGY PATTERN COMMAND PATTERN VISITOR PATTERN STATE PATTERN ITERATOR PATTERN INTERPRETER ...

    DesignPatterns:设计模式

    设计模式设计模式:可重用的面向对象软件的元素栏目:如何构建“运行”: Microsoft Visual Studio 2017 v15.7或更高版本Microsoft .Net Framework v4.7或更高版本Microsoft Visual C ++ 2017年

    Design.Patterns.Explained.Simply

    Flyweight Private Class Data Proxy Behavioral patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Null Object Observer State Strategy Template Method Visitor

    Design Patterns Elements of Reusable Object-Oriented Software

    • What Is a Design Pattern? • Design Patterns in Smalltalk MVC • Describing Design Patterns • The Catalog of Design Patterns • Organizing the Catalog • How Design Patterns Solve Design ...

    管理系统javasal源码-Design-Patterns-Demo:超全的设计模式——理论+实现demo

    管理系统java sal源码 [toc] 设计模式 源码地址: 博客地址: 分类 序号 模式 & ...创建型模式:这些设计模式提供了一种在创建对象的同时隐藏创建逻辑...Pattern)享元模式(Flyweight Pattern)代理模式(Proxy Pattern)

    oh-my-design-patterns::artist_palette:记录我在学习设计模式时编写的文章和代码

    装饰模式(Decorator Pattern) Java Kotlin外观模式(Facade Pattern) Java享元模式(Flyweight Pattern) Java代理模式(Proxy Pattern) Java Kotlin行为型(Behavioral)解释器模式(Interpreter Pattern) ...

    CoreJava-DesignPattern

    CoreJava-DesignPattern 创意设计模式 -- Abstract Factory - Done -- Builder - Done -- Factory Method -- Object Pool -- Prototype - Done -- Singleton - Done 结构设计模式 -- Adapter -- Bridge -- ...

    design patterns elements of reusable object-oriented software

    ★附录A介绍了剩下的设计模式:Bridge(桥接)、Builder(生成器)、Chainof Responsibility(责任链)、Flyweight(蝇量)、Interpreter(解释器)、Mediator(中介者)、Memento(备忘录)、Prototype(原型)、...

Global site tag (gtag.js) - Google Analytics