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; }