廣州白云做網(wǎng)站的公司網(wǎng)絡(luò)銷售怎么聊客戶
什么是策略模式?
策略模式提供生成某一種產(chǎn)品的不同方式
Strategy策略類定義了某個(gè)各種算法的公共方法,不同的算法類通過繼承Strategy策略類,實(shí)現(xiàn)自己的算法
Context的作用是減少客戶端和Strategy策略類之間的耦合,客戶端只需要調(diào)用Context并且傳遞相應(yīng)的算法參數(shù),來(lái)調(diào)用不同的算法,Context的內(nèi)部實(shí)現(xiàn)可以用一個(gè)Map<id, Strategy>,在Spring啟動(dòng)的時(shí)候,會(huì)把Strategy類自動(dòng)裝配進(jìn)Map
Spring的策略模式
Resource getResource(String location) 根據(jù)傳入的 location(算法參數(shù))返回一個(gè)資源子類(具體算法),如 ClassPathResource 內(nèi)部定義了解析 xml 的方法
else if (location.startsWith(CLASSPATH_URL_PREFIX)) {return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
}
else {try {// Try to parse the location as a URL...URL url = ResourceUtils.toURL(location);return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));}catch (MalformedURLException ex) {// No URL -> resolve as resource path.return getResourceByPath(location);}
}
getInstantiationStrategy().instantiate(),如果需要被反射創(chuàng)建的 bean 的方法沒有覆寫,直接用無(wú)參構(gòu)造器創(chuàng)建,否則用CGLIB創(chuàng)建。