Design Mode (7)-template (from the template of the Transaction Processing Application)

Source: Internet
Author: User

First, let's look at a database operationProgram:

 
# Include <iostream> using namespace STD; Class dataworks {public: void insertstu () {cout <"disconnect... "<Endl; cout <" enable transaction... "<Endl; cout <" execute insert () operation "<Endl; cout <" Commit transaction... "<Endl; cout <" disconnect... "<Endl;} void updatestu () {cout <" enable connection... "<Endl; cout <" enable transaction... "<Endl; cout <" execute the update () operation "<Endl; cout <" Submit the transaction... "<Endl; cout <" disconnect... "<Endl;} void deletestu () {cout <" disconnect... "<Endl; cout <" enable transaction... "<Endl; cout <" execute Delete () operation "<Endl; cout <" Submit the transaction... "<Endl; cout <" disconnect... "<Endl ;}; void main () {data=* data== new data=(); data=> insertstu (); data=> updatestu (); dataworks-> deletestu ();}

We can see that there are multiple repeated operations in the running result.

How can we avoid it? We use a template to encapsulate what we do not need to repeat.

check the new Code :

# Include <iostream> using namespace STD; Class datatemplate {PRIVATE: void beginconnection () {cout <"disconnect... "<Endl;} void begintransation () {cout <" enable transaction... "<Endl;} void committransation () {cout <" Commit transaction... "<Endl;} void closeconnection () {cout <" close connection... "<Endl;} public: Virtual void insertdata () = 0; virtual void updatedata () = 0; virtual void deletedata () = 0; void insertstu () {beginconnection (); begintransation (); insertdata (); committransation (); closeconnection () ;}void updatestu () {beginconnection (); begintransation (); updatedata (); committransation (); closeconnection ();} void deletestu () {beginconnection (); begintransation (); deletedata (); committransation (); closeconnection () ;}}; Class datadeal: public datatemplate {public: Virtual void insertdata () {cout <"execute insert () operation" <Endl;} virtual void updatedata () {cout <"execute Update () operation "<Endl;} virtual void deletedata () {cout <" execute Delete () operation "<Endl ;}; void main () {datadeal * datadeal = new datadeal (); datadeal-> insertstu (); datadeal-> updatestu (); datadeal-> deletestu ();}

Execute the following result:

The results are the same. However, if there are a lot of processing programs missing in our class, we only need to care about the business logic we want to process.

Template Method utility 1: saves subclass code.

In this way, if we abstract all the methods in the parent class, we will not reduce the subclass code.

Therefore, it is easy to write templates, but it is not so easy to write a good template method.

We need to achieve good extraction and put a fixed Implementation Method in the parent class method for implementation.

Template Method utility 2: prevents calling operations

In many books about the design pattern, the template method of the parent class is replaced by a simple method accumulation, such as display ** 1, display * 2, and so on. This creates an illusion for many readers that the template method is to call the following method, but it is not all about it.

We will continue to talk about the application and combination with other models later.

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.