策略设计模式和模板设计模式的区别

策略设计模式:准备一组算法,并将算法封装起来,使得它们可以互换;它的重心不是如何现实算法而是如何组合. 客户端得清楚算法的情况.

模板方法设计模式:在基类中定义骨架方法,其他的延迟到子类中实现.

通过排序来比较两者不同:
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 ; iarr[j];
    }

}

模板方法模式根据不同类型的数组定义一组算法

package designPattern.behaviouralType.strategyCompareTemplate.template;

public abstract class ChoiceSorter {

    public void sort(T array){
        setArr(array);
        int length = getLength();
        for (int i = 0; i {

    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 {
    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> {
    private List list;

    @Override
    protected void swap(List 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 array, int i, int j) {
        return list.get(i)>list.get(j);
    }

    @Override
    protected int getLength() {
        return list.size();
    }

    @Override
    protected void setArr(List 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 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 doubleSort = new DoubleChoiceSorter();
        doubleSort.setArr(doubleArray);
        doubleSort.sort(doubleArray);
        System.out.println(Arrays.toString(doubleArray));
        System.out.println("---------以上是 double 类型数组排序--------");

        List list = new ArrayList<>();
        list.add(1);
        list.add(3);
        list.add(4);
        list.add(6);
        list.add(10);
        list.add(21);
        ChoiceSorter> 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 {

    private StrategySorter strategySorter;

    public ChoiceSorter(StrategySorter strategySorter) {
        this.strategySorter = strategySorter;
    }

    public void sort(T array){
        strategySorter.setArray(array);
        int length = strategySorter.getLength();
        for (int i = 0; i {

    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 {

    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> {
    private List list;

    @Override
    public int getLength() {
        return list.size();
    }

    @Override
    public void setArray(List array) {
        this.list = array;
    }

    @Override
    protected void swap(List 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 array, int i, int j) {
        return list.get(i)>list.get(j);
    }


}

package designPattern.behaviouralType.strategyCompareTemplate.strategy;

public abstract class StrategySorter {

    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 intSort = new IntChoiceSorter();
        ChoiceSorter 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 doubleSort = new DoubleChoiceSorter();
        ChoiceSorter choiceSorterdouble = new ChoiceSorter<>(doubleSort);
        choiceSorterdouble.sort(doubleArray);
        System.out.println(Arrays.toString(doubleArray));
        System.out.println("---------以上是 double 类型数组排序--------");

        List list = new ArrayList<>();
        list.add(1);
        list.add(3);
        list.add(4);
        list.add(6);
        list.add(10);
        list.add(21);
        StrategySorter> listsort = new ListChoiceSorter();
        ChoiceSorter> choiceSorterlist = new ChoiceSorter<>(listsort);
        choiceSorterlist.sort(list);
        System.out.println(list);

    }
}

在实际操作之后,发现策略模式相对于模板方法模式创建的类多, 并且操作麻烦.

参考https://blog.csdn.net/shensky711/article/details/53418034

你可能感兴趣的:(设计模式)