一、概念
运用共享技术有效地支持大量细粒度对象的复用。它通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似类的开销,从而提高系统资源的利用率。
享元模式把对象的信息分为两个部分:内部状态和外部状态。
- 内部状态指对象共享出来的信息,存储在享元信息内部,并且不会随环境的改变而改变;
- 外部状态指对象得以依赖的一个标记,随环境的改变而改变,不可共享。
适用性
当都具备下列情况时,使用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。
当客户对象请求一个享元对象时,享元工厂检査系统中是否存在符合要求的享元对象,如果存在则提供给客户;如果不存在的话,则创建一个新的享元对象。
二、代码示例
/**
* 抽象享元角色
*/
public abstract class Flyweight {
// 注入非享元角色
public abstract void operation(UnSharedConcreteFlyweight unshared);
}
/**
* 享元工厂角色
*/
public class FlyweightFactory {
/**
* Map对象存储享元角色
*/
private Map<String, Flyweight> map = new HashMap<String, Flyweight>();
/**
* 创建享元角色
*/
public Flyweight getFlyweight(String key) {
if (!map.containsKey(key)) {
map.put(key, new ConcreteFlyweight(key));
}
return map.get(key);
}
public int getSize() {
return map.size();
}
}
/**
* 具体享元角色
*/
public class ConcreteFlyweight extends Flyweight{
private String key;
public ConcreteFlyweight(String key) {
this.key = key;
}
@Override
public void operation(UnSharedConcreteFlyweight unshared) {
System.out.println("具体享元 " + key + " 被调用");
System.out.println("非享元信息: " + unshared.getName());
System.out.println();
}
}
/**
* 非享元角色接口
*/
public interface UnSharedConcreteFlyweight {
String getName();
}
/**
* 非享元角色
*/
public class UnsharedConcreteFlyweight1 implements UnSharedConcreteFlyweight {
private String name;
public UnsharedConcreteFlyweight1(String name) {
this.name = name;
}
@Override
public String getName() {
return name + "来战!!!";
}
}
/**
* 非享元角色
*/
public class UnsharedConcreteFlyweight2 implements UnSharedConcreteFlyweight {
private String name;
public UnsharedConcreteFlyweight2(String name) {
this.name = name;
}
@Override
public String getName() {
return name + "进场!!!";
}
}
public class Client {
public static void main(String[] args) {
// 创建享元工厂
FlyweightFactory factory = new FlyweightFactory();
// 从享元工厂获取享元角色,如果没有就创建一个,工厂保持享元角色长久存在
Flyweight flyweight1 = factory.getFlyweight("决战紫禁之巅");
// 享元角色注入非享元角色
flyweight1.operation(new UnsharedConcreteFlyweight1("叶孤城"));
flyweight1.operation(new UnsharedConcreteFlyweight1("西门吹雪"));
flyweight1.operation(new UnsharedConcreteFlyweight1("Gold Moustache"));
flyweight1.operation(new UnsharedConcreteFlyweight1("MiniSter"));
// 从享元工厂获取享元角色,如果没有就创建一个,工厂保持享元角色长久存在
Flyweight flyweight2 = factory.getFlyweight("公交车之抢座大战");
flyweight2.operation(new UnsharedConcreteFlyweight2("一号大爷"));
flyweight2.operation(new UnsharedConcreteFlyweight2("二号大妈"));
flyweight2.operation(new UnsharedConcreteFlyweight2("三号大妈"));
flyweight2.operation(new UnsharedConcreteFlyweight2("四号大爷"));
flyweight2.operation(new UnsharedConcreteFlyweight2("五号老奶奶"));
flyweight2.operation(new UnsharedConcreteFlyweight2("六号广场大妈"));
System.out.println(factory.getSize());
}
}