最近小白在看 Spring IOC 和 AOP 源码时发现 Spring 中有很多类都实现了同一个接口,像下面这种
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 框架搞的鬼;所以小白就在网上找到了一圈、自己能看懂的一段代码,然后照着网上的写法,自己写了一个关于排序算法的调用,以供参考:
一、枚举排序类型
/**
* @author zhugu
* @version 1.0
* @Date 2019/4/17 14:51
* @Description 排序类型
*/
public enum SortType {
SELECTION,
BUBBLE,
INSERT,
}
二、编写一个排序接口,调用入口
/**
* @author zhugu
* @version 1.0
* @Date 2019/4/17 14:15
* @Description 排序
*/
public interface Sort {
SortType getSortType();
int[] sorting(int[] sourceArray);
}
三、编写几个常见的排序算法
(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)、插入排序
/**
* @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;
}
}
四、排序工厂,实现 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);
@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);
}
}
五、测试
@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);
}
}
响应参数:
[
1,
4,
6,
6,
46,
46,
54,
54,
65,
65,
74,
85,
465,
879,
9874
]