本文内容来源于网络收集
作者:冰河    
来源:冰河技术公众号
- 本章难度:★★☆☆☆
- 本章重点:用最简短的篇幅介绍享元模式最核心的知识,理解享元模式的设计精髓,并能够灵活运用到实际项目中,编写可维护的代码。
一、概述
  
    
  
运用共享技术有效地支持大量细粒度的对象。
二、适用性
  
    
  
当都具备下列情况时,使用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对象提供一个已创建的实例或者创建一个实例(如果不存在的话)。
四、类图
  
    
  
五、示例
  
    
  
Flyweight
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
 | /**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description Flyweight
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public interface Flyweight {
    void action(int arg);
}
 | 
 
ConcreteFlyweight
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 | /**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description ConcreteFlyweight
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public class FlyweightImpl implements Flyweight{
    @Override
    public void action(int arg) {
        System.out.println("参数值: " + arg);
    }
}
 | 
 
FlyweightFactory
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 | /**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description FlyweightFactory
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public class FlyweightFactory {
    private static Map<String, Flyweight> flyweights = new HashMap<>();
    public FlyweightFactory(String arg) {
        flyweights.put(arg, new FlyweightImpl());
    }
    public static Flyweight getFlyweight(String key) {
        if (flyweights.get(key) == null) {
            flyweights.put(key, new FlyweightImpl());
        }
        return flyweights.get(key);
    }
    public static int getSize() {
        return flyweights.size();
    }
}
 | 
 
Test
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 | /**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description 测试类
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public class Test {
    public static void main(String[] args) {
        Flyweight fly1 = FlyweightFactory.getFlyweight("a");
        fly1.action(1);
        Flyweight fly2 = FlyweightFactory.getFlyweight("a");
        System.out.println(fly1 == fly2);
        Flyweight fly3 = FlyweightFactory.getFlyweight("b");
        fly3.action(2);
        Flyweight fly4 = FlyweightFactory.getFlyweight("c");
        fly4.action(3);
        Flyweight fly5 = FlyweightFactory.getFlyweight("d");
        fly4.action(4);
        System.out.println(FlyweightFactory.getSize());
    }
}
 | 
 
Result
| 1
2
3
4
5
6
 | 参数值: 1
true
参数值: 2
参数值: 3
参数值: 4
4
 |