Design pattern-Template method pattern
Reading notes are not original, summary:
Template method Pattern Definition
Define The skeleton of an algorithm in a operation,deferring some steps to Subclasses,template Method lets subclasses red Efine certain steps of an algorithm without changing the algorithm ' s structure.
Template method Pattern: Defines the skeleton of an algorithm in a method, and delays some steps into subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm.
Template Method Mode further introduction
The template pattern is really simple and uses only the Java integration mechanism, but it is a very wide-ranging pattern. Among them, Absstractclass (in the code below) is called abstract template, its methods are divided into two categories;
Basic Methods
The basic method is also called the basic operation, which is the method implemented by the subclass, and the template method is called.
Template Method
Can have one or several, is generally a concrete method, that is, a framework to implement the basic method of scheduling, complete the fixed logic.
Code Listing Abstract template class
public abstract class abstractclass {//Basic method protected abstract void dosomething Span class= "Hljs-params" > () ; Basic method protected abstract void doanything () ; Template method public void Templatemethod () {//Call the basic method, complete the related logic dosometh ing () ; doanything () ; }
- Code Listing Concrete Template classes
Publicclass ConcreteClass1 extends AbstractClass{ @Override protected void dosomething(){//Business logic processing} @Override protected void doanything(){//Business logic Processing}} Public class ConcreteClass2 extends AbstractClass{ @Override protected void dosomething(){//Business logic processing} @Override protected void doanything(){//Business logic Processing}}
- Code Listing Scenario Classes
Publicclass client {public static void Span class= "Hljs-title" >main (String [] args) { class1 = new concreteclass1 () ; abstractclass class2 = new ConcreteClass2 () ; Call the template method class1 . templatemethod () ; class2 . templatemethod () ;}}
Design mode-Template method mode