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

最近小白在看 Spring IOC 和 AOP 源码时发现 Spring 中有不少类都实现了同一个接口,像下面这种java

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

此种方式是如何实现的小白还不清楚,但确定知道是 Spring 框架搞的鬼;因此小白就在网上找到了一圈、本身能看懂的一段代码,而后照着网上的写法,本身写了一个关于排序算法的调用,以供参考:算法

1、枚举排序类型bash

/** * @author zhugu * @version 1.0 * @Date 2019/4/17 14:51 * @Description 排序类型 */
public enum SortType {
    SELECTION,
    BUBBLE,
    INSERT,
}
复制代码

2、编写一个排序接口,调用入口app

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

3、编写几个常见的排序算法
(1)、冒泡排序框架

/** * @author zhugu * @version 1.0 * @Date 2019/4/17 14:15 * @Description 冒泡排序 */
@Component
public class BubbleSort implements Sort {
    @Override
    public SortType getSortType() {
        return SortType.BUBBLE;
    }

    @Override
    public 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;
    }
}
复制代码

(2)、插入排序ide

/** * @author zhugu * @version 1.0 * @Date 2019/4/17 14:17 * @Description 插入排序 */
@Component
public class InsertSort implements Sort {
    @Override
    public SortType getSortType() {
        return SortType.INSERT;
    }

    @Override
    public int[] sorting(int[] sourceArray) {
        // 7, 2, 4, 6, 3, 9, 1
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        printArr(arr);

        // 从下标为1的元素开始选择合适的位置插入,由于下标为0的只有一个元素,默认是有序的
        for (int i = 1; i < arr.length; i++) {
            // 记录要插入的数据
            int temp = arr[i], j = i;
            // 从已经排序的序列最右边的开始比较,找到比其小的数
            while (j > 0 && temp < arr[j - 1]) {
                arr[j] = arr[j - 1];
                j--;
            }

            // 存在比其小的数,插入
            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]);
            }
        }
    }
}
复制代码

(3)、选择排序测试

/** * @author zhugu * @version 1.0 * @Date 2019/4/17 14:16 * @Description 选择排序 */
@Component
public class SelectionSort implements Sort {
    @Override
    public SortType getSortType() {
        return SortType.SELECTION;
    }

    @Override
    public int[] sorting(int[] sourceArray) {
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        // 总共要通过 N-1 轮比较
        for (int i = 0; i < arr.length - 1; i++) {
            int min = i;

            // 每轮须要比较的次数 N-i
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    // 记录目前能找到的最小值元素的下标
                    min = j;
                }
            }

            // 将找到的最小值和i位置所在的值进行交换
            if (i != min) {
                int temp = arr[i];
                arr[i] = arr[min];
                arr[min] = temp;
            }
        }

        return arr;
    }
}
复制代码

4、排序工厂,实现 ApplicationContextAware 接口this

/** * @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);
    @Override
    public 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);
    }
}
复制代码

5、测试spa

@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);
    }
}
复制代码

响应参数:code

[
    1,
    4,
    6,
    6,
    46,
    46,
    54,
    54,
    65,
    65,
    74,
    85,
    465,
    879,
    9874
]
复制代码
相关文章
相关标签/搜索