Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1276492
  • 博文数量: 185
  • 博客积分: 50
  • 博客等级: 民兵
  • 技术积分: 3934
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-11 13:11
个人简介

iihero@ChinaUnix, ehero.[iihero] 数据库技术的痴迷爱好者. 您可以通过iihero AT qq.com联系到我 以下是我的三本图书: Sybase ASE in Action, Oracle Spatial及OCI高级编程, Java2网络协议内幕

文章分类

全部博文(185)

文章存档

2014年(4)

2013年(181)

分类: 项目管理

2013-07-25 10:02:52

概述

运用共享技术有效地支持大量细粒度的对象。

适用情形

  •     当都具备下列情况时,使用Flyweight模式:
  •     1.一个应用程序使用了大量的对象。
  •     2.完全由于使用大量的对象,造成很大的存储开销。
  •     3.对象的大多数状态都可变为外部状态。
  •     4.如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。
  •     5.应用程序不依赖于对象标识。由于Flyweight对象可以被共享,对于概念上明显有别的对象,标识测试将返回真值。

参与者

  •     1.Flyweight      描述一个接口,通过这个接口flyweight可以接受并作用于外部状态。
  •     2.ConcreteFlyweight
  •       实现Flyweight接口,并为内部状态(如果有的话)增加存储空间。      ConcreteFlyweight对象必须是可共享的。它所存储的状态必须是内部的;即,它必须独立于ConcreteFlyweight对象的场景。
  •     3.UnsharedConcreteFlyweight      并非所有的Flyweight子类都需要被共享。Flyweight接口使共享成为可能,但它并不强制共享。      在Flyweight对象结构的某些层次,UnsharedConcreteFlyweight对象通常将ConcreteFlyweight对象作为子节点。
  •     4.FlyweightFactory      创建并管理flyweight对象。      确保合理地共享flyweight。当用户请求一个flyweight时,FlyweightFactory对象提供一个已创建的实例或者创建一个(如果不存在的话)。
结构图


示例代码:

  1. package com.sql9.structured;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. abstract class Flyweight   
  7. {  
  8.     abstract public void doOperation(int extrinsicState);         
  9. }  
  10.   
  11. class UnsharedConcreteFlyweight extends Flyweight  
  12. {  
  13.     @Override  
  14.     public void doOperation(int extrinsicState)  
  15.     {  
  16.     }  
  17. }  
  18.   
  19. class ConcreteEvenFlyweight extends Flyweight  
  20. {  
  21.     @Override  
  22.     public void doOperation(int extrinsicState)  
  23.     {  
  24.         System.out.println("In ConcreteEvenFlyweight.DoOperation: " + extrinsicState);                       
  25.     }  
  26. }  
  27.   
  28. class ConcreteUnevenFlyweight extends Flyweight  
  29. {  
  30.     @Override  
  31.     public void doOperation(int extrinsicState)  
  32.     {  
  33.         System.out.println("In ConcreteUnevenFlyweight.DoOperation: " + extrinsicState);             
  34.     }     
  35. }  
  36.   
  37. class FlyweightFactory   
  38. {  
  39.     private List pool = new ArrayList();  
  40.   
  41.     // the flyweightfactory can crete all entries in the pool at startup   
  42.     // (if the pool is small, and it is likely all will be used), or as   
  43.     // needed, if the pool si large and it is likely some will never be used  
  44.     public FlyweightFactory()  
  45.     {  
  46.         pool.add(new ConcreteEvenFlyweight());        
  47.         pool.add(new ConcreteUnevenFlyweight());              
  48.     }  
  49.   
  50.     public Flyweight getFlyweight(int key)  
  51.     {  
  52.         // here we would determine if the flyweight identified by key   
  53.         // exists, and if so return it. If not, we would create it.   
  54.         // As in this demo we have implementation all the possible   
  55.         // flyweights we wish to use, we retrun the suitable one.   
  56.         int i = key % 2;  
  57.         return((Flyweight)pool.get(i));   
  58.     }  
  59. }  
  60.   
  61. public class FlyweightTest {  
  62.   
  63.     public static void main(String[] args) {  
  64.         int[] data = {1,2,3,4,5,6,7,8};  
  65.           
  66.         FlyweightFactory f = new FlyweightFactory();  
  67.           
  68.         int extrinsicState = 3;  
  69.         for (int i : data)  
  70.         {  
  71.             Flyweight flyweight = f.getFlyweight(i);  
  72.             flyweight.doOperation(extrinsicState);  
  73.         }  
  74.     }  
  75. }  

结果

  1. In ConcreteUnevenFlyweight.DoOperation: 3  
  2. In ConcreteEvenFlyweight.DoOperation: 3  
  3. In ConcreteUnevenFlyweight.DoOperation: 3  
  4. In ConcreteEvenFlyweight.DoOperation: 3  
  5. In ConcreteUnevenFlyweight.DoOperation: 3  
  6. In ConcreteEvenFlyweight.DoOperation: 3  
  7. In ConcreteUnevenFlyweight.DoOperation: 3  
  8. In ConcreteEvenFlyweight.DoOperation: 3  

阅读(1788) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~