- 浏览: 162385 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (193)
- Axis2 (10)
- Andriod (2)
- Java (22)
- Eclipse (2)
- 程序人生 (3)
- Windows (1)
- Sql Server 2005/2008 (7)
- 健身 (2)
- Log4j (1)
- Ant (1)
- Fatjar (2)
- 国际化 (1)
- Linux (3)
- JDBC (1)
- Oracle (2)
- 各种报错 (4)
- SWT (5)
- Tomcat (2)
- 车辆管理 (1)
- SVN (2)
- Spring (5)
- 域名服务器 (0)
- HaoWaYa (1)
- FTP (1)
- 集散中心 (1)
- 专业知识 (1)
- 面试准备 (19)
- 设计模式 (22)
- Junit (1)
- 软件下载 (3)
- 深入理解Java虚拟机 (3)
- 数据结构 (4)
- 雅思 托福 (0)
- UML (1)
- Maven (1)
- CV (1)
- ServiceMix (1)
- 电子书 (5)
- Struts1/2 (4)
- DOM W3C DHTML (3)
- Jawr (1)
- LoadRunner (1)
- Java反编译 (0)
- 英语学习 (0)
- 技术书籍 (1)
- Cygwin (0)
- ibatis (1)
- 数据库 (1)
- jQuery (0)
- s (2)
- 源代码项目 (5)
- JSRs (0)
- JCP (0)
- XML (2)
- Dojo (3)
- Effective Java (1)
- 一站到底 (3)
- JavaScript (6)
- DB2 (1)
- 刷机 (1)
- 字符 (1)
- Dynamic Web Project (1)
- 股市日记 (1)
- 代码片段 (0)
- CSS (1)
- PDF (0)
- 英语口语 (1)
- 乒乓球 (1)
- 体检 (0)
- 送花 (0)
- 面试准备-再战江湖 (5)
- ddq (0)
- sss (0)
- ssssss (0)
- 2020面试 (0)
最新评论
-
samsongbest:
Copperfield 写道你的目标很远大,佩服~惭愧,都忘了 ...
人生目标 -
Copperfield:
你的目标很远大,佩服~
人生目标
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
发表评论
-
22 行为模式 - 调停者(Mediator)模式
2012-08-23 10:21 7971.用意 调停者模式包装了一系列对象相互 ... -
21 行为模式 - 解释器(Interpreter)模式
2012-08-23 10:18 7201.用意 给定一个语言之后,解释器模式可以定 ... -
20 行为模式 - 访问者(Visitor)模式
2012-08-22 10:28 8251.用意 封装一些施加于某种数据结构元素之上的操 ... -
19 行为模式 - 状态(State)模式
2012-08-22 10:25 7121.用意 状态模式允许一个对象在其内部状态改变的时候 ... -
18 行为模式 - 备忘录(Memento)模式
2012-08-22 10:12 798需要跟进多重检查点 ... -
17 行为模式 - 命令(Command)模式
2012-08-20 10:29 5311.用意 命令模式把一个请求或者操作封装到一个对象中。命令模 ... -
16 行为模式 - 责任链(Chain of Responsibility)模式
2012-08-20 10:24 7221.用意 在责任链模式里,很多对象由每一个对象对其下家的引用 ... -
15 行为模式 - 迭代子(Iterator)模式
2012-08-17 10:32 7081.用意 迭代子模式又叫游标模式,可以顺序地访问一个聚集中的 ... -
14 行为模式 - 观察者(Observer)模式
2012-08-09 10:27 7721.用意 观察者模式定义了一种一对多的依赖关系,让多个观察者 ... -
13 行为模式 - 模板方法(Template Method)模式
2012-08-09 10:21 6521.用意 准备一个抽象类,将部分逻辑以具体方法以及具体构造子 ... -
12 行为模式 - 策略(Strategy)模式
2012-08-07 10:00 6891.用意 针对一组算法,将每一个算法封装到具有共同接口的 ... -
11 结构模式 - 桥梁(Bridge)模式
2012-08-01 10:01 7011.用意 2.类图 3.代码 z 1.用意 将 ... -
10 结构模式 - 门面(Facade)模式
2012-07-31 13:52 6341.含义 外部与一个子系统的通信必须通过一个统一的门面对象进 ... -
8 代理(Proxy)模式
2012-07-31 09:48 8581.含义 代理模式给某一个对象提供一个代理对象,并由代理对象 ... -
7 装饰(Decorator)模式
2012-07-27 14:03 6721.含义 装饰模式又名包装(Wrapper)模式,以对客户端 ... -
6 合成(Composite)模式
2012-07-27 14:02 7331.含义 合成模式把部分和整体的关系用树结构表示出来。合成模 ... -
5 适配器(Adapter)模式
2012-07-27 13:52 7021. 含义 把一个类的接口变换成客户端所期待的另一种接口,从 ... -
4 原始模型(Prototype)模式
2012-07-27 10:49 7631. 含义 通过给出一个原型对象来指明所要创建的对象的类型, ... -
3 建造(Builder)模式
2012-07-27 09:44 6821.含义:建造模式可以将一个产品的内部表象与产品的生成过程分割 ... -
1 抽象工厂(Abstract Factory)模式
2012-07-26 17:54 7061.类图 2.类 Creator ...
相关推荐
c++设计模式-结构型模式-享元模式;qt工程;c++简单源码; 享元(Flyweight)模式的定义:运用共享技术来有效地支持大量细粒度对象的复用。它通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似类...
6共享元模式(Flyweight) 用意:以共享的方式高效地支持大量的细粒度对象
C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式) (Level 300)
在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要... 这是第12节:结构型模式Flyweight享元模式
享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式。
C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式)
FlyWeight享元 --- 对象结构型模式
主要介绍了C++设计模式编程的Flyweight享元模式结构,享元模式在实现过程中主要是要为共享对象提供一个存放的"仓库"(对象池),需要的朋友可以参考下
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 ...
二、享元模式结构图 三、享元模式中主要角色 抽象享元(Flyweight)角色:此角色是所有的具体享元类的超类,为这些类规定出需要实现的公共接口。那些需要外蕴状态的操作可以通过调用商业以参数形式传入 具体享元...
23种设计模式之十(结构型模式)Flyweight模式
4.6 FLYWEIGHT(享元)——对象结构型模式 4.7 PROXY(代理)——对象结构型模式 4.8 结构型模式的讨论 第五章 行为模式 5.1 CHAIN OF RESPONSIBIL ITY(职责链)——对象行为型模式 5.2 COMMAND(命令)——对象...
主要介绍了Java设计模式编程中的Flyweight享元模式的开发结构,享元模式能够最大限度地重用现有的同类对象,需要的朋友可以参考下
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模式 ...
享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( Template Method ) 观察者模式( Observer ) 迭代子模式( Iterator ) 责任链模式( Chain of Responsibility ) 命令模式...
扩展系统功能——装饰模式(三) 扩展系统功能——装饰模式(四) 外观模式-Facade Pattern 深入浅出外观模式(一) 深入浅出外观模式(二) 深入浅出外观模式(三) 享元模式-Flyweight Pattern 实现对象的复用——...
这是JAVA设计模式中属于结构模式的部分,包括Flyweight(共享模式)、Bridge(桥模式)、Decorator(装饰模式)、Composite(组合模式)、Adapter(适配器模式)、Proxy(代理模式)、Facade (外观模式)的源代码。其中有些模式中...
第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式(Chain of Responsibility) 第24章 桥接模式(Bridge) 第25章 访问者模式(Visitor) ...
第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式(Chain of Responsibility) 第24章 桥接模式(Bridge) 第25章 访问者模式(Visitor) ...
第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式(Chain of Responsibility) 第24章 桥接模式(Bridge) 第25章 访问者模式(Visitor) ...