本文主要是介绍设计模式之享元模式--- Pattern Flyweight,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
模式的定义
享元模式(Flyweight Pattern)是沲技术的重要实现方式,其定义如下:
Use sharing to support large numbers of fine-grained objects efficiently.
使用共享对象可有效地支持大量的细粒度的对象.
享元模式的定义提出了二个要求:细粒度的对象和共享对象。分配太多的对象将有损程序的性能,同时还容易造成内存溢出。避免这种情况,就是使用享元模式中的共享技术。
细粒度的状态分为内部状态(instrinsic)和外部状态(extrinsic):
内部状态:
内部状态是对象可共享出来的信息,存储在享元对象内部并且不会随着环境改变而改变,属于可以共享部分。
外部状态:
外部状态是对象得以依赖的一个标记,是随着环境改变而改变,不可以共享的状态。
类型
结构类
模式的使用场景
- 系统中存在大量的相似对象
- 细粒度的对象都具备较接近的外部状态,而且内部状态与环境无关,也就是说对象没有特定的身份。
- 需要缓冲池的场景
优缺点
享元模式是一个非常简单的模式,它可以大大减少应用程序创建的对象,降低程序内存的占用,增强程序的性能,但是它同时也提高了系统的复杂性,需要分离出外部和内部的状态,而且外部状态具有固化特性,不应该随着内部状态改变而改变,否则导致系统的逻辑混乱。
UML类图
角色介绍
- Flyweight—抽象享元角色
简单的说就是一个抽象类,同时定义出对象的外部状态和内部状态的接口或实现。
- ConcreteFlyweight—具体享元角色
具体的产品类,该角色中需要注意的是内部状态处理应该是与环境无关的,不应该出现一个操作改变内部状态,同时修改了外部状态,这是绝对不允许的。
- unsharedConcreteFlyweight—不可共享的享元角色
不存在外部状态或者安全要求不能够使用共享技术的对象,该对象一般不会出现在享元工厂中。
- FlyweightFactory—享元工厂
构造一个池容器,同时提供从池中获得对象的方法。
享元模式的目的在于运用共享技术,使得一些细粒度的对象可以共享,多使用细粒度的对象,便于重用或重构。
模式的通用源码
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();@Overridepublic 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}@Overridepublic void operate() {// TODO Auto-generated method stubSystem.out.println("ConcreteFlyweight1----operate()");}}
ConcreteFlyweight2:
public class ConcreteFlyweight2 extends Flyweight {public ConcreteFlyweight2(String extrinsic) {super(extrinsic);// TODO Auto-generated constructor stub}@Overridepublic void operate() {// TODO Auto-generated method stubSystem.out.println("ConcreteFlyweight2----operate()");}}
FlyweightFactory:
import java.util.HashMap;public class FlyweightFactory {//创建一个池容器private static HashMap<String, Flyweight> pool = new HashMap<String, Flyweight>();//享元工厂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 stubFlyweight 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的数量。
Android源码中的模式实现
杂谈
参考资料
(1).设计模式之禅—第28章 享元模式
(2)享元模式
https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/flyweight
这篇关于设计模式之享元模式--- Pattern Flyweight的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!