策略模式实现对数组的普通排序和特殊排序

策略模式实现对数组的普通排序和特殊排序 —> {1, 3, 7, 9, 8, 6, 2}

/**
 * 策略+工厂+模板
 * @Date: 2022/9/16  9:46
 * @Version 1.0
 * 策略模式实现对数组的普通排序和特殊排序  ————> {1, 3, 7, 9, 8, 6, 2}
 */
public class Test3 {
    public static void main(String[] args) {
        int[] arr = {1, 3, 2, 6, 7, 8, 9};
        int num = 0;
        Template template = new NormalSort();
        template.sort(arr, num);
    }

    //定义模板接口
    public interface Template {
        void sort(int[] arr, int num);
    }

    //模板实现类,定义具体的代码逻辑
    public static abstract class TemplateImplementation implements Template {
        @Override
        public void sort(int[] arr, int num) {
            //共有流程,排序
            CommonSort(arr);
            //特有逻辑,不同的排序策略
            doSpeciaThing(arr, num);
            //共有逻辑,打印
            CoownedPrint(arr);
        }

        //特有逻辑,不同的排序策略,给不出具体实现,定义出抽象方法
        protected abstract void doSpeciaThing(int[] arr, int num);

        //共有逻辑,打印
        protected void CoownedPrint(int[] arr) {
            System.out.println(Arrays.toString(arr));
        }

        //共有流程,排序
        protected void CommonSort(int[] arr) {
            Arrays.sort(arr);
        }
    }

    //排序策略的类 --->普通排序
    public static class NormalSort extends TemplateImplementation {
        @Override
        protected void doSpeciaThing(int[] arr, int num) {
            PolicyInterface policyInterface = SortStrategyFactory.getSortStrategy(num);
            policyInterface.SortMethod(arr);
        }
    }

//    public static class SpeciSort extends TemplateImplementation {
//        @Override
//        protected void doSpeciaThing(int[] arr, int num) {
//            PolicyInterface policyInterface = SortStrategyFactory.getSortStrategy(num);
//            policyInterface.SortMethod(arr);
//        }
//    }

    //策略模式接口
    public interface PolicyInterface {
        void SortMethod(int[] arr);
    }

    //策略1--->普通排序
    public static class CommonSortingStrategy implements PolicyInterface {
        @Override
        public void SortMethod(int[] arr) {
            //此处不需写逻辑,因为普通排序的逻辑在上面的共有流程已经实现
        }
    }

    //策略2--->特殊排序
    public static class SpecialSortingStrategy implements PolicyInterface {
        @Override
        public void SortMethod(int[] arr) {
            //定义新数组
            int[] arr2 = new int[arr.length];
            int startNum = 0;
            int endNum = arr2.length - 1;

            //遍历数组      //1,2,3,6,7,8,9
            for (int i = 0; i < arr.length; i++) {
                if (i % 2 == 0) {
                    arr2[startNum] = arr[i];
                    startNum++;
                } else {
                    arr2[endNum] = arr[i];
                    endNum--;
                }
            }
            for (int i = 0; i < arr2.length; i++) {
                arr[i] = arr2[i];
            }
        }
    }

    //策略工厂
    public static class SortStrategyFactory {
        public static PolicyInterface getSortStrategy(int num) {
            if (num == 0) {
                //普通排序
                return new CommonSortingStrategy();
            } else {
                //特殊排序
                return new SpecialSortingStrategy();
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值