策略模式demo

策略模式学习demo

1.策略模式测试类

public class StrategyPatternDemo {
    public static void main(String[] args) {
        int[] array = {64, 25, 12, 22, 11};

        SortContext context = new SortContext(new BubbleSort());
        context.sort(array); // 使用冒泡排序

        System.out.println("排序后的数组: ");
        for (int num : array) {
            System.out.print(num + " ");
        }

        System.out.println();

        // 重新设置排序策略为选择排序
        context.setSortStrategy(new SelectionSort());
        context.sort(array); // 使用选择排序

        System.out.println("排序后的数组: ");
        for (int num : array) {
            System.out.print(num + " ");
        }
    }
}

2.排序策略接口

public interface SortStrategy {
    void sort(int[] array);
}

3.排序策略类

1.选择排序
public class SelectionSort implements SortStrategy {
    @Override
    public void sort(int[] array) {
        System.out.println("使用选择排序");
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            // 交换
            int temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;
        }
    }
}

2.冒泡排序

public class BubbleSort implements SortStrategy {
    @Override
    public void sort(int[] array) {
        System.out.println("使用冒泡排序");
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    // 交换
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }
}

4.策略上下文

public class SortContext {
    private SortStrategy sortStrategy;

    public SortContext(SortStrategy sortStrategy) {
        this.sortStrategy = sortStrategy;
    }

    public void setSortStrategy(SortStrategy sortStrategy) {
        this.sortStrategy = sortStrategy;
    }

    public void sort(int[] arr) {
        sortStrategy.sort(arr);
    }

}
好的,下面是一个简单的 Java 实现策略模式的示例代码: 首先,我们需要定义一个策略接口,例如: ```java interface Strategy { int execute(int a, int b); } ``` 然后,我们可以定义一些具体的策略类,例如: ```java class AddStrategy implements Strategy { @Override public int execute(int a, int b) { return a + b; } } class SubStrategy implements Strategy { @Override public int execute(int a, int b) { return a - b; } } class MultiplyStrategy implements Strategy { @Override public int execute(int a, int b) { return a * b; } } ``` 接下来,我们可以定义一个上下文类,用于执行策略。例如: ```java class Context { private final Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } public int executeStrategy(int a, int b) { return strategy.execute(a, b); } } ``` 最后,我们可以在客户端代码中使用上下文类来执行具体的策略。例如: ```java public class Client { public static void main(String[] args) { Context context = new Context(new AddStrategy()); int result = context.executeStrategy(1, 2); System.out.println(result); // 输出 3 context = new Context(new SubStrategy()); result = context.executeStrategy(3, 2); System.out.println(result); // 输出 1 context = new Context(new MultiplyStrategy()); result = context.executeStrategy(2, 3); System.out.println(result); // 输出 6 } } ``` 这个示例演示了如何使用策略模式来实现根据不同的策略执行不同的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值