First, Mode analysis
Prepare an abstract class, implement some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the intent of the template method pattern.
The key points of the template pattern are:
1, there is a parent class, the overall business logic and common methods to encapsulate;
2. The parent class defines the specific differentiated business as an abstract method;
3, after the subclass inherits the parent class, realizes these abstract methods, completes to own business process;
4, subclasses can not change the parent class of the overall business logic flow;
5. Subclasses can also override other methods in the parent class to achieve customized business
Second, the mode code
Parent class abstract class
PackageTemplate.patten; Public Abstract classAbstracttemplate {//Business logic Execution Public voidTemplatemethod () {Abstractmethod (); Hookmethod (); Concretemethod (); } //abstract method that requires subclasses to overwrite protected Abstract voidAbstractmethod (); //hook method, can cover the quilt class protected voidHookmethod () {System.out.println ("I am the hook method in the template"); } //the specific method of the parent class can not be overridden Private voidConcretemethod () {System.out.println ("I am the parent of a specific method cannot be overridden"); }}
Subclasses implement specific business logic
Package Template.patten; Public class extends abstracttemplate { @Override publicvoid Hookmethod () { System.out.println ("Call the Hookmethod of the subclass implementation before calling the method of the parent class"); Super . Hookmethod (); } @Override protectedvoid Abstractmethod () { System.out.println (" The concrete implementation of the abstract method "); }}
Client Calls
Package Template.patten; Public class Client { publicstaticvoid main (string[] args) { abstracttemplate Template=new concretetemplate (); Template.templatemethod (); }}
Third, the application scenario
At present, the realization of business functions depends mainly on the definition of template pattern, and defines all the services into several templates:
1. Query template
2. Interface initialization Template
3. Empty template
4. Two-Step template
The two-step template is the most complex, followed by an example
Two-Step template-one of the most commonly used templates in work, mainly for transactions that need to modify the data, assuming the usual internal actions are the following steps
1. Check the correctness of the data
2. Record the transaction log and revise the status to be in progress
3. Conduct trading operations
4, according to the transaction results, modify the transaction log
So for a function, log and modify the transaction results are relatively fixed operations, so encapsulated in the template, check the transaction correctness and trade each transaction is different, so need to be implemented by specific functions
Iv. Application code
Packagetemplate.example;/** * @authorZJL * @time 2016-1-28 **/ Public Abstract classTwophasetrstemplate { Public voidExecute () {prepare (); Preaction (); Submit (); Afteraction (); } Public Abstract voidprepare (); Public Abstract voidsubmit (); Private voidpreaction () {System.out.println ("Transaction start, trading status is in progress, record transaction log"); } Private voidafteraction () {System.out.println ("The transaction was successful and the trading log status was updated to Success"); }}
For specific transactions, take the transfer as an example:
Package template.example; Public class extends twophasetrstemplate { @Override publicvoid prepare () { System.out.println ("Check all transfer elements"); } @Override publicvoid submit () { System.out.println ("transfer operation, Bank Core debit operations "); }
Client Calls
Package template.example; Public class Client { publicstaticvoid main (string[] args) { Transfer Transfer =new Transfer (); Transfer.execute (); }}
Execution Result:
Check all transfer elements trading start, trading status is in progress, record transaction log transfer operation, bank core debit operation successful, update transaction log status for success
[design mode at work] Template mode