Template pattern learning

definition:

Template Method pattern is a pattern of behavior class. Preparing an abstract class, the specific part of the logic implemented in the form of specific methods and constructors, and abstract method declarations to force the remaining subclasses implement logic. Different subclasses of abstract methods may be implemented in different ways, so that different implementations of the remaining logic. This is the intention of the template method pattern.

Simple implementation:

 

  Abstract template (Abstract Template) role has the following responsibilities:

  ■ abstract defines one or more operations, so that subclasses implement. These abstract operation called the basic operation, which is composed of a top-level logical step.

  ■ define and implement a template method. This method is generally a specific template method, which gives a top-level logical skeleton, which consists of a step in the corresponding abstract logical operations, deferred to subclasses implement. Top logic may also be some specific method calls.

 1 package org.codewy.pattern.template;
 2 
 3 /**
 4  * 模板模式抽象类
 5  */
 6 public abstract class Game {
 7 
 8     String name;
 9 
10     protected Game(String name) {
11         this.name = name;
12     }
13 
14     abstract void initialize();
15 
16     abstract void startPlay();
17 
18     abstract void endPlay();
19 
20     abstract void deleteGame();
21 
22     // 钩子方法
23     boolean isDeleteGame() {
24         return false;
25     }
26 
27     // 模板方法
28     public final void play() {
29 
30         initialize();
31 
32         startPlay();
33 
34         endPlay();
35 
36         if (isDeleteGame()) {
37             deleteGame();
38         }
39 
40     }
41 
42 }

  Specific template (Concrete Template) roles and responsibilities are as follows:

  ■ one or more abstract methods defined in the parent implementation, which is composed of a top-level logical step.

  ■ abstract template Each character can have any number of specific characters corresponding template, and each template character are those particular abstract method (i.e., component steps top logic) may be given different implementations, such that the top of the logic achieved varies.

 1 package org.codewy.pattern.template;
 2 
 3 public class LOL extends Game {
 4 
 5     public LOL(String name) {
 6         super(name);
 7     }
 8 
 9     @Override
10     void initialize() {
11         System.out.println(name + "初始化LOL游戏");
12     }
13 
14     @Override
15     void startPlay() {
16         System.out.println(name + "开始LOL游戏");
17     }
18 is  
. 19      @Override
 20 is      void endPlay () {
 21 is          System.out.println (name + "End Game LOL" );
 22 is      }
 23 is  
24      @Override
 25      void deleteGame () {
 26 is          System.out.println (name + "deleted LOL game " );
 27      }
 28  
29 }
 1 package org.codewy.pattern.template;
 2 
 3 public class PUBG extends Game {
 4 
 5     private boolean isDeleteGame;
 6 
 7     public PUBG(String name) {
 8         super(name);
 9     }
10 
11     public PUBG(String name, boolean isDeleteGame) {
12         this(name);
13         this.isDeleteGame = isDeleteGame;
14     }
15 
16     @Override
17     void the initialize () {
 18 is          System.out.println (name + "Initialization PUBG game" );
 . 19      }
 20 is  
21 is      @Override
 22 is      void startPlay () {
 23 is          System.out.println (name + "PUBG Start Game" );
 24      }
 25  
26 is      @Override
 27      void endPlay () {
 28          System.out.println (name + "end game PUBG" );
 29      }
 30  
31 is      @Override
 32      void deleteGame () {
 33 is          System.out.println (name + "delete the PUBG game " );
34     }
35 
36     @Override
37     boolean isDeleteGame() {
38         return isDeleteGame;
39     }
40 
41 }

Test Demo:

. 1  Package org.codewy.pattern.template;
 2  
. 3  public  class TemplatePatternDemo {
 . 4  
. 5      public  static  void main (String [] args) {
 . 6          Game lol = new new LOL ( "Boss Ying" );
 . 7          Game pubg = new new PUBG ( "Lee brother", to true );
 . 8          lol.play ();
 . 9          pubg.play ();
 10      }
 . 11  
12 is }

operation result:

 

 The actual application scenarios open source framework:

1、HttpServlet

  Implement a servlet needs to inherit HttpServlet, HttpServlet to use the template mode, HttpServlet defines a template method service (), the basic method doGet (), doPost (), etc., we own realization class overrides doGet (), doPost (), then the parent process to substantially achieve our detailed logic.

 

 

 2、SpringMVC

  HandlerInterceptor interface defines preHandle (), postHandle (), afterCompletion () method, the interceptor implementation class to implement these methods.

  

The method defined in template SpringMVC front controller class DispatcherServlet

 

 

 to sum up:

  1, has a plurality of the same subclass method, and the same logic, the methods can be used to put out a template abstract class.

  2, the same procedure of the main frame, different details, the template method may also be used.

 

Guess you like

Origin www.cnblogs.com/codewy/p/11565128.html