Big talk design pattern C + + implementation-10th chapter-Template Method mode

Source: Internet
Author: User

First, UML diagram


Second, the concept

Template Method Pattern: defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.


Third, the description

Role:

(1) AbstractClass: abstract class, in fact, is an abstract template, defines and implements a template method. This template method is generally a concrete method, it gives a top-level logical framework, and the logical composition of the steps in the corresponding abstract operation, deferred to the sub-class implementation. It is also possible for top-level logic to invoke some concrete methods.

(2) Concreteclass: implements one or more abstract methods defined by the parent class. Each abstractclass can have any number of concreteclass corresponding to it, and each concreteclass can give the different implementations of these abstract methods, which are the constituent steps of the top-level logic, so that the implementation of the top-level logic varies.

Summary: when immutable and mutable behaviors are mixed together in a subclass implementation of a method, the invariant behavior repeats itself in the subclass. We move these behaviors to a single place through the template method pattern, which helps the subclass to get rid of the entanglement of repeated invariant behavior.


Iv. implementation of C + +

(1) TestPaper.h:

#ifndef testpaper_h#define testpaper_h#include <iostream> #include <string>//abstractclass, a template was implemented, Defines the skeleton of the algorithm, the concrete steps to compose the skeleton are implemented in the subclass class Testpaper{public:void TestQuestion1 () {std::cout<< "Yang too Get, later gave Guo Jing, practice into the sword, The Dark iron of the dragon-Slayer knife may be "" a. Ball-mill cast iron B. tinplate c. High-speed alloy steel d. Carbon fiber "<<std::endl;std::cout<<" Answer: "<<answer1 () <<std::endl ;} void TestQuestion2 () {std::cout<< "Yang, Chengying, Lu Matchless to eradicate the emotional flower, resulting in" a. so that the plant no longer harms B. Extinction of a rare species C. destroying the ecological balance of the biosphere D. Causing desertification in the area "< <std::endl;std::cout<< "Answer:" <<answer2 () <<std::endl;} void TestQuestion3 () {std::cout<< "Blue Phoenix caused the Huashan apprentice, Peach Valley six cents vomit not only, if you are a doctor, will give them what medicine" "A. Aspirin B. Bezoar jiedu tablets C. haloperidol d. Let them drink a lot of milk" < <std::endl;std::cout<< "Answer:" <<answer3 () <<std::endl;} Protected:virtual std::string Answer1 () {return "";} Virtual std::string Answer2 () {return "";} Virtual std::string Answer3 () {return "";}};/ /concreteclass, implement specific steps class Testpapera:public testpaper{protected:virtual std::string Answer1 () {return "B";} Virtual std::string Answer2 () {return "C";} Virtual Std::striNg Answer3 () {return "a";}};/ /concreteclass, implement specific steps class Testpaperb:public testpaper{protected:virtual std::string Answer1 () {return "C";} Virtual std::string Answer2 () {return "a";} Virtual std::string Answer3 () {return "a";}}; #endif


(2) Main.h:

#include "TestPaper.h" #include <iostream> #include <cstdlib>//client, client void Main () {std::cout<< " Student a copy of the paper: "<<std::endl; testpaper* studenta=new Testpapera (); Studenta->testquestion1 (); Studenta->testquestion2 ();studentA-> TestQuestion3 ();std::cout<<std::endl;std::cout<< "Student B-Copy paper:" <<std::endl; testpaper* studentb=new Testpaperb (); Studentb->testquestion1 (); Studentb->testquestion2 ();studentB-> TestQuestion3 (); Std::cout<<std::endl;delete studenta;delete studentb;system ("pause");}


(3) Operation result:







Big talk design pattern C + + implementation-10th chapter-Template Method mode

Related Article

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.