日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

使用Spring工厂模式管理多个类实现同一个接口

發(fā)布時(shí)間:2024/4/14 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Spring工厂模式管理多个类实现同一个接口 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近小白在看 Spring IOC 和 AOP 源碼時(shí)發(fā)現(xiàn) Spring 中有很多類(lèi)都實(shí)現(xiàn)了同一個(gè)接口,像下面這種

public interface AopProxy {Object getProxy();Object getProxy(@Nullable ClassLoader classLoader); } 復(fù)制代碼final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {private static final long serialVersionUID = 5531744639992436476L;// 略... } 復(fù)制代碼@SuppressWarnings("serial") class CglibAopProxy implements AopProxy, Serializable {// 略... } 復(fù)制代碼

此種方式是如何實(shí)現(xiàn)的小白還不清楚,但肯定知道是 Spring 框架搞的鬼;所以小白就在網(wǎng)上找到了一圈、自己能看懂的一段代碼,然后照著網(wǎng)上的寫(xiě)法,自己寫(xiě)了一個(gè)關(guān)于排序算法的調(diào)用,以供參考:

一、枚舉排序類(lèi)型

/*** @author zhugu* @version 1.0* @Date 2019/4/17 14:51* @Description 排序類(lèi)型*/ public enum SortType {SELECTION,BUBBLE,INSERT, } 復(fù)制代碼

二、編寫(xiě)一個(gè)排序接口,調(diào)用入口

/*** @author zhugu* @version 1.0* @Date 2019/4/17 14:15* @Description 排序*/ public interface Sort {SortType getSortType();int[] sorting(int[] sourceArray); } 復(fù)制代碼

三、編寫(xiě)幾個(gè)常見(jiàn)的排序算法
(1)、冒泡排序

/*** @author zhugu* @version 1.0* @Date 2019/4/17 14:15* @Description 冒泡排序*/ @Component public class BubbleSort implements Sort {@Overridepublic SortType getSortType() {return SortType.BUBBLE;}@Overridepublic int[] sorting(int[] sourceArray) {int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);for (int i = 0; i < arr.length; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] > arr[j]) {int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}return arr;} } 復(fù)制代碼

(2)、插入排序

/*** @author zhugu* @version 1.0* @Date 2019/4/17 14:17* @Description 插入排序*/ @Component public class InsertSort implements Sort {@Overridepublic SortType getSortType() {return SortType.INSERT;}@Overridepublic int[] sorting(int[] sourceArray) {// 7, 2, 4, 6, 3, 9, 1int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);printArr(arr);// 從下標(biāo)為1的元素開(kāi)始選擇合適的位置插入,因?yàn)橄聵?biāo)為0的只有一個(gè)元素,默認(rèn)是有序的for (int i = 1; i < arr.length; i++) {// 記錄要插入的數(shù)據(jù)int temp = arr[i], j = i;// 從已經(jīng)排序的序列最右邊的開(kāi)始比較,找到比其小的數(shù)while (j > 0 && temp < arr[j - 1]) {arr[j] = arr[j - 1];j--;}// 存在比其小的數(shù),插入if (j != i) {arr[j] = temp;}printArr(arr);}return arr;}private void printArr(int[] arr) {for (int x = 0; x < arr.length; x++) {if (x != arr.length - 1) {System.out.print(arr[x] + ", ");} else {System.out.println(arr[x]);}}} } 復(fù)制代碼

(3)、選擇排序

/*** @author zhugu* @version 1.0* @Date 2019/4/17 14:16* @Description 選擇排序*/ @Component public class SelectionSort implements Sort {@Overridepublic SortType getSortType() {return SortType.SELECTION;}@Overridepublic int[] sorting(int[] sourceArray) {int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);// 總共要經(jīng)過(guò) N-1 輪比較for (int i = 0; i < arr.length - 1; i++) {int min = i;// 每輪需要比較的次數(shù) N-ifor (int j = i + 1; j < arr.length; j++) {if (arr[j] < arr[min]) {// 記錄目前能找到的最小值元素的下標(biāo)min = j;}}// 將找到的最小值和i位置所在的值進(jìn)行交換if (i != min) {int temp = arr[i];arr[i] = arr[min];arr[min] = temp;}}return arr;} } 復(fù)制代碼

四、排序工廠,實(shí)現(xiàn) ApplicationContextAware 接口

/*** @author zhugu* @version 1.0* @Date 2019/4/17 14:56* @Description 排序工廠*/ @Component public class SortFactory implements ApplicationContextAware {private static Map<SortType, Sort> sortBeanMap = new ConcurrentHashMap<>(16);@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {Map<String, Sort> map = applicationContext.getBeansOfType(Sort.class);map.forEach((key, value) -> sortBeanMap.put(value.getSortType(), value));}public int[] sorting(SortType sortType, int[] sourceArray) {Sort sort = sortBeanMap.get(sortType);return sort.sorting(sourceArray);} } 復(fù)制代碼

五、測(cè)試

@RestController public class SortController {private final SortFactory sortFactory;public SortController(SortFactory sortFactory) {this.sortFactory = sortFactory;}@PostMapping(value = "factory/sort")public Object sortFactory(SortType sortType, int[] sourceArr) {return sortFactory.sorting(sortType, sourceArr);} } 復(fù)制代碼

響應(yīng)參數(shù):

[1,4,6,6,46,46,54,54,65,65,74,85,465,879,9874 ] 復(fù)制代碼

轉(zhuǎn)載于:https://juejin.im/post/5cb6d497f265da03a97ae269

總結(jié)

以上是生活随笔為你收集整理的使用Spring工厂模式管理多个类实现同一个接口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。