java设计模式-策略模式

在我们需要实现一个功能的时候,可以有多种算法来实现的时候,我们可以使用if…else或者case来选择对应的算法来实现功能。但是如果又有新的算法添加进来的时候,我们不得不重新修改之前的代码。

那么如何才能够对其进行优雅的编码而不会在有变化的时候去修改我们的原来的代码呢?

定义

策略模式定义了一系列的算法,它们具有共同的一些通性,通过这个通性纽带关联起来,在使用算法的时候可以相互切换。策略模式将算法的本身实现和使用算法隔离开来,这样对于算法的变化从而让调用者无法感知。

结构

这里写图片描述

策略模式涉及到3个角色:
- 使用环境(客户端):持有抽象策略的引用,对外提供了切换策略的方法
- 抽象策略:可以是接口也可以是抽象类,视具体情况而定
- 具体策略:实现了策略中的算法定义,对具体算法的封装

以一个排序为例

在我们对一组数据排序的时候,可以有多种算法选择:冒泡、选择、插入等等。

抽象策略

public interface ISort {
    void sort(int[] source);
}

具体策略

  • 冒泡排序

    public class BubbleSort implements ISort {
        @Override
        public void sort(int[] source) {
            int temp = 0;
            for (int i = 0; i < source.length - 1; i++) {
                for (int j = 0; j < source.length - 1 - i; j++) {
                    if (source[j] > source[j + 1]) {
                        temp = source[j];
                        source[j] = source[j + 1];
                        source[j + 1] = temp;
                    }
                }
            }
        }
    }
    
  • 插入排序

    public class InsertSort implements ISort {
        @Override
        public void sort(int[] source) {
            for (int i = 1; i < source.length; i++) {
                int temp = source[i];
                int j = i - 1;
                for (; j >= 0 && source[j] > temp; j--) {
                    //将大于temp的值整体后移一个单位
                    source[j + 1] = source[j];
                }
                source[j + 1] = temp;
            }
        }
    }
    
  • 选择排序

    public class ChooseSort implements ISort {
        @Override
        public void sort(int[] source) {
            for(int i=0;i<source.length;i++){
                int lowIndex = i;
    
                for(int j=i+1;j<source.length;j++){
                    if(source[j]<source[lowIndex]){
                        lowIndex = j;
                    }
                }
    
                //将当前第一个元素与它后面序列中的最小的一个 元素交换,也就是将最小的元素放在最前端
                int temp = source[i];
                source[i] = source[lowIndex];
                source[lowIndex] = temp;
            }
        }
    }
    

Context

    public class SortContext {
        private ISort sort;

        public void setSort(ISort sort) {
            this.sort = sort;
        }

        public void func(int[] source) {

            System.out.println("排序前的数据:" + Arrays.toString(source));
            sort.sort(source);
            System.out.println("排序后的数据: " + Arrays.toString(source));
        }

        public static void main(String[] args) {
            int[] source = {56,2,345,12,9,7,56};

            int[] source1 = source.clone();
            int[] source2 = source.clone();
            int[] source3 = source.clone();

            SortContext sortContext = new SortContext();
            sortContext.setSort(new BubbleSort());
            sortContext.func(source1);
            System.out.println("-------------------------------------");
            sortContext.setSort(new ChooseSort());
            sortContext.func(source2);
            System.out.println("-------------------------------------");
            sortContext.setSort(new InsertSort());
            sortContext.func(source3);
        }
    }

结果:

排序前的数据:[56, 2, 345, 12, 9, 7, 56]
排序后的数据: [2, 7, 9, 12, 56, 56, 345]
-------------------------------------
排序前的数据:[56, 2, 345, 12, 9, 7, 56]
排序后的数据: [2, 7, 9, 12, 56, 56, 345]
-------------------------------------
排序前的数据:[56, 2, 345, 12, 9, 7, 56]
排序后的数据: [2, 7, 9, 12, 56, 56, 345]

可以看到,我们使用不同的策略算法来达到了我们目的,在我们的客户端并不清楚它们之间的区别。策略模式的核心就是如何去调度这些算法而不是如何实现这些算法。

策略模式的优点是可以动态的切换算法来改变客户端的行为。但是它的缺点也是比较明显的,客户端需要知道所有的策略实现,如果策略比较多,那么不仅仅是类增加了,客户端管理起来也是一个麻烦事。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值