设计模式 ~ 结构型模式 ~ 享元模式 ~ Flyweight Pattern。
概述。
定义。
运用共享技术来有效地支持大量细粒度对象的复用。它通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似对象的开销,从而提高系统资源的利用率。
结构。
享元(Flyweight )模式中存在以下两种状态。
- 内部状态,即不会随着环境的改变而改变的可共享部分。
- 外部状态,指随环境改变而改变的不可以共享的部分。享元模式的实现要领就是区分应用中的这两种状态,并将外部状态外部化。
享元模式的主要有以下角色。
-
抽象享元角色(Flyweight)。
通常是一个接口或抽象类,在抽象享元类中声明了具体享元类公共的方法,这些方法可以向外界提供享元对象的内部数据(内部状态),同时也可以通过这些方法来设置外部数据(外部状态)。 -
具体享元(Concrete Flyweight)。
ta 实现了抽象享元类,称为享元对象;在具体享元类中为内部状态提供了存储空间。通常我们可以结合单例模式来设计具体享元类,为每一个具体享元类提供唯一的享元对象。 -
非享元(Unsharable Flyweight)。
并不是所有的抽象享元类的子类都需要被共享,不能被共享的子类可设计为非共享具体享元类;当需要一个非共享具体享元类的对象时可以直接通过实例化创建。 -
享元工厂(Flyweight Factory)角色。
负责创建和管理享元角色。当客户对象请求一个享元对象时,享元工厂检査系统中是否存在符合要求的享元对象,如果存在则提供给客户;如果不存在的话,则创建一个新的享元对象。
案例实现。
【eg.】俄罗斯方块。
下面的图片是众所周知的俄罗斯方块中的一个个方块,如果在俄罗斯方块这个游戏中,每个不同的方块都是一个实例对象,这些对象就要占用很多的内存空间,下面利用享元模式进行实现。
先来看类图。
代码如下。
俄罗斯方块有不同的形状,我们可以对这些形状向上抽取出 AbstractClassBox,用来定义共性的属性和行为。
package com.geek.flyweight.pattern;
/**
* 抽象享元角色。
*
* @author geek
*/
public abstract class AbstractClassBox {
/**
* 形状。
*
* @return
*/
abstract String getShape();
/**
* 展示。
*
* @param color
*/
public void display(String color) {
System.out.println("方块形状 ~ " + this.getShape() + "。颜色 ~ " + color);
}
}
接下来就是定义不同的形状了,IBox 类、LBox 类、OBox 类等。
package com.geek.flyweight.pattern;
/**
* I 图形类 ~ 具体享元角色。
*
* @author geek
*/
public class IBox extends AbstractClassBox {
/**
* 形状。
*
* @return
*/
@Override
public String getShape() {
return "I";
}
}
package com.geek.flyweight.pattern;
/**
* L 图形类 ~ 具体享元角色。
*
* @author geek
*/
public class LBox extends AbstractClassBox {
/**
* 形状。
*
* @return
*/
@Override
public String getShape() {
return "L";
}
}
package com.geek.flyweight.pattern;
/**
* @author geek
*/
public class OBox extends AbstractClassBox {
/**
* 形状。
*
* @return
*/
@Override
public String getShape() {
return "O";
}
}
提供了一个工厂类(BoxFactory),用来管理享元对象(也就是 AbstractBox 子类对象),该工厂类对象只需要一个,所以可以使用单例模式。并给工厂类提供一个获取形状的方法。
package com.geek.flyweight.pattern;
import java.util.HashMap;
/**
* 工厂类。将该类设计为单例。饿汉式。
*
* @author geek
*/
public class BoxFactory {
private static final BoxFactory FACTORY = new BoxFactory();
private static HashMap<String, AbstractClassBox> map;
/**
* 在构造方法中进行初始化操作。
*/
private BoxFactory() {
map = new HashMap<String, AbstractClassBox>();
AbstractClassBox iBox = new IBox();
AbstractClassBox lBox = new LBox();
AbstractClassBox oBox = new OBox();
map.put("I", iBox);
map.put("L", lBox);
map.put("O", oBox);
}
/**
* 提供一个方法获取该工厂类对象。
*
* @return
*/
public static BoxFactory getInstance() {
return FACTORY;
}
/**
* 根据名称获取图形对象。
*
* @param name
* @return
*/
public AbstractClassBox getShape(String name) {
return map.get(name);
}
}
package com.geek.flyweight.pattern;
/**
* @author geek
*/
public class Client {
public static void main(String[] args) {
// 获取 I 图形对象。
AbstractClassBox iBox = BoxFactory.getInstance().getShape("I");
iBox.display("红色");
// 获取 O 图形对象。
AbstractClassBox oBox = BoxFactory.getInstance().getShape("O");
oBox.display("橙色");
// 获取 L 图形对象。
AbstractClassBox lBox = BoxFactory.getInstance().getShape("L");
lBox.display("黄色");
// 获取 L 图形对象。
AbstractClassBox lBox1 = BoxFactory.getInstance().getShape("L");
lBox.display("绿色");
System.out.println("lBox == lBox1 = " + (lBox == lBox1));
}
}
/*
Connected to the target VM, address: '127.0.0.1:59797', transport: 'socket'
方块形状 ~ I。颜色 ~ 红色
方块形状 ~ O。颜色 ~ 橙色
方块形状 ~ L。颜色 ~ 黄色
方块形状 ~ L。颜色 ~ 绿色
lBox == lBox1 = true
Disconnected from the target VM, address: '127.0.0.1:59797', transport: 'socket'
Process finished with exit code 0
*/
优缺点和使用场景。
优点。
-
极大减少内存中相似或相同对象数量,节约系统资源,提供系统性能。
-
享元模式中的外部状态相对独立,且不影响内部状态。
缺点。
为了使对象可以共享,需要将享元对象的部分状态外部化,分离内部状态和外部状态,使程序逻辑复杂。
使用场景。
-
一个系统有大量相同或者相似的对象,造成内存的大量耗费。
-
对象的大部分状态都可以外部化,可以将这些外部状态传入对象中。
-
在使用享元模式时需要维护一个存储享元对象的享元池,而这需要耗费一定的系统资源,因此,应当在需要多次重复使用享元对象时才值得使用享元模式。
JDK 源码解析。
Integer 类使用了享元模式。我们先看下面的例子。
package com.geek.flyweight_pattern;
/**
* @author geek
*/
public class Demo {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
System.out.println("i1 == i2 = " + (i1 == i2));
Integer i3 = 128;
Integer i4 = 128;
System.out.println("i3 == i4 = " + (i3 == i4));
}
}
/*
Connected to the target VM, address: '127.0.0.1:52993', transport: 'socket'
i1 == i2 = true
i3 == i4 = false
Disconnected from the target VM, address: '127.0.0.1:52993', transport: 'socket'
Process finished with exit code 0
*/
为什么第一个输出语句输出的是 true,第二个输出语句输出的是 false?通过反编译软件进行反编译,代码如下。
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: Demo.java
package com.geek.flyweight.pattern;
import java.io.PrintStream;
public class Demo
{
public Demo()
{
}
public static void main(String args[])
{
Integer i1 = Integer.valueOf(127);
Integer i2 = Integer.valueOf(127);
System.out.println((new StringBuilder()).append("i1 == i2 = ").append(i1 == i2).toString());
Integer i3 = Integer.valueOf(128);
Integer i4 = Integer.valueOf(128);
System.out.println((new StringBuilder()).append("i3 == i4 = ").append(i3 == i4).toString());
}
}
上面代码可以看到,直接给 Integer 类型的变量赋值基本数据类型数据的操作底层使用的是 valueOf()
,所以只需要看该方法即可。
public final class Integer extends Number implements Comparable<Integer> {
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
}
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
可以看到 Integer
默认先创建并缓存 -128 ~ 127
之间数的 Integer
对象,当调用 valueOf
时如果参数在 -128 ~ 127
之间则计算下标并从缓存中返回,否则创建一个新的 Integer
对象。