惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 夜雨竹林

asp mvc3资料 设计模式之命令模式 设计模式之模板方法和策略模式的区别(二) 面向对象的分析和设计遵循的原则 系统分析师网上内容推荐 英语学习资料 Repository模式 战略性设计之上下文 moq英文官方资料 moq中文介绍 领域驱动设计软件核心复杂性应对之道速查 UML用例图中包含(include)、扩展(extend)和泛化(generalization)三种 领域驱动设计软件核心复杂性应对之道 研究 asp.net mvc2 ajax 原理 - 夜雨竹林 studyurl - 夜雨竹林 aspdotnet部分资源 Asp.net MVC权限设计思考 asp.net小技巧 标准的ASP.NET名称空间
设计模式之模板方法和策略模式的区别(一)
夜雨竹林 · 2011-07-23 · via 博客园 - 夜雨竹林

模板方法:

定义一个算法的大纲,而由其子类定义其中某些步骤的内容。而其算法的个别步骤可以有不同的实现细节。算法结构依然维持不变。用继承的方式改变算法中的具体步骤,依赖程度高,算法在父类(父类是抽象类)中实现,算法的具体步骤在子类中实现。

策略模式:

定义一个算法家族,并让这些算法可以互换。用组合的方式改变整个算法,依赖程度低,父类是接口类,算法在子类中具体实现。

 示例冒泡算法:

模板方法有三个类具体代码如下:

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);
  }

}

由代码可以看出算法被父类抽象出来了,具体的比如数组的长度、具体数组类型、如何交换、判断是否交换等都在子类里实现。