设计模式(11)-结构型-享元模式(Flyweight)

概述

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

适用情形

  •     当都具备下列情况时,使用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对象提供一个已创建的实例或者创建一个(如果不存在的话)。
结构图


示例代码:

package com.sql9.structured;

import java.util.ArrayList;
import java.util.List;

abstract class Flyweight 
{
    abstract public void doOperation(int extrinsicState);       
}

class UnsharedConcreteFlyweight extends Flyweight
{
    @Override
    public void doOperation(int extrinsicState)
    {
    }
}

class ConcreteEvenFlyweight extends Flyweight
{
    @Override
    public void doOperation(int extrinsicState)
    {
        System.out.println("In ConcreteEvenFlyweight.DoOperation: " + extrinsicState);                     
    }
}

class ConcreteUnevenFlyweight extends Flyweight
{
    @Override
    public void doOperation(int extrinsicState)
    {
        System.out.println("In ConcreteUnevenFlyweight.DoOperation: " + extrinsicState);           
    }   
}

class FlyweightFactory 
{
    private List<Flyweight> pool = new ArrayList<Flyweight>();

    // the flyweightfactory can crete all entries in the pool at startup 
    // (if the pool is small, and it is likely all will be used), or as 
    // needed, if the pool si large and it is likely some will never be used
    public FlyweightFactory()
    {
        pool.add(new ConcreteEvenFlyweight());      
        pool.add(new ConcreteUnevenFlyweight());            
    }

    public Flyweight getFlyweight(int key)
    {
        // here we would determine if the flyweight identified by key 
        // exists, and if so return it. If not, we would create it. 
        // As in this demo we have implementation all the possible 
        // flyweights we wish to use, we retrun the suitable one. 
        int i = key % 2;
        return((Flyweight)pool.get(i)); 
    }
}

public class FlyweightTest {

    public static void main(String[] args) {
        int[] data = {1,2,3,4,5,6,7,8};
        
        FlyweightFactory f = new FlyweightFactory();
        
        int extrinsicState = 3;
        for (int i : data)
        {
            Flyweight flyweight = f.getFlyweight(i);
            flyweight.doOperation(extrinsicState);
        }
    }
}

结果

In ConcreteUnevenFlyweight.DoOperation: 3
In ConcreteEvenFlyweight.DoOperation: 3
In ConcreteUnevenFlyweight.DoOperation: 3
In ConcreteEvenFlyweight.DoOperation: 3
In ConcreteUnevenFlyweight.DoOperation: 3
In ConcreteEvenFlyweight.DoOperation: 3
In ConcreteUnevenFlyweight.DoOperation: 3
In ConcreteEvenFlyweight.DoOperation: 3




<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iihero_

谢谢打赏,不断前行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值