日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

设计模式之模板方法和策略模式的区别(一)

發布時間:2025/3/15 asp.net 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式之模板方法和策略模式的区别(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

模板方法:

定義一個算法的大綱,而由其子類定義其中某些步驟的內容。而其算法的個別步驟可以有不同的實現細節。算法結構依然維持不變。用繼承的方式改變算法中的具體步驟,依賴程度高,算法在父類(父類是抽象類)中實現,算法的具體步驟在子類中實現。

策略模式:

定義一個算法家族,并讓這些算法可以互換。用組合的方式改變整個算法,依賴程度低,父類是接口類,算法在子類中具體實現。

?示例冒泡算法:

模板方法有三個類具體代碼如下:

package asdppp.TmBSorter;
//父類為抽象類
public abstract class BubbleSorter
{
? private int operations = 0;

//使用protected是為了讓子類可以賦值?

?protected int length = 0;
//封裝了抽象的冒泡算法,之所以是抽象的因為在這個算法中swap(怎么交換)、outOfOrder(是否交換)被抽象出來了

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);
??????? operations++;
????? }

??? return operations;
? }
//算法中的swap、outOfOrder兩個步驟需要子類具體實現對哪些array進行操作
? protected abstract void swap(int index);
? protected abstract boolean outOfOrder(int index);
}

?

第一個子類

package asdppp.TmBSorter;

//繼承父類也就繼承了其算法
public class DoubleBubbleSorter extends BubbleSorter
{
? private double[] array = null;

?//兩個子類使用了父類的算法,兩個子類的數據類型參數是swap和outOfOrder雖然代碼相同但其實不同???

public int sort(double [] theArray)
? {
??? array = theArray;

//對父類算法的length進行賦值???

?length = array.length;
??? return doSort();
? }

?//具體實現了其算法中的具體步驟swap、outOfOrder,其實本例中子類的這兩個代碼是一樣的

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

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

?

第二個子類

?

package asdppp.TmBSorter;
//繼承父類也就繼承了其算法
public class IntBubbleSorter extends BubbleSorter
{
? private int[] array = null;
? public int sort(int [] theArray)
? {
??? array = theArray;
??? length = array.length;
??? return doSort();
? }
? //具體實現了其算法中的具體步驟swap、outOfOrder
? protected void swap(int index)
? {
??? int temp = array[index];
??? array[index] = array[index+1];
??? array[index+1] = temp;
? }

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

測試類

package asdppp.TmBSorter;
import junit.framework.TestCase;

public class TestBubbleSort extends TestCase
{
? public static void main(String[] args)
? {
??? junit.swingui.TestRunner.main(args);
? }
? public TestBubbleSort(String name)
? {
??? super(name);
? }

? public void testEmptyIntArray()
? {
??? int[] array = new int[0];
??? int operations = (new IntBubbleSorter()).sort(array);
??? assertEquals(0, operations);
? }

? public void testIntArrayWithOneElement()
? {
??? int[] array = {0};
??? int operations = (new IntBubbleSorter()).sort(array);
??? assertEquals(0, operations);
??? assertEquals(0, array[0]);
??? assertEquals(1, array.length);
? }

? public void testIntArrayWithTwoInOrderElements()
? {
??? int[] array = {0,1};
??? int operations = (new IntBubbleSorter()).sort(array);
??? assertEquals("operations",1, operations);
??? assertEquals(0, array[0]);
??? assertEquals(1, array[1]);
??? assertEquals(2, array.length);
? }

? public void testIntArrayWithTwoOutOfOrderElements()
? {
??? int[] array = {1,0};
??? int operations = (new IntBubbleSorter()).sort(array);
??? assertEquals("operations",1, operations);
??? assertEquals("array[0]", 0, array[0]);
??? assertEquals("array[1]", 1, array[1]);
??? assertEquals(2, array.length);
? }

? public void testIntArrayWithThreeOutOfOrderElements()
? {
??? int[] array = {3,2,1};
??? int operations = (new IntBubbleSorter()).sort(array);
??? assertEquals("operations", 3, operations);
??? assertEquals("array[0]", 1, array[0]);
??? assertEquals("array[1]", 2, array[1]);
??? assertEquals("array[2]", 3, array[2]);
??? assertEquals(3, array.length);
? }

? public void testIntArrayWithTenOutOfOrderElements()
? {
??? int[] array = {9,8,7,6,5,4,3,2,1,0};
??? int operations = (new IntBubbleSorter()).sort(array);
??? assertEquals("operations", 45, operations);
??? for (int i=0; i<10; i++)
????? assertEquals("array["+i+"]", i, array[i]);
? }

? public void testDoubleArrayWithTenOutOfOrderElements()
? {
??? double[] array = {9,8,7,6,5,4,3,2,1,0};
??? int operations = (new DoubleBubbleSorter()).sort(array);
??? assertEquals("operations", 45, operations);
??? for (int i=0; i<10; i++)
????? assertEquals("array["+i+"]", i, array[i], .001);
? }

}

由代碼可以看出算法被父類抽象出來了,具體的比如數組的長度、具體數組類型、如何交換、判斷是否交換等都在子類里實現。

?

轉載于:https://www.cnblogs.com/yelinpalace/archive/2011/07/23/2115092.html

總結

以上是生活随笔為你收集整理的设计模式之模板方法和策略模式的区别(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。