第0章:简介
享元模式的定义:为了减少不会要额外的内存消耗,将多个对同一对象的访问集中起来,不必为每个访问者创建一个单独的对象,以此来降低内存的消耗。
享元模式的本质:共享实例来避免new出新实例
参考:研磨设计模式(书籍),大话设计模式(书籍),图解设计模式(书籍)
模式图:
待补充
第1章:实践
第0节:简单的实现
(1)抽象享元接口(Flyweight.java)
package com.mcc.core.designPattern.structure.flyweight.simpleFlyweight; /** * 抽象享元接口 * 规定出所有具体享元角色需要实现的方法 * * 享元模式的定义:为了减少不会要额外的内存消耗,将多个对同一对象的访问集中起来,不必为每个访问者创建一个单独的对象,以此来降低内存的消耗。 * 享元模式的本质:共享实例来避免new出新实例 * * @author <a href="mailto:417877417@qq.com">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public interface Flyweight { /** * 普通方法 */ public void method1(); /** * 一个示意性方法,参数externalState是外蕴状态 * @param externalState */ public void method2(String externalState); } |
(2)具体享元对象(ConcreteFlyweight.java)
package com.mcc.core.designPattern.structure.flyweight.simpleFlyweight; /** * 具体享元对象,表示的就是那些实例会被共享的类 * 实现抽象享元所规定出的接口。如果有内蕴状态的话,必须负责为内蕴状态提供存储空间 * * 享元模式采用一个共享来避免大量拥有相同内容对象的开销。这种开销最常见、最直观的就是内存的损耗。 * 享元对象能做到共享的关键是区分内蕴状态(Internal State)和外蕴状态(External State): * (1)一个内蕴状态是存储在享元对象内部的,并且是不会随环境的改变而有所不同。 * 因此,一个享元可以具有内蕴状态并可以共享。 * (2)一个外蕴状态是随环境的改变而改变的、不可以共享的。享元对象的外蕴状态必须由客户端保存, * 并在享元对象被创建之后,在需要使用的时候再传入到享元对象内部。 * 外蕴状态不可以影响享元对象的内蕴状态,它们是相互独立的。 * * @author <a href="mailto:417877417@qq.com">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class ConcreteFlyweight implements Flyweight { //内蕴状态 private String intrinsicState = null; /** * 构造函数,内蕴状态作为参数传入 * @param intrinsicState */ public ConcreteFlyweight(String intrinsicState) { this.intrinsicState = intrinsicState; } /** * 普通方法 */ @Override public void method1() { System.out.println("具体享元method1方法"); } /** * 一个示意性方法,参数externalState是外蕴状态 * @param externalState */ @Override public void method2(String externalState) { System.out.println("内蕴状态:" + this.intrinsicState); System.out.println("外蕴状态:" + externalState); } } |
(3)享元工厂(FlyweightFactory.java)
package com.mcc.core.designPattern.structure.flyweight.simpleFlyweight; import java.util.HashMap; import java.util.Map; /** * 享元工厂 * 用于生产具体享元对象ConcreteFlyweight,可以实现共享 * 必须指出的是,客户端不可以直接将具体享元类实例化,而必须通过这个工厂对象 * * @author <a href="mailto:417877417@qq.com">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class FlyweightFactory { //管理已经生成的享元对象,用于共享 private Map<String,Flyweight> flyweightPool = new HashMap<String,Flyweight>(); /** * 采用饿汉式单例模式,线程安全,类装载的时候就会被加载 * 一般而言,享元工厂对象在整个系统中只有一个,因此也可以使用单例模式。 */ private static FlyweightFactory singleton = new FlyweightFactory(); private FlyweightFactory(){ } public static FlyweightFactory getSingleton(){ return singleton; } /** * 获取具体对象实例方法,线程安全 * @param intrinsicState * @return */ public synchronized Flyweight getFlyweight(String intrinsicState){ //先从缓存中查找对象 Flyweight flyweight = flyweightPool.get(intrinsicState); if(flyweight == null){ //如果对象不存在则创建一个新的Flyweight对象 flyweight = new ConcreteFlyweight(intrinsicState); //把这个新的Flyweight对象添加到缓存中 flyweightPool.put(intrinsicState,flyweight); } return flyweight; } } |
(4)客户端测试(Client.java)
package com.mcc.core.designPattern.structure.flyweight.simpleFlyweight; /** * 客户端测试 * * @author <a href="mailto:417877417@qq.com">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class Client { public static void main(String args[]){ FlyweightFactory flyweightFactory = FlyweightFactory.getSingleton(); Flyweight flyweight1 = flyweightFactory.getFlyweight("flyweight1"); flyweight1.method2("外部状态1"); Flyweight flyweight2 = flyweightFactory.getFlyweight("flyweight2"); flyweight2.method2("外部状态2"); Flyweight flyweight3 = flyweightFactory.getFlyweight("flyweight1"); flyweight3.method2("外部状态3"); } } |
第1节:复杂的实现
(1)抽象享元接口(Flyweight.java)
package com.mcc.core.designPattern.structure.flyweight.complexFlyweight; /** * 抽象享元接口 * 规定出所有具体享元角色需要实现的方法 * * @author <a href="mailto:417877417@qq.com">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public interface Flyweight { /** * 普通方法 */ public void method1(); /** * 一个示意性方法,参数externalState是外蕴状态 * @param externalState */ public void method2(String externalState); } |
(2)具体享元对象(ConcreteFlyweight.java)
package com.mcc.core.designPattern.structure.flyweight.complexFlyweight; /** * 具体享元对象,表示的就是那些实例会被共享的类 * 实现抽象享元所规定出的接口。如果有内蕴状态的话,必须负责为内蕴状态提供存储空间 * * 享元模式采用一个共享来避免大量拥有相同内容对象的开销。这种开销最常见、最直观的就是内存的损耗。 * 享元对象能做到共享的关键是区分内蕴状态(Internal State)和外蕴状态(External State): * (1)一个内蕴状态是存储在享元对象内部的,并且是不会随环境的改变而有所不同。 * 因此,一个享元可以具有内蕴状态并可以共享。 * (2)一个外蕴状态是随环境的改变而改变的、不可以共享的。享元对象的外蕴状态必须由客户端保存, * 并在享元对象被创建之后,在需要使用的时候再传入到享元对象内部。 * 外蕴状态不可以影响享元对象的内蕴状态,它们是相互独立的。 * * @author <a href="mailto:417877417@qq.com">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class ConcreteFlyweight implements Flyweight { //内蕴状态 private String intrinsicState = null; /** * 构造函数,内蕴状态作为参数传入 * @param intrinsicState */ public ConcreteFlyweight(String intrinsicState) { this.intrinsicState = intrinsicState; } /** * 普通方法 */ @Override public void method1() { System.out.println("具体享元method1方法"); } /** * 一个示意性方法,参数externalState是外蕴状态 * @param externalState */ @Override public void method2(String externalState) { System.out.println("内蕴状态:" + this.intrinsicState); System.out.println("外蕴状态:" + externalState); } } |
(3)复合享元(ConcreteCompositeFlyweight.java)
package com.mcc.core.designPattern.structure.flyweight.complexFlyweight; import java.util.HashMap; import java.util.Map; /** * 复合享元 * 所代表的对象是不可以共享的,但是一个复合享元对象可以分解成为多个本身是单纯享元对象的组合。 * * @author <a href="mailto:417877417@qq.com">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class ConcreteCompositeFlyweight implements Flyweight { private Map<String,Flyweight> flyweights = new HashMap<String,Flyweight>(); /** * 增加一个新的单纯享元对象到集合中 * @param intrinsicState * @param flyweight */ public void add(String intrinsicState , Flyweight flyweight){ flyweights.put(intrinsicState,flyweight); } /** * 普遍方法 */ @Override public void method1() { System.out.println("复合享元method1方法"); } /** * 一个示意性方法,参数externalState是外蕴状态 * @param externalState */ @Override public void method2(String externalState) { Flyweight flyweight = null; for(Object o : flyweights.keySet()){ flyweight = flyweights.get(o); flyweight.method2(externalState); } } } |
(4)享元工厂(FlyweightFactory.java)
package com.mcc.core.designPattern.structure.flyweight.complexFlyweight; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 享元工厂 * 用于生产具体享元对象ConcreteFlyweight,可以实现共享 * 必须指出的是,客户端不可以直接将具体享元类实例化,而必须通过这个工厂对象 * * @author <a href="mailto:417877417@qq.com">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class FlyweightFactory { //管理已经生成的享元对象,用于共享 private Map<String, Flyweight> flyweightPool = new HashMap<String, Flyweight>(); /** * 采用饿汉式单例模式,线程安全,类装载的时候就会被加载 * 一般而言,享元工厂对象在整个系统中只有一个,因此也可以使用单例模式。 */ private static FlyweightFactory singleton = new FlyweightFactory(); private FlyweightFactory(){ } public static FlyweightFactory getSingleton(){ return singleton; } /** * 简单享元工厂方法 * 获取具体对象实例方法,线程安全 * @param intrinsicState * @return */ public synchronized Flyweight getFlyweight(String intrinsicState){ //先从缓存中查找对象 Flyweight flyweight = flyweightPool.get(intrinsicState); if(flyweight == null){ //如果对象不存在则创建一个新的Flyweight对象 flyweight = new ConcreteFlyweight(intrinsicState); //把这个新的Flyweight对象添加到缓存中 flyweightPool.put(intrinsicState,flyweight); } return flyweight; } /** * 复合享元工厂方法 * @param intrinsicStates * @return */ public Flyweight getFlyweight(List<String> intrinsicStates){ ConcreteCompositeFlyweight concreteCompositeFlyweight = new ConcreteCompositeFlyweight(); for(String intrinsicState : intrinsicStates){ concreteCompositeFlyweight.add(intrinsicState,this.getFlyweight(intrinsicState)); } return concreteCompositeFlyweight; } } |
(5)客户端测试(Client.java)
package com.mcc.core.designPattern.structure.flyweight.complexFlyweight; import java.util.ArrayList; import java.util.List; /** * 客户端测试 * * @author <a href="mailto:417877417@qq.com">menergy</a> * My blog: https://blog.csdn.net/menergy * Created by Administrator on 2018/4/26. */ public class Client { public static void main(String args[]){ List<String> intrinsicStates = new ArrayList<String>(); intrinsicStates.add("Flyweight1"); intrinsicStates.add("Flyweight2"); intrinsicStates.add("Flyweight3"); intrinsicStates.add("Flyweight1"); intrinsicStates.add("Flyweight2"); FlyweightFactory flyFactory = FlyweightFactory.getSingleton(); Flyweight concreteCompositeFlyweight1 = flyFactory.getFlyweight(intrinsicStates); Flyweight concreteCompositeFlyweight2 = flyFactory.getFlyweight(intrinsicStates); concreteCompositeFlyweight1.method2("外部状态1"); System.out.println("---------------------------------"); System.out.println("复合享元模式是否可以共享对象:" + (concreteCompositeFlyweight1 == concreteCompositeFlyweight2)); String intrinsicState = "Flyweight4"; Flyweight concreteFlyweight1 = flyFactory.getFlyweight("Flyweight4"); Flyweight concreteFlyweight2 = flyFactory.getFlyweight("Flyweight4"); System.out.println("单纯享元模式是否可以共享对象:" + (concreteFlyweight1 == concreteFlyweight2)); } } |