A combination based implementation of Spring integrated JDBC Template Method design pattern
Template design pattern refers to the corresponding template method is extracted in a specific location definition, and then the same call procedure operation through the template implementation.
For template design patterns, there are typically 2 ways
1. Implementation of inheritance-based approaches
2. Implementation based on the combination method
The previous implementation is: 1. Implementation based on inheritance, spring integration of JDBC Template method design pattern based on the implementation of inheritance
Next implementation: 2. Implementation based on a combination approach
The combination-based approach to spring's integration of JDBC template method Design patterns is especially handy in cases where we have a large number of classes.
This implementation simulates the JdbcTemplate class of Org.springframework.jdbc.core in spring.
Create a template class on the basis of 1: myjdbctemplate, detailed description see code.
Package Org.oms.spring.template;public class Myjdbctemplate {private void BeginConnection () {System.out.println (" Begin connection! ");} private void CloseConnection () {System.out.println ("close connection!");} /** * Call method, pass in the interface of a hook function */public void execute (Mycallback callback) {beginconnection (); Callback.dointemplate (); CloseConnection ();} /** * All the methods to be implemented are created in the template parameter modification ask the corresponding object */public void Add (Final Object object) {Execute (new Mycallback () {@Overridepublic void D Ointemplate () {System.out.println ("Add" + object);});} public void Delete (final int id) {Execute (new Mycallback () {@Overridepublic void Dointemplate () {System.out.println (" Delete "+ ID);}});} public void Update (final Object object) {Execute (new Mycallback () {@Overridepublic void Dointemplate () { SYSTEM.OUT.PRINTLN ("Update" + object);}})
Creating an interface Mycallback
Package Org.oms.spring.template;public interface Mycallback {public void dointemplate ();
modify the class Myjdbctemplatebyin in 1:
Package org.oms.spring.template;/** * Portfolio-based implementation template design pattern * * @author Sunlight * */public abstract class Myjdbctemplatebyin {Private myjdbctemplate mt=new myjdbctemplate ();p ublic void Add (Object object) {Mt.add (object);} public void Delete (int id) {mt.delete (id);} public void Update (Object object) {Mt.update (object);} public abstract void Run ();/** * There is a function called the hook function in the template method, the function of the hook function is to let the implementation class through some methods to control the process in the template * * @return */public Abstract Boolean Islog ();}
the Messagedao.java class and the Roledao.java class do not change, the implementation of the specific reference 1.
Test class and results:
Spring Integrated JDBC Template method design pattern based on a combination of implementation