原型模式:对象的创建模式
原型管理器:原型管理器角色保持一个聚集,作为对所有原型对象的登记,这个角色提供必要的方法,供外界增加新的原型对象和取得已经登记过的原型对象。
public class PrototypeManager { /** * 用来记录原型的编号和原型实例的对应关系 */ private static Map<String,Prototype> map = new HashMap<String,Prototype>(); /** * 私有化构造方法,避免外部创建实例 */ private PrototypeManager(){} /** * 向原型管理器里面添加或是修改某个原型注册 * @param prototypeId 原型编号 * @param prototype 原型实例 */ public synchronized static void setPrototype(String prototypeId , Prototype prototype){ map.put(prototypeId, prototype); } /** * 从原型管理器里面删除某个原型注册 * @param prototypeId 原型编号 */ public synchronized static void removePrototype(String prototypeId){ map.remove(prototypeId); } /** * 获取某个原型编号对应的原型实例 * @param prototypeId 原型编号 * @return 原型编号对应的原型实例 * @throws Exception 如果原型编号对应的实例不存在,则抛出异常 */ public synchronized static Prototype getPrototype(String prototypeId) throws Exception{ Prototype prototype = map.get(prototypeId); if(prototype == null){ throw new Exception("您希望获取的原型还没有注册或已被销毁"); } return prototype; } }
public class PropertiesUtil { /** 保存所有的properties */ private static ConcurrentHashMap<String, Properties> propertiesMap = new ConcurrentHashMap<String, Properties>(); /** * 取得指定的properties * @param path properties的路径(如:com/webvoice/properties/webvoice.properties) * @return properties */ public synchronized static Properties getProperties(String path) { Properties prop = new Properties(); try ( InputStream propIS = PropertiesUtil.class.getClassLoader().getResourceAsStream(path); ){ prop.load(propIS); propertiesMap.put(path, prop); } catch (Exception e) { AbstractBaseAction.logger.systemError("001", "获取properties失败:path=[" + path + "].", "PropertiesUtil", e); } return propertiesMap.get(path); } }
PropertiesUtil 的完整版
1 public class PropertiesUtil { 2 3 /** 保存所有的properties */ 4 private static ConcurrentHashMap<String, Properties> propertiesMap = new ConcurrentHashMap<String, Properties>(); 5 6 /** 7 * 取得指定的properties 8 * 9 * @param path properties的路径(如:com/webvoice/properties/webvoice.properties) 10 * @return properties 11 */ 12 public synchronized static Properties getProperties(String path) { 13 Properties prop = new Properties(); 14 try ( 15 InputStream propIS = getClassLoader().getResourceAsStream(path); 16 ){ 17 18 prop.load(propIS); 19 propertiesMap.put(path, prop); 20 } catch (Exception e) { 21 AbstractBaseAction.logger.systemError("001", "获取properties失败:path=[" + path + "].", "PropertiesUtil", e); 22 } 23 return propertiesMap.get(path); 24 } 25 26 /** 27 * 取得当前运行程序的ClassLoader 28 * 29 * @return ClassLoader 30 */ 31 public static ClassLoader getClassLoader() { 32 return PropertiesUtil.class.getClassLoader(); 33 } 34 35 /** 36 * 取得properties中key定义的值,转化为数组返回 37 * 38 * @param key 39 * @param prop properties 40 * @return key对应值分割之后的数组 41 */ 42 public static String[] getArrayFromProperties(String key, Properties prop) { 43 String allValue = prop.getProperty(key); 44 String[] values = null; 45 if (allValue != null) { 46 values = allValue.split(","); 47 } 48 return values; 49 } 50 51 /** 52 * 取得properties中key定义的值,转化为数组返回 53 * 54 * @param key 55 * @param path properties的路径(如:com/webvoice/properties/webvoice.properties) 56 * @return key对应值分割之后的数组 57 */ 58 public static String[] getArrayFromProperties(String key, String path) { 59 String[] values = null; 60 Properties prop = getProperties(path); 61 if (prop == null) { 62 return values; 63 } 64 String allValue = prop.getProperty(key); 65 if (allValue != null) { 66 values = allValue.split(","); 67 } 68 return values; 69 } 70 71 /** 72 * 取得properties中key定义的值,转化为List返回 73 * 74 * @param key 75 * @param prop properties 76 * @return key对应值分割之后的List 77 */ 78 public synchronized static List<String> getListFromProperties(String key, Properties prop) { 79 List<String> retList = new ArrayList<String>(); 80 String allValue = prop.getProperty(key); 81 if (allValue != null) { 82 String[] values = allValue.split(","); 83 for (String singleValue : values) { 84 retList.add(singleValue); 85 } 86 } 87 return retList; 88 } 89 90 /** 91 * 取得properties中key定义的值,转化为List返回 92 * 93 * @param key 94 * @param path properties的路径(如:com/webvoice/properties/webvoice.properties) 95 * @return key对应值分割之后的List 96 */ 97 public synchronized static List<String> getListFromProperties(String key, String path) { 98 List<String> retList = new ArrayList<String>(); 99 Properties prop = getProperties(path); 100 if (prop == null) { 101 return retList; 102 } 103 String allValue = prop.getProperty(key); 104 if (allValue != null && !"".equals(allValue)) { 105 String[] values = allValue.split(","); 106 for (String singleValue : values) { 107 retList.add(singleValue); 108 } 109 } 110 return retList; 111 } 112 113 }
1 public class PropertiesUtil { 2 3 /** 保存所有的properties */ 4 private static ConcurrentHashMap<String, Properties> propertiesMap = new ConcurrentHashMap<String, Properties>(); 5 6 /** 7 * 取得指定的properties 8 * @param path properties的路径(如:com/webvoice/properties/webvoice.properties) 9 * @return properties 10 */ 11 public synchronized static Properties getProperties(String path) { 12 Properties prop = new Properties(); 13 try ( 14 InputStream propIS = PropertiesUtil.class.getClassLoader().getResourceAsStream(path); 15 ){ 16 17 prop.load(propIS); 18 propertiesMap.put(path, prop); 19 } catch (Exception e) { 20 AbstractBaseAction.logger.systemError("001", "获取properties失败:path=[" + path + "].", "PropertiesUtil", e); 21 } 22 return propertiesMap.get(path); 23 } 24 }