09 结构模式 - 享元(Flyweight)模式

1.含义

享元模式以共享的方式高效地支持大量的细粒度对象。

 

2.类图

单纯享元


09 结构模式 - 享元(Flyweight)模式

复合享元


09 结构模式 - 享元(Flyweight)模式

 

3.代码

单纯享元

/*
 * 抽象享元角色
 */
abstract public class Flyweight {
	abstract public void operation(String state);
}


/*
 * 具体享元角色
 */
public class ConcreteFlyweight extends Flyweight {
	private Character intrinsicState = null;

	public ConcreteFlyweight(Character state) {
		this.intrinsicState = state;
	}

	public void operation(String state) {
		System.out.println("\nIntrinsic State =" + intrinsicState
				+ ",Extrinsic State = " + state);
	}
}


/*
 * 享元工厂角色
 */
public class FlyweightFactory {
	private HashMap files = new HashMap();
	private Flyweight lnkFlyweight;

	public FlyweightFactory() {
	}

	public Flyweight factory(Character state) {
		if (files.containsKey(state)) {
			return (Flyweight) files.get(state);
		} else {
			Flyweight fly = new ConcreteFlyweight(state);
			files.put(state, fly);
			return fly;
		}
	}

	public void checkFlyweight() {
		Flyweight fly;
		int i = 0;
		System.out.println("\n======checkFlyweight()======");
		for (Iterator it = files.entrySet().iterator(); it.hasNext();) {
			Map.Entry e = (Map.Entry) it.next();
			System.out.println("Item " + (++i) + ":" + e.getKey());
		}
		System.out.println("======checkFlyweight()======");
	}
}


/*
 * 测试类
 */
public class Test {

	public static void main(String[] args) {
		FlyweightFactory f = new FlyweightFactory();
		Flyweight fly = f.factory(new Character('a'));
		fly.operation("First Call");
		fly = f.factory(new Character('b'));
		fly.operation("Second Call");
		fly = f.factory(new Character('a'));
		fly.operation("Third Call");
		f.checkFlyweight();
	}
}
 

复合享元

/*
 * 抽象享元角色
 */
abstract public class Flyweight {
	abstract public void operation(String state);
}


/*
 * 具体享元角色
 */
public class ConcreteFlyweight extends Flyweight {
	private Character intrinsicState = null;

	public ConcreteFlyweight(Character state) {
		this.intrinsicState = state;
	}

	public void operation(String state) {
		System.out.println("\nIntrinsic State =" + intrinsicState
				+ ",Extrinsic State = " + state);
	}
}


/*
 * 具体复合享元角色
 */
public class ConcreteCompositeFlyweight extends Flyweight {
	private HashMap files = new HashMap(10);
	private Flyweight flyweight;

	public ConcreteCompositeFlyweight() {
	}

	public void add(Character key, Flyweight fly) {
		files.put(key, fly);
	}

	public void operation(String extrinsicState) {
		Flyweight fly = null;
		for (Iterator it = files.entrySet().iterator(); it.hasNext();) {
			Map.Entry e = (Map.Entry) it.next();
			fly = (Flyweight) e.getValue();
			fly.operation(extrinsicState);
		}
	}
}


/*
 * 享元工厂角色
 */
public class FlyweightFactory {
	private HashMap files = new HashMap();
	private Flyweight lnkFlyweight;

	public FlyweightFactory() {
	}

	/**
	 * 复合享元工厂方法,所需状态以参量形式传入
	 * @param compositeState
	 * @return
	 */
	public Flyweight factory(String compositeState) {
		ConcreteCompositeFlyweight ccfw = new ConcreteCompositeFlyweight();
		int len = compositeState.length();
		Character state = null;
		for (int i = 0; i < len; i++) {
			state = new Character(compositeState.charAt(i));
			System.out.println("factory(" + state + ")");
			ccfw.add(state, this.factory(state));
		}
		return ccfw;
	}

	/**
	 * 单纯享元工厂方法,所需状态以参量形式传入
	 * 
	 * @param state
	 * @return
	 */
	public Flyweight factory(Character state) {
		if (files.containsKey(state)) {
			return (Flyweight) files.get(state);
		} else {
			Flyweight fly = new ConcreteFlyweight(state);
			files.put(state, fly);
			return fly;
		}
	}

	public void checkFlyweight() {
		Flyweight fly;
		int i = 0;
		System.out.println("\n======checkFlyweight()======");
		for (Iterator it = files.entrySet().iterator(); it.hasNext();) {
			Map.Entry e = (Map.Entry) it.next();
			System.out.println("Item " + (++i) + ":" + e.getKey());
		}
		System.out.println("======checkFlyweight()======");
	}
}


/*
 * 测试类
 */
public class Test {

	public static void main(String[] args) {
		FlyweightFactory f = new FlyweightFactory();
		Flyweight fly = f.factory("aba");
		fly.operation("Composite Call");
	}
}
 

z

你可能感兴趣的:(flyweight)