1.概述
享元模式(Flyweight),运用共享技术有效地支持大量细粒度的对象。
2.示例
Flyweight 类,它是所有具体享元类的超类或接口,通过这个接口,Flyweight可以接受并作用于外部状态。
Flyweight.java
public abstract class Flyweight {
public abstract void operation(int extrinsicState);
}
ConcreteFlyweight 是继承 Flyweight 超类或实现 Flyweight 接口,并为内部状态增加存储空间。
ConcreteFlyweight.java
public class ConcreteFlyweight extends Flyweight{
@Override
public void operation(int extrinsicState) {
System.out.println("ConcreteFlyweight: " + extrinsicState);
}
}
UnsharedConcreteFlyweigh t是指那些不需要共享的 Flyweight 子类。因为 Flyweight 接口共享成为可能,但它并不强制共享。
UnsharedConcreteFlyweight.java
public class UnsharedConcreteFlyweight extends Flyweight {
@Override
public void operation(int extrinsicState) {
System.out.println("UnsharedConcreteFlyweight: " + extrinsicState);
}
}
FlyweightFactory,是一个享元工厂,用来创建并管理 Flyweight 对象。它主要是用来确保合理地共享 Flyweight,当用户请求一个 Flyweight 时,FlyweightFactory 对象提供一个已创建的实例或者创建一个(如果不存在的话)。
FlyweightFactory.java
public class FlyweightFactory {
private Map<String, Flyweight> flyweights = new HashMap<>();
// 初始化工厂时,先生成三个实例
public FlyweightFactory() {
flyweights.put("X", new ConcreteFlyweight());
flyweights.put("Y", new ConcreteFlyweight());
flyweights.put("Z", new ConcreteFlyweight());
}
// 根据客户端请求,获得已生成的实例
public Flyweight getFlyweight(String key) {
return flyweights.get(key);
}
}
客户端代码。
Client.java
public class Client {
public static void main(String[] args) {
// 代码外部状态
int extrinsicState = 22;
FlyweightFactory factory = new FlyweightFactory();
Flyweight fx = factory.getFlyweight("X");
fx.operation(--extrinsicState);
Flyweight fy = factory.getFlyweight("Y");
fy.operation(--extrinsicState);
Flyweight fz = factory.getFlyweight("Z");
fz.operation(--extrinsicState);
Flyweight fu = new UnsharedConcreteFlyweight();
fu.operation(--extrinsicState);
}
}
输出如下:
ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18
Process finished with exit code 0
3.总结
- 享元模式可以避免大量非常相似类的开销。在程序设计中,有时需要生成大量细粒度的类实例来表示数据。如果能发现这些实例除了几个参数外基本上都是相同的,有时就能够受大幅度地减少需要实例化的类的数量。如果能把那些参数移到类实例的外面,在方法调用时将它们传递进来,就可以通过共享大幅度地减少单个实例的数目。
- 就知道你会问这样的问题,如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就应该考虑使用;还有就是对象的大多数状态可以外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象,此时可以考虑使用享元模式。