策略设计模式:准备一组算法,并将算法封装起来,使得它们可以互换;它的重心不是如何现实算法而是如何组合. 客户端得清楚算法的情况.
模板方法设计模式:在基类中定义骨架方法,其他的延迟到子类中实现.
通过排序来比较两者不同:
1.定义一个普通的选择排序类:
package designPattern.behaviouralType.strategyCompareTemplate;
import java.util.Arrays;
public class SimpleSort {
public static void main(String[] args) {
int [] arr = {1,3,2,5,19,0};
sortArr(arr);
System.out.println(Arrays.toString(arr));
}
private static void sortArr(int[] arr) {
int temp = 0;
for (int i = 0 ; i<arr.length-1;i++){
for (int j = i+1; j<arr.length; j++ ){
if(neddSwap(arr,i,j)){
swap(arr,i,j,temp);
}
}
}
}
private static void swap(int[] arr, int i, int j, int temp) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static boolean neddSwap(int[] arr, int i, int j) {
return arr[i]>arr[j];
}
}
模板方法模式根据不同类型的数组定义一组算法
package designPattern.behaviouralType.strategyCompareTemplate.template;
public abstract class ChoiceSorter<T> {
public void sort(T array){
setArr(array);
int length = getLength();
for (int i = 0; i<length-1; i++){
for (int j = i+1; j<length; j++){
if(needSwap(array,i,j)){
swap(array, i,j);
}
}
}
}
// 交换
protected abstract void swap(T array, int i, int j);
// 是否需要替换
protected abstract boolean needSwap(T array, int i, int j);
// 获得数组长度
protected abstract int getLength();
// 封装数组
protected abstract void setArr(T array);
}
package designPattern.behaviouralType.strategyCompareTemplate.template;
// int 类型数组
public class IntChoiceSorter extends ChoiceSorter<int[]> {
private int[] array;
@Override
protected void swap(int[] array, int i, int j) {
int temp = 0;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
@Override
protected boolean needSwap(int[] array, int i, int j) {
return array[i]>array[j];
}
@Override
protected int getLength() {
return array.length;
}
@Override
protected void setArr(int[] array) {
this.array = array;
}
}
package designPattern.behaviouralType.strategyCompareTemplate.template;
// double 数组
public class DoubleChoiceSorter extends ChoiceSorter<double[]> {
private double[] array;
@Override
protected void swap(double[] array, int i, int j) {
double temp = 0.0;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
@Override
protected boolean needSwap(double[] array, int i, int j) {
return array[i]>array[j];
}
@Override
protected int getLength() {
return array.length;
}
@Override
protected void setArr(double[] array) {
this.array = array;
}
}
package designPattern.behaviouralType.strategyCompareTemplate.template;
import java.util.List;
// 集合排序
public class ListChoiceSorter extends ChoiceSorter<List<Integer>> {
private List<Integer> list;
@Override
protected void swap(List<Integer> array, int i, int j) {
int temp = list.get(i);
list.set(i,list.get(j));
list.set(j,temp);
}
@Override
protected boolean needSwap(List<Integer> array, int i, int j) {
return list.get(i)>list.get(j);
}
@Override
protected int getLength() {
return list.size();
}
@Override
protected void setArr(List<Integer> array) {
this.list = array;
}
}
package designPattern.behaviouralType.strategyCompareTemplate.template;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TemplateChioceSorterTest {
public static void main(String[] args) {
// int数组排序
int[] intarray = {1,2,0,19,4,67};
ChoiceSorter<int[]> intSort = new IntChoiceSorter();
intSort.setArr(intarray);
intSort.sort(intarray);
System.out.println(Arrays.toString(intarray));
System.out.println("---------以上是 int 类型数组排序--------");
double[] doubleArray = {1.1,1.9,1.8,1.6,1.7,1.2};
ChoiceSorter<double[]> doubleSort = new DoubleChoiceSorter();
doubleSort.setArr(doubleArray);
doubleSort.sort(doubleArray);
System.out.println(Arrays.toString(doubleArray));
System.out.println("---------以上是 double 类型数组排序--------");
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(3);
list.add(4);
list.add(6);
list.add(10);
list.add(21);
ChoiceSorter<List<Integer>> listSort = new ListChoiceSorter();
listSort.setArr(list);
listSort.sort(list);
System.out.println(list);
System.out.println("---------以上是 list 集合排序--------");
}
}
具体实现直接由子类完成.
策略模式排序
package designPattern.behaviouralType.strategyCompareTemplate.strategy;
public class ChoiceSorter<T> {
private StrategySorter<T> strategySorter;
public ChoiceSorter(StrategySorter<T> strategySorter) {
this.strategySorter = strategySorter;
}
public void sort(T array){
strategySorter.setArray(array);
int length = strategySorter.getLength();
for (int i = 0; i<length-1; i++){
for (int j = i+1; j<length; j++){
if(strategySorter.needSwap(array, i ,j)){
strategySorter.swap(array,i,j);
}
}
}
}
}
package designPattern.behaviouralType.strategyCompareTemplate.strategy;
// int 类型数组
public class IntChoiceSorter extends StrategySorter<int[]> {
private int[] array;
@Override
public int getLength() {
return array.length;
}
@Override
public void setArray(int[] array) {
this.array = array;
}
@Override
protected void swap(int[] array, int i, int j) {
int temp = 0;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
@Override
protected boolean needSwap(int[] array, int i, int j) {
return array[i]>array[j];
}
}
package designPattern.behaviouralType.strategyCompareTemplate.strategy;
// double 数组
public class DoubleChoiceSorter extends StrategySorter<double[]> {
private double[] array;
@Override
public int getLength() {
return array.length;
}
@Override
public void setArray(double[] array) {
this.array = array;
}
@Override
protected void swap(double[] array, int i, int j) {
double temp = 0.0;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
@Override
protected boolean needSwap(double[] array, int i, int j) {
return array[i]>array[j];
}
}
package designPattern.behaviouralType.strategyCompareTemplate.strategy;
import designPattern.behaviouralType.strategyCompareTemplate.template.ChoiceSorter;
import java.util.List;
// 集合排序
public class ListChoiceSorter extends StrategySorter<List<Integer>> {
private List<Integer> list;
@Override
public int getLength() {
return list.size();
}
@Override
public void setArray(List<Integer> array) {
this.list = array;
}
@Override
protected void swap(List<Integer> array, int i, int j) {
int temp = list.get(i);
list.set(i,list.get(j));
list.set(j,temp);
}
@Override
protected boolean needSwap(List<Integer> array, int i, int j) {
return list.get(i)>list.get(j);
}
}
package designPattern.behaviouralType.strategyCompareTemplate.strategy;
public abstract class StrategySorter<T> {
public abstract int getLength();
public abstract void setArray(T array);
// 交换
protected abstract void swap(T array, int i, int j);
// 是否需要替换
protected abstract boolean needSwap(T array, int i, int j);
}
package designPattern.behaviouralType.strategyCompareTemplate.strategy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StrategyChoiceSorterTest {
public static void main(String[] args) {
int[] intarray = {1,2,0,19,4,67};
StrategySorter<int[]> intSort = new IntChoiceSorter();
ChoiceSorter<int[]> choiceSorterint = new ChoiceSorter<>(intSort);
choiceSorterint.sort(intarray);
System.out.println(Arrays.toString(intarray));
System.out.println("---------以上是 int 类型数组排序--------");
double[] doubleArray = {1.1,1.9,1.8,1.6,1.7,1.2};
StrategySorter<double[]> doubleSort = new DoubleChoiceSorter();
ChoiceSorter<double[]> choiceSorterdouble = new ChoiceSorter<>(doubleSort);
choiceSorterdouble.sort(doubleArray);
System.out.println(Arrays.toString(doubleArray));
System.out.println("---------以上是 double 类型数组排序--------");
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(3);
list.add(4);
list.add(6);
list.add(10);
list.add(21);
StrategySorter<List<Integer>> listsort = new ListChoiceSorter();
ChoiceSorter<List<Integer>> choiceSorterlist = new ChoiceSorter<>(listsort);
choiceSorterlist.sort(list);
System.out.println(list);
}
}
在实际操作之后,发现策略模式相对于模板方法模式创建的类多, 并且操作麻烦.
参考https://blog.csdn.net/shensky711/article/details/53418034