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

最近小白在看 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
]
复制代码

转载于:https://juejin.im/post/5cb6d497f265da03a97ae269

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值