Talking about excellent design pattern--template method Talking about excellent design pattern--template method
2016-1-1 by Damon
Talk about the template method, a sentence summary
The parent class method calls the abstract method, the method abstract definition in the parent class, the actual implementation of the processing to the subclass, follow the process to complete the whole thing is "template method pattern".
What good is it?
Depending on the definition of the parent class, different subclasses can provide different implementations for the parent abstract template method, providing the ability to expand.
A simple small example
Parent class: Abstractdisplay
public abstract class AbstractDisplay { // method Abstract definition in parent class public abstract void open (); public abstract void Print (); public abstract void close (); // Parent class method calls abstract methods Public final void diplay () { open (); for (int i = 0; i < 5; i++) { print (); } &nbsP; close (); } }
Subclass: Chardisplay
public class CharDisplay extends AbstractDisplay { private char ch; public chardisplay (char ch) { this.ch = ch; } public void open () { // Concrete Implementation System.out.print ("<<"); } public void print () { // Concrete Implementation System.out.print (CH); } public void Close () { // Concrete implementation system.out.println (">>"); } }
Subclass: Stringdisplay
public class StringDisplay extends AbstractDisplay { private String string; private int width; public Stringdisplay (string string) { this.string = string; This.width = string.getbytes () .length; } public void open () { // Concrete Implementation printline (); } public void print () { // Concrete Implementation system.out.println ("|" + string + "|"); } public Void close () { // Concrete implementation printline (); } private void printline () { system.out.print ("+"); for (int i = 0; i < width; i++) { system.out.priNT ("-"); } system.out.println ("+"); } }
Test: Main
public class main { /** * <p>main</p> * <p></p> * @author damon * @date 2016 January 4 * @param args */ public static void Main (String[] args) { // TODO Auto-generated method stub abstractdisplay d1 = new chardisplay (' H '); &Nbsp; d1.diplay (); // abstractdisplay d2 = new stringdisplay ("Hello"); // D2.diplay (); } }
Excellent case analysis
The previous time in the study of SPRINGMVC front-end controller Dispatchservlet saw in the initialization process there are many similar "template method" disguised embodiment, the following examples to share:
Httpservletbean class
A disguised embodiment of the // template method pattern public final void init () throws ServletException { // Ignore other content // Template Method Calls Initservletbean (); } /** * subclasses may override this to perform custom initialization. * all bean properties of this servlet will have been set before this * method is invoked. * <p>This default implementation is empty. * @throws ServletException if subclass initialization fails * general translation: empty implementation, do not handle things, subclasses will provide initialization implementation * Analysis: Template method pattern in the parent class is defined as abstract compared to the template method pattern, Subclasses must provide implementations of, * here using the parent class template method null implementation, the child-bearing parent class protected, the benefit is that subclasses can have a choice of implementation */ protected void initservletbean () throws servletexception { }
Httpservletbean class subclass Frameworkservlet class
// Template Method sub-class implementation protected final void Initservletbean () throws ServletException { Getservletcontext (). log ("initializing spring frameworkservlet " " + getservletname () + "'"); if (this.logger.isInfoEnabled ()) { this.logger.info ("FrameworkServlet ' " + getservletname () + " ': initialization started "); } long starttime = system.currenttimemillis (); try { this.webApplicationContext = Initwebapplicationcontext (); &nbsP; initframeworkservlet (); } catch (Servletexception ex) { this.logger.error ("Context Initialization failed ", ex); throw ex; } catch (Runtimeexception ex) { this.logger.error ("context initialization failed", ex); throw ex; } if (this.logger.isInfoEnabled ()) { long elapsedtime = system.currenttimemillis () - startTime; this.logger.info ("FrameworkServlet ") + getservletname () + "': initialization completed in " + elapsedTime + " ms"); } }
The same design, such as
Frameworkservlet class
protected webapplicationcontext initwebapplicationcontext () { // Ignore other content if (!this.refresheventreceived) { // Template method call onrefresh (WAC); } } /** * Template method which can be overridden to add servlet-specific refresh work. * called after Successful context refresh. * <p>this implementation is empty. * @param context the current webapplicationcontext * @see #refresh () */ protected void onrefresh (Applicationcontext context) { // for subclasses: do nothing by default. }
Frameworkservlet Class Subclass Dispatcherservlet Class
The template method subclass implements protected void Onrefresh (ApplicationContext context) {initstrategies (context); }
Talking about excellent design pattern--template method