定义
享元模式让某个类的一个实例能用来提供许多“虚拟实例”。
角色
- 抽象享元类(Flyweight):通常是一个接口或抽象类,向外界提供享元对象的内部数据或外部数据。
- 具体享元类(Concrete Flyweight):具体实现内部数据共享的类。
- 享元工厂类(Flyweight Factory):用于创建和管理享元对象的工厂类。
类图
实现
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
public interface Flyweight {
void operation(String extrinsicState);
String getIntrinsicState(); }
public class ConcreteFlyweight implements Flyweight {
private String intrinsicState;
public ConcreteFlyweight(String intrinsicState) { this.intrinsicState = intrinsicState; }
@Override public void operation(String extrinsicState) { System.out.println(String.format("内部状态:%s;外部状态:%s", intrinsicState, extrinsicState)); }
@Override public String getIntrinsicState() { return intrinsicState; } }
public class FlyweightFactory { public static final Map<String, Flyweight> FLYWEIGHT_MAP = new HashMap<>();
public static Flyweight getFlyweight(String intrinsicState) { if (FLYWEIGHT_MAP.containsKey(intrinsicState)) { return FLYWEIGHT_MAP.get(intrinsicState); } else { ConcreteFlyweight flyweight = new ConcreteFlyweight(intrinsicState); FLYWEIGHT_MAP.put(intrinsicState, flyweight); return flyweight; } } }
|
优缺点
优点
缺点
- 提高了系统的复杂度,需要分理出外部状态和内部状态,而且外部状态具有固化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱
适用场景
模式应用