A combination based implementation of Spring integrated JDBC Template Method design pattern

Source: Internet
Author: User
Tags object object

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.