`
samsongbest
  • 浏览: 162385 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

 
阅读更多

1.含义

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

 

2.类图

单纯享元


复合享元


 

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

  • 大小: 90.4 KB
  • 大小: 61.3 KB
分享到:
评论

相关推荐

    c++设计模式-结构型模式-享元模式

    c++设计模式-结构型模式-享元模式;qt工程;c++简单源码; 享元(Flyweight)模式的定义:运用共享技术来有效地支持大量细粒度对象的复用。它通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似类...

    结构型模式之共享元模式(Flyweight)

    6共享元模式(Flyweight) 用意:以共享的方式高效地支持大量的细粒度对象

    C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式) (Level 300)

    C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式) (Level 300)

    C#面向对象设计模式纵横谈\12 结构型模式Flyweight享元模式.zip

    在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要... 这是第12节:结构型模式Flyweight享元模式

    享元模式 Flyweight Pattern

    享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式。

    C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式)

    C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式)

    FlyWeight享元

    FlyWeight享元 --- 对象结构型模式

    C++设计模式编程之Flyweight享元模式结构详解

    主要介绍了C++设计模式编程的Flyweight享元模式结构,享元模式在实现过程中主要是要为共享对象提供一个存放的"仓库"(对象池),需要的朋友可以参考下

    设计模式--C++

    4.6 Flyweight(享元)—对象结构型模式 128 4.7 Proxy(代理)—对象结构型模式 137 4.8 结构型模式的讨论 144 4.8.1 Adapter 与 Bridge 144 4.8.2 Composite、 Decorator 与 Proxy 145 第 5 章 行为模式 147 5.1 ...

    学习php设计模式 php实现享元模式(flyweight)

    二、享元模式结构图 三、享元模式中主要角色 抽象享元(Flyweight)角色:此角色是所有的具体享元类的超类,为这些类规定出需要实现的公共接口。那些需要外蕴状态的操作可以通过调用商业以参数形式传入 具体享元...

    C++ Flyweight模式

    23种设计模式之十(结构型模式)Flyweight模式

    设计模式--可复用面向对象软件的基础

    4.6 FLYWEIGHT(享元)——对象结构型模式 4.7 PROXY(代理)——对象结构型模式 4.8 结构型模式的讨论 第五章 行为模式 5.1 CHAIN OF RESPONSIBIL ITY(职责链)——对象行为型模式 5.2 COMMAND(命令)——对象...

    详解Java设计模式编程中的Flyweight享元模式的开发结构

    主要介绍了Java设计模式编程中的Flyweight享元模式的开发结构,享元模式能够最大限度地重用现有的同类对象,需要的朋友可以参考下

    设计模式精解-GoF 23种设计模式解析

    2.5 Flyweight模式 2.6 Facade模式 2.7 Proxy模式 3 行为模式.....55 3.1 Template模式 3.2 Strategy模式 3.3 State模式 3.4 Observer模式 3.5 Memento模式 3.6 Mediator模式 3.7 Command模式 3.8 Visitor模式 ...

    09-通过容器实现的外观模式(2).html

    享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( Template Method ) 观察者模式( Observer ) 迭代子模式( Iterator ) 责任链模式( Chain of Responsibility ) 命令模式...

    design-pattern-java.pdf

    扩展系统功能——装饰模式(三) 扩展系统功能——装饰模式(四) 外观模式-Facade Pattern 深入浅出外观模式(一) 深入浅出外观模式(二) 深入浅出外观模式(三) 享元模式-Flyweight Pattern 实现对象的复用——...

    JAVA设计模式之结构模式

    这是JAVA设计模式中属于结构模式的部分,包括Flyweight(共享模式)、Bridge(桥模式)、Decorator(装饰模式)、Composite(组合模式)、Adapter(适配器模式)、Proxy(代理模式)、Facade (外观模式)的源代码。其中有些模式中...

    研磨设计模式-part2

    第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式(Chain of Responsibility) 第24章 桥接模式(Bridge) 第25章 访问者模式(Visitor) ...

    研磨设计模式-part4

    第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式(Chain of Responsibility) 第24章 桥接模式(Bridge) 第25章 访问者模式(Visitor) ...

    研磨设计模式-part3

    第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式(Chain of Responsibility) 第24章 桥接模式(Bridge) 第25章 访问者模式(Visitor) ...

Global site tag (gtag.js) - Google Analytics