原型模式:對象的創建模式
原型管理器:原型管理器角色保持一個聚集,作為對所有原型對象的登記,這個角色提供必要的方法,供外界增加新的原型對象和取得已經登記過的原型對象。
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 }View Code
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 properties10 */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 }View Code新聞熱點
疑難解答