Template Method Design Mode (jdbcteampleta

Source: Internet
Author: User

Problem cause: if we do not use spring templeta, we can connect to the database through connection, statement, resultset ...... and so on.

But if we open jdbctempleta, we actually use the template design mode to encapsulate it. The template design pattern refers to extracting the corresponding template method (which is the duplicate code) and defining it in a specific position, and then operating the same call process, there are two methods for implementing the template design pattern through templates. 1. Based on inheritance implementation, for example: // abstract class package Org. zttc. ITAT. template;/*** implement template design mode based on inheritance * @ author administrator **/public abstract class myjdbctemplatebyin {private void beginconnection () {system. out. println ("begin connection");} private void closeconnection () {system. out. println ("close connection");} public abstract void run ();/** * In the template method, a function is called a hook function, the function of the hook enables the implementation class to control the process * @ return */Public Abstract Boolean islog (); Public void execute () {beginconnection (); if (islog () {system. out. println ("added logs");} Run (); closeconnection () ;}// implementation class package Org. zttc. ITAT. template; public class roledao extends myjdbctemplatebyin {private myjdbctemplate Mt = new myjdbctemplate (); Public void add (int id) {Mt. add (ID);} public void Delete (in T ID) {Mt. delete (ID) ;}@ overridepublic void run () {system. out. println ("role Add") ;}@ overridepublic Boolean islog () {return false ;}2. Combination-based implementation // hook function interface, the example here shows that the interface does not have to be used to inherit or implement (iplenments), but can also be used as a parameter. Package Org. zttc. ITAT. template; public interface mycallback {public void dointemplate ();} package Org. zttc. ITAT. template; public class myjdbctemplate {private void beginconnection () {system. out. println ("begin connection");} private void closeconnection () {system. out. println ("close connection");}/*** call the method and pass in the interface of a hook function. A common understanding is that an abstract class is used as a parameter, you can call the method in the method body so that you can implement the method when calling this method. This is equivalent to opening an empty check. It will be used at a time, but you can The amount already filled in. */Public void execute (mycallback call) {beginconnection (); call. dointemplate (); closeconnection () ;}/ *** create all the methods to be implemented in the template. * // a large question: why should I use the static parameter public void add (final int ID) {execute (New mycallback () {@ overridepublic void dointemplate () {system. out. println ("add:" + id) ;}}) ;}public void Delete (final int ID) {execute (New mycallback () {@ overridepublic void dointemplate () {system. out. println ("delete:" + id) ;}}}} package Org. zttc. ITAT. template; public class roledao extends myjdbctemplatebyin {private myjdbctemplate Mt = new myjdbctemplate (); Public void add (int id) {Mt. add (ID);} public void Delete (int id) {Mt. delete (ID) ;}@ override public void run () {system. out. println ("role Add") ;}@ override public Boolean islog () {return false ;}}

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.