FlyweightFactory.java
上传用户:hensond
上传日期:2021-12-27
资源大小:817k
文件大小:1k
源码类别:

软件工程

开发平台:

Java

  1. package com.company.section3;
  2. import java.util.HashMap;
  3. /**
  4.  * @author cbf4Life cbf4life@126.com
  5.  * I'm glad to share my knowledge with you all.
  6.  */
  7. public class FlyweightFactory {
  8. //定义一个池容器
  9. private static  HashMap<String,Flyweight> pool= new HashMap<String,Flyweight>();
  10. //享元工厂
  11. public static Flyweight getFlyweight(String Extrinsic){
  12. //需要返回的对象
  13. Flyweight flyweight = null;
  14. //在池中没有改对象
  15. if(pool.containsKey(Extrinsic)){
  16. flyweight = pool.get(Extrinsic);
  17. }else{
  18. //根据外部状态创建享元对象
  19. flyweight = new ConcreteFlyweight1(Extrinsic);
  20. //放置到池中
  21. pool.put(Extrinsic, flyweight);
  22. }
  23. return flyweight;
  24. }
  25. }