Stategy模式(策略模式)是一种定义一系列算法的方法,但这些算法都有一个共同的功能,只是它们实现的过程不同,从而减少了各算法类与使用算法类之间的耦合性。
现在我们用Stategy模式进行实现一个排序,先看下面的UML图

Sort是提供各类排序方法,InsertSort是插入排序法,BubbleSort是冒泡排序法,SelectSort是选择排序法,但他们都是提供了排序的功能,而且都是从小到大进行排序,Context是个委托调用排序的方法。我们用代码来实现。
先创建接口Sort,里面只提供一个方法,sort
package com.tankiy.Stategy;
import java.util.List;
/**
* <p>Title: Stategy策略模式</p>
*
* <p>Description: 接口</p>
*
* @author Tankiy
* @version 1.0
*/
public interface Sort {
/**
* @param list 需要排序List
* @return 返回排序后
*/
public int[] sort(int[] arrays);
}
实现选择排序法InsertSort
package com.tankiy.Stategy;
import java.util.List;
/**
* <p>Title: Stategy策略模式</p>
*
* <p>Description: 插入排序</p>
*
* @author Tankiy
* @version 1.0
*/
public class InsertSort implements Sort {
public int[] sort(int[] arrays) {
for(int i = 1; i < arrays.length; i++) {
int select = arrays[i];
int j = 0;
//若前一位大于或者等于选择的数,那么则进行交换位置,然后插入
for(j = i; j > 0 && arrays[j - 1] >= select; j--) {
arrays[j] = arrays[j - 1];
}
arrays[j] = select;
}
return arrays;
}
}
冒泡排序
package com.tankiy.Stategy;
import java.util.List;
/**
* <p>Title: Stategy策略模式</p>
*
* <p>Description: 冒泡排序</p>
*
* @author Tankiy
* @version 1.0
*/
public class BubbleSort implements Sort{
public int[] sort(int[] arrays) {
//进行冒泡排序
for(int i = 1; i < arrays.length; i++) {
for(int j = 0; j < i; j++) {
//若前一位大于后一位,进行冒泡交换位置
if(arrays[j] > arrays[i]) {
int temp = arrays[j];
arrays[j] = arrays[i];
arrays[i] = temp;
}
}
}
return arrays;
}
}
选择排序
package com.tankiy.Stategy;
import java.util.List;
/**
* <p>Title: Stategy策略模式</p>
*
* <p>Description: 选择排序</p>
*
* @author Tankiy
* @version 1.0
*/
public class SelectSort implements Sort {
public int[] sort(int[] arrays) {
int min = 0;
//进行选择排序
int top = 0;
for(int i = 0; i < arrays.length - 1; i++) {
min = i;
for(int j = i + 1; j < arrays.length; j++) {
if(arrays[j] < arrays[min]) {
min = j;
}
//获取当前对换的大值
top = arrays[i];
//获取最小的并插入最左边
arrays[i] = arrays[min];
//大值进行切换
arrays[min] = top;
}
}
return arrays;
}
}
委托调用Context
package com.tankiy.Stategy;
/**
* <p>Title: Stategy策略模式</p>
*
* <p>Description: 委托管理</p>
*
* @author Tankiy
* @version 1.0
*/
public class Context {
public Sort sort;
public Context(Sort sort) {
this.sort = sort;
}
public int[] sort(int[] arrays) {
return sort(arrays);
}
}
测试用例
package com.tankiy.Stategy;
import java.util.Random;
/**
* <p>Title: Stategy策略模式</p>
*
* <p>Description: 测试</p>
*
* @author Tankiy
* @version 1.0
*/
public class SortTest {
/**
* @param args
*/
public static void main(String[] args) {
int[] arrays = new int[10];
Random random = new Random();
System.err.print("排序前:");
for(int i = 0; i < arrays.length; i++) {
//随机添加整数
arrays[i] = random.nextInt(10);
System.out.print(arrays[i] + ",");
}
System.err.print("\n");
System.err.print("插入排序:");
Sort bubbleSort = new InsertSort();
Context ctx = new Context(bubbleSort);
arrays = bubbleSort.sort(arrays);
for(int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i] + ",");
}
System.err.println("\n");
}
}
结果

策略模式好处大大减少了算法类与使用算法类的耦合性。若新增一个新的排序法,只需实现Sort接口,扩展性强。
538

被折叠的 条评论
为什么被折叠?



