Template method Mode

Source: Internet
Author: User

Good: Transfer the immutable code parts to the parent class, leaving the mutable code in the subclass rewrite with virtual.

GOF defines the skeleton of an algorithm in an operation to the template method pattern, and delays some steps into subclasses. Enables subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. The structure of the algorithm here can be understood as a business process that you design based on your needs. Specific steps are those that may have variables in the content.

As you can see, the template method pattern is also designed to subtly address the impact of changes on the system. The use of template methods to enhance system extensibility, minimizing the impact of changes on the system. This, in the following example can be clearly seen.

See an example:

#include <iostream> #include <vector> #include <string> using namespace std; Class AbstractClass {public:void Show () {cout<< "I Am" <<getname () <<endl;} protected:virtual string Get Name () = 0; }; Class Naruto:public AbstractClass {protected:virtual string GetName () {return "the most handsome six-generation---blockbuster Naruto" in the History of Naruto;}; Class Onepice:public AbstractClass {protected:virtual string GetName () {return "I am a sea thief without evil---fly";}}; Client int main () {naruto* man = new Naruto (); Man->show (); onepice* man2 = new Onepice (); Man2->show (); return 0; }

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.