策略模式(Strategy Pattern):定义一系列算
法类,将每一个算法封装起来,并让它们可以相互替换,策略模式让算法独立于使用它的客
户而变化,也称为政策模式(Policy)。策略模式是一种对象行为型模式。
![](https://img.haomeiwen.com/i7484530/be5f872ce95be911.png)
在策略模式结构图中包含如下几个角色:
● Context(环境类):环境类是使用算法的角色,它在解决某个问题(即实现某个方法)时
可以采用多种策略。在环境类中维持一个对抽象策略类的引用实例,用于定义所采用的策
略。
● Strategy(抽象策略类):它为所支持的算法声明了抽象方法,是所有策略类的父类,它可
以是抽象类或具体类,也可以是接口。环境类通过抽象策略类中声明的方法在运行时调用具
体策略类中实现的算法。
● ConcreteStrategy(具体策略类):它实现了在抽象策略类中声明的算法,在运行时,具体策
略类将覆盖在环境类中定义的抽象策略类对象,使用一种具体的算法实现某个业务处理。
public class ArrayHandler {
private Sort sortObj;
public int[] sort(int arr[]) {
sortObj.sort(arr);
return arr;
}
public void setSortObj(Sort sortObj) {
this.sortObj = sortObj;
}
}
public interface Sort {
public abstract int[] sort(int arr[]);
}
--------------------------------------------------------------------
public class BubbleSort implements Sort {
public int[] sort(int arr[]) {
int len = arr.length;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
int temp;
if (arr[i] > arr[j]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
System.out.println("冒泡排序");
return arr;
}
}
--------------------------------------------------------------------
public class InsertionSort implements Sort {
public int[] sort(int arr[]) {
int len = arr.length;
for (int i = 1; i < len; i++) {
int j;
int temp = arr[i];
for (j = i; j > 0; j--) {
if (arr[j - 1] > temp) {
arr[j] = arr[j - 1];
} else
break;
}
arr[j] = temp;
}
System.out.println("插入排序");
return arr;
}
}
--------------------------------------------------------------------
public class QuickSort implements Sort {
public int[] sort(int arr[]) {
System.out.println("快速排序");
sort(arr, 0, arr.length - 1);
return arr;
}
public void sort(int arr[], int p, int r) {
int q = 0;
if (p < r) {
q = partition(arr, p, r);
sort(arr, p, q - 1);
sort(arr, q + 1, r);
}
}
public int partition(int[] a, int p, int r) {
int x = a[r];
int j = p - 1;
for (int i = p; i <= r - 1; i++) {
if (a[i] <= x) {
j++;
swap(a, j, i);
}
}
swap(a, j + 1, r);
return j + 1;
}
public void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
--------------------------------------------------------------------
public class SelectionSort implements Sort {
public int[] sort(int arr[]) {
int len = arr.length;
int temp;
for (int i = 0; i < len; i++) {
temp = arr[i];
int j;
int samllestLocation = i;
for (j = i + 1; j < len; j++) {
if (arr[j] < temp) {
temp = arr[j];
samllestLocation = j;
}
}
arr[samllestLocation] = arr[i];
arr[i] = temp;
}
System.out.println("选择排序");
return arr;
}
}
<?xml version="1.0"?>
<config>
<className>QuickSort</className>
</config>
------------------------------------------------------------------
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class XMLUtil {
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
public static Object getBean() {
try {
//创建文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("config.xml"));
//获取包含类名的文本节点
NodeList nl = doc.getElementsByTagName("className");
Node classNode = nl.item(0).getFirstChild();
String cName = classNode.getNodeValue();
//通过类名生成实例对象并将其返回
Class c = Class.forName(cName);
Object obj = c.newInstance();
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
public class Client {
public static void main(String args[]) {
int arr[] = {1, 4, 6, 2, 5, 3, 7, 10, 9};
int result[];
ArrayHandler ah = new ArrayHandler();
Sort sort;
sort = (Sort) XMLUtil.getBean();
ah.setSortObj(sort); //设置具体策略
result = ah.sort(arr);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + ",");
}
}
}
![](https://img.haomeiwen.com/i7484530/17f07fe1c7b40ccf.png)
网友评论