Design Mode note Template Method mode Template Method

Source: Internet
Author: User
Tags define abstract

Design Mode note Template Method mode Template Method

 

 

// TemplateMethod template method mode ---- class behavior mode

 

/*

1: Intention:

Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. TemplateMethod makes the subclass unchanged

The structure of an algorithm allows you to redefine certain steps of the algorithm.

2: motivation:

3: Applicability:

1> implement the unchanged part of the algorithm at one time, and leave the variable behavior to the subclass for implementation.

2> the public behaviors in each subclass should be extracted and centralized into a public parent class to avoid code duplication.

3> Control subclass extension.

4: structure:

AbstractClass:

TemplateMethod ()

{... // The locations of these... are fixed, and the subclass has no permission to change.

PrimitiveOperation1 ()

...

PrimitiveOperation2 ()

...

}

PrimitiveOperation1

PrimitiveOperation2

|

|

|

ConcreteClass:

PrimitiveOperation1 ()

PrimitiveOperation2 () // two classes that can be implemented again

5: participants:

1> AbstractClass:

1) define abstract primitive operations. Specific subclasses will redefine them to implement each step of an algorithm.

2) implement a template method and define the skeleton of an algorithm. This template not only calls primitive operations, but also calls definitions in AbstractClass

Or operations in other objects.

2> ConcreteClass:

Implement primitive operations to complete the steps related to the specific subclass in the algorithm.

6: collaboration:

ConcreteClass relies on AbstractClass to implement the unchanged steps in the algorithm.

7: effect:

1> specific operations.

2> specific AbstractClass operations

3> primitive operations

4> Factory Method

5> hook operation. It provides default behavior, and child classes can be expanded as necessary.

Note what is a primitive operation (pure virtual function) and what is a hook operation (with a default virtual function)

8: Implementation:

1> use C ++ for access control:

Primitive operations can be defined as protected members. This ensures that they can only be called by the template method (in fact, they can also be defined as private members, because

The subclass only needs to implement the definition again, instead of using it ). Primitive operations must be pure virtual functions, and the template method itself can be defined

Non-virtual member functions.

2> reduce primitive operations as much as possible:

An important purpose of defining a template method is to minimize the number of primitive operations that must be redefined when a subclass implements the algorithm.

3> naming conventions:

You can add a prefix to operations that must be redefined to identify them.

9: Sample Code :*/

 

// Assume that MyView inherits from View DoDisplay as a hook operation, because DoDisplay requires a default implementation

Void View: Display ()

{

// Set the focus to display the object

SetFocus ();

// Operation that can be redefined by subclass

DoDisplay ();

// Finally, remove the focus.

ResetFocus ();

}

 

Void view: DoDisplay (){}

 

 

Void MyView: DoDisplay ()

{

// Render the view's contents

// Do what you want to do. You can display anything you want.

}

 

 

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.