Java 语言 函数式编程实现策略模式的示例

Java阿木 发布于 2025-06-25 16 次阅读


Java 函数式编程实现策略模式示例

策略模式是一种行为设计模式,它定义了一系列算法,并将每一个算法封装起来,使它们可以互相替换。策略模式让算法的变化独立于使用算法的客户。在Java中,函数式编程是一种强大的编程范式,它强调使用不可变数据和纯函数。本文将结合Java函数式编程,通过一个示例来展示如何实现策略模式。

策略模式概述

策略模式的核心思想是将算法的封装与使用算法的客户解耦。在策略模式中,通常包含以下角色:

- Context(环境类):维护一个策略对象的引用,并负责调用策略对象的方法。

- Strategy(策略接口):定义所有支持的算法的公共接口。

- ConcreteStrategy(具体策略类):实现Strategy接口,定义所有支持的算法。

示例:排序算法

假设我们需要实现一个排序算法,包括冒泡排序、选择排序和插入排序。我们可以使用策略模式来设计这个系统。

1. 定义策略接口

java

public interface SortStrategy {


void sort(int[] array);


}


2. 实现具体策略类

java

public class BubbleSortStrategy implements SortStrategy {


@Override


public void sort(int[] array) {


for (int i = 0; i < array.length - 1; i++) {


for (int j = 0; j < array.length - 1 - i; j++) {


if (array[j] > array[j + 1]) {


int temp = array[j];


array[j] = array[j + 1];


array[j + 1] = temp;


}


}


}


}


}

public class SelectionSortStrategy implements SortStrategy {


@Override


public void sort(int[] array) {


for (int i = 0; i < array.length - 1; i++) {


int minIndex = i;


for (int j = i + 1; j < array.length; j++) {


if (array[j] < array[minIndex]) {


minIndex = j;


}


}


int temp = array[minIndex];


array[minIndex] = array[i];


array[i] = temp;


}


}


}

public class InsertionSortStrategy implements SortStrategy {


@Override


public void sort(int[] array) {


for (int i = 1; i < array.length; i++) {


int key = array[i];


int j = i - 1;


while (j >= 0 && array[j] > key) {


array[j + 1] = array[j];


j--;


}


array[j + 1] = key;


}


}


}


3. 定义环境类

java

public class SortContext {


private SortStrategy strategy;

public void setStrategy(SortStrategy strategy) {


this.strategy = strategy;


}

public void sort(int[] array) {


strategy.sort(array);


}


}


4. 使用策略模式

java

public class StrategyPatternDemo {


public static void main(String[] args) {


SortContext sortContext = new SortContext();

// 使用冒泡排序


sortContext.setStrategy(new BubbleSortStrategy());


int[] array1 = {64, 34, 25, 12, 22, 11, 90};


sortContext.sort(array1);


System.out.println("Sorted array with Bubble Sort: ");


printArray(array1);

// 使用选择排序


sortContext.setStrategy(new SelectionSortStrategy());


int[] array2 = {64, 34, 25, 12, 22, 11, 90};


sortContext.sort(array2);


System.out.println("Sorted array with Selection Sort: ");


printArray(array2);

// 使用插入排序


sortContext.setStrategy(new InsertionSortStrategy());


int[] array3 = {64, 34, 25, 12, 22, 11, 90};


sortContext.sort(array3);


System.out.println("Sorted array with Insertion Sort: ");


printArray(array3);


}

private static void printArray(int[] array) {


for (int value : array) {


System.out.print(value + " ");


}


System.out.println();


}


}


函数式编程的应用

在上述示例中,我们可以看到函数式编程的一些应用:

- 使用Lambda表达式作为策略对象:在Java 8及更高版本中,我们可以使用Lambda表达式来创建策略对象,从而简化代码。

- 使用Stream API进行数据处理:在排序算法的实现中,我们可以使用Stream API来处理数组,例如使用`Arrays.stream()`方法。

java

public class SortContext {


private SortStrategy strategy;

public void setStrategy(SortStrategy strategy) {


this.strategy = strategy;


}

public void sort(int[] array) {


Arrays.stream(array).sorted().toArray();


}


}


总结

通过结合Java函数式编程和策略模式,我们可以设计出灵活且易于扩展的代码。策略模式允许我们根据不同的需求选择不同的算法,而函数式编程则提供了更简洁、更易于理解的代码风格。在实际开发中,我们可以根据具体需求选择合适的策略和编程范式,以提高代码的可读性和可维护性。