1、概念
运用共享技术来有效地支持大量细粒度对象的复用。它通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似类的开销,从而提高系统资源的利用率。
享元模式把对象的信息分为两个部分:内部状态和外部状态。
- 内部状态指对象共享出来的信息,存储在享元信息内部,并且不回随环境的改变而改变;
- 外部状态指对象得以依赖的一个标记,随环境的改变而改变,不可共享。
优点: - 相同对象只要保存一份,这降低了系统中对象的数量,从而降低了系统中细粒度对象给内存带来的压力。
缺点: - 为了使对象可以共享,需要将一些不能共享的状态外部化,这将增加程序的复杂性。
- 读取享元模式的外部状态会使得运行时间稍微变长。
2、实现
享元模式的主要角色:
- 抽象享元角色(Flyweight):是所有的具体享元类的基类,为具体享元规范需要实现的公共接口,非享元的外部状态以参数的形式通过方法传入。
- 具体享元(Concrete Flyweight)角色:实现抽象享元角色中所规定的接口。
- 非享元(Unsharable Flyweight)角色:是不可以共享的外部状态,它以参数的形式注入具体享元的相关方法中。
- 享元工厂(Flyweight Factory)角色:负责创建和管理享元角色。当客户对象请求一个享元对象时,享元工厂检査系统中是否存在符合要求的享元对象,如果存在则提供给客户;如果不存在的话,则创建一个新的享元对象。
package com.mine.design.flyweight;
public abstract class Flyweight {
public abstract void operation(UnsharedConcreteFlyweight unshared);
}
package com.mine.design.flyweight;
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());
}
}
package com.mine.design.flyweight;
public class UnsharedConcreteFlyweight {
private String name;
public UnsharedConcreteFlyweight(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.mine.design.flyweight;
import java.util.HashMap;
import java.util.Map;
public class FlyweightFactory {
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();
}
}
package com.mine.design.flyweight;
public class Client {
public static void main(String[] args) {
FlyweightFactory factory = new FlyweightFactory();
Flyweight flyweight1 = factory.getFlyweight("射雕");
flyweight1.operation(new UnsharedConcreteFlyweight("黄老邪"));
flyweight1.operation(new UnsharedConcreteFlyweight("欧阳锋"));
flyweight1.operation(new UnsharedConcreteFlyweight("一灯"));
flyweight1.operation(new UnsharedConcreteFlyweight("洪七"));
Flyweight flyweight2 = factory.getFlyweight("神雕");
flyweight2.operation(new UnsharedConcreteFlyweight("郭靖"));
flyweight2.operation(new UnsharedConcreteFlyweight("杨过"));
flyweight2.operation(new UnsharedConcreteFlyweight("小龙女"));
flyweight2.operation(new UnsharedConcreteFlyweight("郭芙"));
flyweight2.operation(new UnsharedConcreteFlyweight("郭襄"));
flyweight2.operation(new UnsharedConcreteFlyweight(""));
Flyweight flyweight3 = factory.getFlyweight("射雕");
flyweight3.operation(new UnsharedConcreteFlyweight("老顽童"));
flyweight3.operation(new UnsharedConcreteFlyweight("郭靖"));
Flyweight flyweight4 = factory.getFlyweight("倚天屠龙记");
flyweight4.operation(new UnsharedConcreteFlyweight("张三丰"));
flyweight4.operation(new UnsharedConcreteFlyweight("张翠山"));
flyweight4.operation(new UnsharedConcreteFlyweight("张无忌"));
flyweight4.operation(new UnsharedConcreteFlyweight("赵敏"));
Flyweight flyweight5 = factory.getFlyweight("神雕");
flyweight5.operation(new UnsharedConcreteFlyweight("老顽童"));
flyweight5.operation(new UnsharedConcreteFlyweight("老乞丐"));
System.out.println(factory.getSize());
}
}