1. 程式人生 > >策略設計模式和模板設計模式的區別

策略設計模式和模板設計模式的區別

策略設計模式:準備一組演算法,並將演算法封裝起來,使得它們可以互換;它的重心不是如何現實演算法而是如何組合. 客戶端得清楚演算法的情況. 模板方法設計模式:在基類中定義骨架方法,其他的延遲到子類中實現.

通過排序來比較兩者不同: 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