敏捷软件开发(4)--- TEMPLATE METHOD & STRATEGY 模式

1.TEMPLATE METHOD 

泛型,也就是这个模式,是可以基于泛型的。

我们往往会有一些算法,比如排序算法。它的算法部分,我可以把它放在一个基类里面,这样具体类型的比较可以放在子类里面。

看如下冒泡排序算法:

package com.joyfulmath.agileexample.template.method;

/**
 * @author deman.lu
 * @version on 2016-06-09 10:04
 */
public abstract class BubbleSorter {
    private int operations = 0;
    protected int length = 0;
    protected int doSort()
    {
        operations = 0;
        if(length<=1)
            return operations;

        for(int nextToLast = length-2;nextToLast>=0;nextToLast--)
            for(int index = 0;index<=nextToLast;index++)
            {
                if(outOfOrder(index))
                {
                    swap(index);
                }
            }
        return operations;
    }

    protected abstract void swap(int index);
    protected abstract boolean outOfOrder(int index);

}

先看int的排序:

package com.joyfulmath.agileexample.template.method;

/**
 * @author deman.lu
 * @version on 2016-06-09 10:18
 */
public class IntBubbleSorter extends BubbleSorter{
    private int[] array = null;

    public int sort(int[] theArray)
    {
        array = theArray;
        length = theArray.length;
        return doSort();
    }

    @Override
    protected void swap(int index) {
        int temp = array[index];
        array[index]  = array[index+1];
        array[index+1] = temp;
    }

    @Override
    protected boolean outOfOrder(int index) {
        return array[index]>array[index+1];
    }
}

只要实现了比较和交换2个接口,就可以了。

在看看基于泛型的子类:

package com.joyfulmath.agileexample.template.method;

/**
 * @author deman.lu
 * @version on 2016-06-09 10:23
 */
public class GenericBubbleSorter<T extends Comparable> extends BubbleSorter {

    private T[] array = null;

    public int sort(T[] theArray)
    {
        array = theArray;
        length = theArray.length;
        return doSort();
    }

    @Override
    protected void swap(int index) {
        T temp = array[index];
        array[index]  = array[index+1];
        array[index+1] = temp;
    }

    @Override
    protected boolean outOfOrder(int index) {
        return array[index].compareTo(array[index+1])>0;
    }
}
public class BubbleDemo {

    public static void action()
    {
        Integer[] array = new Integer[]{
                1,2,3,5,6,8,10,0,2,3
        };
        GenericBubbleSorter<Integer> intBubleSorter = new GenericBubbleSorter<>();
        intBubleSorter.sort(array);
        for(int i=0;i<array.length;i++)
        {
            TraceLog.i(array[i].toString());
        }
    }
}

这样就可以实现冒泡排序了。

敏捷开发的原则,就是不一定要使用设计模式,看情况,看需要。所以这里可以说这个BubbleSorter有些多余,直接GenericBubbleSorter使用,并实现排序算法就可以,视具体情况而定。

但是有时候,我们希望把排序算法和具体的使用者隔离开来,或者说我希望修改排序算法,但不修改其他的代码,这样耦合就降低了。

2.STRATEGY 模式

关于策略模式的介绍,可以看我以前的博客:设计模式4---策略模式

这里我们介绍冒泡排序的另一种模式。

public class BubbleSorter {
    private int operations = 0;
    protected int length = 0;
    private SortHandler itsSortHandle = null;

    public BubbleSorter(SortHandler itsSortHandle) {
        this.itsSortHandle = itsSortHandle;
    }

    public int sort(Object array) {
        itsSortHandle.setArray(array);
        length = itsSortHandle.length();
        operations = 0;
        if (length <= 1)
            return operations;

        for (int nextToLast = length - 2; nextToLast >= 0; nextToLast--)
            for (int index = 0; index <= nextToLast; index++) {
                if (itsSortHandle.outOfOrder(index)) {
                    itsSortHandle.swap(index);
                }
                operations++;
            }
        return operations;
    }

}

这里把排序算法还是放在BubbleSorter里,他不知道谁要排序(SortHandler ),所以BubbleSorter & SortHandler 的实现类是 解耦的。

public class GenericSortHandle<T extends Comparable> implements SortHandler {
    private T[] array = null;

    @Override
    public void swap(int index) {
        T temp = array[index];
        array[index]  = array[index+1];
        array[index+1] = temp;
    }

    @Override
    public boolean outOfOrder(int index) {
        return array[index].compareTo(array[index+1])>0;
    }

    @Override
    public int length() {
        return array.length;
    }

    @Override
    public void setArray(Object array) {
        this.array = (T[]) array;
    }
}

这里可以做2个替换,一个是排序算法,一个是排序的素材。这就是策略模式,

算法可以替换,算法使用的环境是一致的。

public class BubbleDemo2 {
    public static void action()
    {
        Integer[] array = new Integer[]{
                1,2,3,5,6,8,10,0,2,3
        };
        GenericSortHandle<Integer> intBubleSorter = new GenericSortHandle<>();
        BubbleSorter bubbleSorter = new BubbleSorter(intBubleSorter);
        bubbleSorter.sort(array);
        for(int i=0;i<array.length;i++)
        {
            TraceLog.i(array[i].toString());
        }
    }
}

还是那句话,设计模式的使用,根据具体情况而定,如果需求,环境发生变化,就有可能从没有设计模式,到重构代码,运用设计模式。

这就是敏捷开发,根据需求变化而变换设计模式的使用,包括不使用任何模式!

参考:

《敏捷软件开发》 Robert C. Martin 

原文地址:https://www.cnblogs.com/deman/p/5572152.html