zoukankan      html  css  js  c++  java
  • 设计模式—模板方法

    一、模板方法的定义

    Template Method Pattern:Definethe skeleton of an algorithm in an operation,deferring some steps tosubclasses.Template Methodletssubclasses redefine certain steps of an algorithmwithoutchanging the algorithm's structure.

    定义一个操作中算法的骨架,将一些步骤的执行延迟到子类,模板方法让子类在不改变算法的结构下重新定义一个算法的步骤。

    二、模板方法的应用场景

    根据模板方法的定义,定义一个操作中的算法的骨架,其实就是在抽象类中定义一个模板方法,该模板方法里面会有一系列的方法的执行步骤,其中有些方法是该抽象类中的方法,它是所有子类公用的方法,有些方法是需要在子类中去实现的方法。因此,模板方法通常使用在,对于不同类型的他们具有相同的执行步骤,并且他们具有部分相同的行为。我们可以将相同的行为提取到抽象父类中去实现,不同的行为留给子类实行,从而实现代码的复用。模板方法是基于继承实现的代码复用技术。下面我们将通过一个例子来理解模板方法的使用。

    三、模板方法示例

    现在假设我们需要构造一个建造房子的算法。建造一个房子需要包含以下几个步骤:

    建地基 、建柱子、砌墙及建窗户。 假设这些步骤的执行顺序不能颠倒,及建地基必须在前其次建柱子,砌墙在建窗户,实际建房子其实也是这个基本步骤来的,那么下面我需要建两种类型的房子,一个是木房,一个是玻璃房。并且建地基和窗户是两种房子的共同的行为。那么下面我们将通过模板方法来实现这个过程。

     1 public abstract class HouseTemplate {
     2 
     3      public final void buildHouse(){
     4             buildFoundation();
     5             buildPillars();
     6             buildWalls();
     7             buildWindows();
     8             System.out.println("House is built.");
     9         }
    10 
    11      private void buildFoundation()
    12      {
    13          System.out.println("build foundation");
    14      }
    15      private void buildWindows()
    16      {
    17          System.out.println("build windows");
    18      }
    19 
    20      protected abstract void buildPillars();
    21 
    22      protected abstract void buildWalls();
    23 
    24 


    25 }
     1 public class GlassHouse extends HouseTemplate{
     2 
     3     @Override
     4     protected void buildPillars() {
     5         System.out.println("Building Glass Walls");
     6 
     7     }
     8 
     9     @Override
    10     protected void buildWalls() {
    11         System.out.println("Building Pillars with glass coating");
    12 
    13     }
    14 
    15 }
    public class WoodenHouse extends HouseTemplate{
    
    	@Override
    	protected void buildPillars() {
    		System.out.println("Building Wooden Walls");
    
    	}
    
    	@Override
    	protected void buildWalls() {
    		System.out.println("Building Pillars with Wood coating");
    
    	}
    
    }
    
     1 public class HousingClient {
     2 
     3     public static void main(String args[])
     4     {
     5          HouseTemplate houseType = new WoodenHouse();
     6 
     7             //using template method
     8             houseType.buildHouse();
     9             System.out.println("************");
    10 
    11             houseType = new GlassHouse();
    12 
    13             houseType.buildHouse();
    14     }
    15 
    16 }

    程序的输出结果如下:
    build foundation
    Building Wooden Walls
    Building Pillars with Wood coating
    build windows
    House is built.
    ************
    build foundation
    Building Glass Walls
    Building Pillars with glass coating
    build windows
    House is built.

    在抽象的父类中我们定义了buildHouse这个模板方法,该模板方法定义了算法的骨架,并将对buildPillars 和buildWalls的执行延迟到子类中执行。并且该模板方法被定义成final的,防止子类覆写来改变父类的行为,从而违反“开闭原则”。

    四、模板方法总结

    1)模板方法模式是一种类的行为型模式,在它的结构图中只有类之间的继承关系,没有对象关联关系。
     
    2)板方法模式是基于继承的代码复用基本技术,模板方法模式的结构和用法也是面向对象设计的核心之一。在模板方法模式中,可以将相同的代码放在父类中,而将不同的方法实现放在不同的子类中。
    3)在模板方法模式中,我们需要准备一个抽象类,将部分逻辑以具体方法的形式实现,然后声明一些抽象方法来让子类实现剩余的逻辑。不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现,这就是模板方法模式的用意。模板方法模式体现了面向对象的诸多重要思想,是一种使用频率较高的模式。
    4)在大多数情况下都是子类调用父类方法,但是在模板方法中时父类调用子类的方法,这通常被称作 Hollywood Principle,—— 不要打电话给我,我将打电话给你。
  • 相关阅读:
    HDU 2544 最短路
    HDU 3367 Pseudoforest
    USACO 2001 OPEN
    HDU 3371 Connect the Cities
    HDU 1301 Jungle Roads
    HDU 1879 继续畅通工程
    HDU 1233 还是畅通工程
    HDU 1162 Eddy's picture
    HDU 5745 La Vie en rose
    HDU 5744 Keep On Movin
  • 原文地址:https://www.cnblogs.com/justinli/p/template.html
Copyright © 2011-2022 走看看