首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计形式-结构模式-享元模式-Java

2013-03-10 
设计模式--结构模式--享元模式--JavaFlyweightIntent 目的Use sharing to support large numbers of fine-

设计模式--结构模式--享元模式--Java

FlyweightIntent 目的Use sharing to support large numbers of fine-grained objects efficiently
利用共享,使大量细粒度的对象得到有效的利用

Applicability 适用性Anapplication uses a large number of objects
应用程序大量使用一些对象

Storagecosts are high because of the sheer quantity of objects
存储大量对象开销大

Mostobject state can be made extrinsic
大多数对象的状态可以由外部提供

Manygroupsof objects may be replaced by relatively few shared objects once extrinsicstate is removed
假设不包含外部状态,大量的对象可由少量共享的对象取代

UML
设计形式-结构模式-享元模式-Java
Participants 参与者Flyweightdeclaresan interface through which flyweights can receive and act on extrinsic state
定义一个接口,可以用于flyweight接收外部状态

ConcreteFlyweightimplementsthe Flyweight interface and adds storage for intrinsic state, if any. AConcreteFlyweight object must be sharable. Any state it stores must beintrinsic; that is, it must be independent of the ConcreteFlyweight object'scontext

实现flyweight接口,为内部状态提供存储空间。 ConcreteFlyweight 必须是可共享的,这些内部状态必须独立于ConcreteFlyweight 对象上下文。

UnsharedConcreteFlyweight 
notall Flyweight subclasses need to be shared. The Flyweight interfaceenablessharing; it doesn't enforce it. It's common forUnsharedConcreteFlyweight objectsto have ConcreteFlyweight objects as children at some level in the flyweightobject structure (as the Row and Column classes have)

并不是所有的Flyweight子类都需要共享。在F l y w e i g h t对象结构的某些层次,U n s h a r e d C o n c r e t e F ly w e i g h t对象通常将C o n c r e t e F l y w e i g h t对象作为子节点

FlyweightFactory

createsand manages flyweight objects. ensures that flyweights are shared properly.When a client requests  a flyweight, the FlyweightFactory object supplies an existinginstance or creates one, if none exists.

 创建和管理flyweight 对象。确保flyweights 对象恰当的共享。当客户端需要一个flyweight对象时,工厂返回一个存在的实例,如果不存在,创建一个并返回

Client

maintainsa reference to flyweight(s). computes or stores the extrinsic state offlyweight(s).

 持有flyweight(s引用,计算或者存储flyweight外部状态

Tank War exampleexm1     游戏中会用到享元模式,譬如Tank发射的炮弹,发射一次new一次,当炮弹超出屏幕或者打到敌军坦克,炮弹便销毁了,游戏异常激烈,而VM每隔一段时间会进行一次垃圾处理,所以对性能上有所影响(这个原因在小游戏中是次要的),主要原因为同一种炮弹都包含了一些固定不变的信息,如炮弹的速度,边缘,等,主要变化的是位置,方向与存活状态。     这个时候我们会想到这不就可以利用享元模式么!     下面是我曾经做的游戏的局部代码,化繁为简,便于阅读理解:
@Overridepublic void shoot(Point2D.Double centerPoint, Direction direction) {setPoint(centerPoint);setDirection(direction);setAlive(true);}

       首先设置位置,其次方向,最后比较重要的是将其激活。
       综上所述,利用了flyweight模式,显而易见地可以减少垃圾对象,减轻gc负担,另一方面使大量细粒度的对象得到有效的利用。





热点排行