享元模式(Flyweight Pattern)是沲技术的重要实现方式,其定义如下:
Use sharing to support large numbers of fine-grained objects efficiently.
使用共享对象可有效地支持大量的细粒度的对象.
享元模式的定义提出了二个要求:细粒度的对象和共享对象。分配太多的对象将有损程序的性能,同时还容易造成内存溢出。避免这种情况,就是使用享元模式中的共享技术。
细粒度的状态分为内部状态(instrinsic)和外部状态(extrinsic):
内部状态:
内部状态是对象可共享出来的信息,存储在享元对象内部并且不会随着环境改变而改变,属于可以共享部分。
外部状态:
外部状态是对象得以依赖的一个标记,是随着环境改变而改变,不可以共享的状态。
结构类
享元模式是一个非常简单的模式,它可以大大减少应用程序创建的对象,降低程序内存的占用,增强程序的性能,但是它同时也提高了系统的复杂性,需要分离出外部和内部的状态,而且外部状态具有固化特性,不应该随着内部状态改变而改变,否则导致系统的逻辑混乱。
简单的说就是一个抽象类,同时定义出对象的外部状态和内部状态的接口或实现。
具体的产品类,该角色中需要注意的是内部状态处理应该是与环境无关的,不应该出现一个操作改变内部状态,同时修改了外部状态,这是绝对不允许的。
不存在外部状态或者安全要求不能够使用共享技术的对象,该对象一般不会出现在享元工厂中。
构造一个池容器,同时提供从池中获得对象的方法。
享元模式的目的在于运用共享技术,使得一些细粒度的对象可以共享,多使用细粒度的对象,便于重用或重构。
Flyweight:
public abstract class Flyweight {
//内部状态
private String instrinsic;
//外部状态
private final String extrinsic;
//要求享元角色必须接受外部状态
public Flyweight(String extrinsic) {
super();
this.extrinsic = extrinsic;
}
public String getInstrinsic() {
return instrinsic;
}
public void setInstrinsic(String instrinsic) {
this.instrinsic = instrinsic;
}
//定义业务逻辑
public abstract void operate();
@Override
public String toString() {
return "Flyweight [instrinsic=" + instrinsic + ", extrinsic="
+ extrinsic + "]";
}
}
ConcreteFlyweight1:
public class ConcreteFlyweight1 extends Flyweight {
public ConcreteFlyweight1(String extrinsic) {
super(extrinsic);
// TODO Auto-generated constructor stub
}
@Override
public void operate() {
// TODO Auto-generated method stub
System.out.println("ConcreteFlyweight1----operate()");
}
}
ConcreteFlyweight2:
public class ConcreteFlyweight2 extends Flyweight {
public ConcreteFlyweight2(String extrinsic) {
super(extrinsic);
// TODO Auto-generated constructor stub
}
@Override
public void operate() {
// TODO Auto-generated method stub
System.out.println("ConcreteFlyweight2----operate()");
}
}
FlyweightFactory:
import java.util.HashMap;
public class FlyweightFactory {
//创建一个池容器
private static HashMap pool = new HashMap();
//享元工厂
public static Flyweight getFlyweight(String Extrinsic){
Flyweight flyweight = null;
if(pool.containsKey(Extrinsic)){
flyweight = pool.get(Extrinsic);
}else {
flyweight = new ConcreteFlyweight1(Extrinsic);
pool.put(Extrinsic, flyweight);
}
return flyweight;
}
//打印池容器内的信息
public static void printPool(){
for (int i = 0; i < pool.size(); i++) {
System.out.println(":"+pool.get("indext:"+i));
}
}
}
Client:
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Flyweight flyweight = null;
for (int i = 0; i < 3; i++) {
flyweight = FlyweightFactory.getFlyweight("indext:"+i);
flyweight.operate();
}
FlyweightFactory.printPool();
System.out.println("------------------------");
for (int i = 0; i < 3; i++) {
flyweight = FlyweightFactory.getFlyweight("indext:"+i);
flyweight.operate();
}
FlyweightFactory.printPool();
}
}
输出结果:
ConcreteFlyweight1----operate()
ConcreteFlyweight1----operate()
ConcreteFlyweight1----operate()
:Flyweight [instrinsic=null, extrinsic=indext:0]
:Flyweight [instrinsic=null, extrinsic=indext:1]
:Flyweight [instrinsic=null, extrinsic=indext:2]
------------------------
ConcreteFlyweight1----operate()
ConcreteFlyweight1----operate()
ConcreteFlyweight1----operate()
:Flyweight [instrinsic=null, extrinsic=indext:0]
:Flyweight [instrinsic=null, extrinsic=indext:1]
:Flyweight [instrinsic=null, extrinsic=indext:2]
从结果中,我们可以看出,池技术的缓冲方法确实是没有增加Flyweight的数量。
(1).设计模式之禅—第28章 享元模式
(2)享元模式
https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/flyweight