Template method Pattern template methods (class behavior type)

Source: Internet
Author: User
Tags naming convention

Today, I reread the template method mode, the temperature is known new.

She is a basic technology based on inherited code reuse, and her structure and usage are also the core of object-oriented design.

Java aspect Application: In the Selvert is used.

Naming convention: The name of the hook method should begin with do.

You'll get a deeper understanding when you go to see the data yourself.

There are questions to ask here.

Interesting insights into design patterns: TEMPLATE method--Read the classic article on how to convince girls to go to bed. Girls from the understanding to go to bed the same steps are divided into coincidence, break the deadlock, start the pursuit, kiss, foreplay, hands-on, caress, go in eight Steps (Template method), but each step for different situations, there is not the same approach, it depends on you to improvise (concrete);

Template method Pattern: The template method pattern prepares an abstract class, implements part of the logic in the form of concrete methods and constructs, and then declares some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. First, a top-level logical framework is established, and the details of the logic are left to specific subclasses to implement.

-------------------------------

Template method Pattern template methods (class behavior type)

1. Overview

In the object-oriented development process, we typically encounter a problem where we know the key steps required by an algorithm and determine the order in which these steps are executed. But the concrete implementation of some steps is unknown, or the implementation of some steps is related to the specific environment.
Example 1: Bank Business process
When dealing with a bank, there are generally several basic fixed steps:
Queue-> to handle specific business-> to grade the bank staff.
The same is the logic of scoring the number of numbers and grading the bank staff. But handling specific business is a different, specific business may withdraw, deposit or transfer.

2. The question

How to ensure the normal implementation of the architecture logic, without the quilt class destruction.

3. The solution

Template method: Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The T-template method allows subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm. (Template method Pattern:definethe skeleton of a algorithm in operation,deferring some. Template Methodletssubclasses redefine certain steps of the algorithmwithoutchanging the algorithm ' s structure. )

1 Template method pattern is based on the inheritance of code reuse Basic Technology, template method pattern structure and usage is also one of the core of object-oriented design. In Template method mode, the same code can be placed in the parent class, and different method implementations are placed in different subclasses. 2 in the template method pattern, we need to prepare an abstract class, implement part of the logic in concrete ways and in the form of a concrete constructor, and then declare some abstract methods to enable the subclass to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic, which is the intention of the template method pattern. Template method pattern embodies many important ideas of object-oriented, and is a mode with high frequency.

4. Applicability

The template method applies to the following situations:
·1) A one-time implementation of an invariant part of the algorithm, and the variable behavior left to the subclass to implement.
2) Public behavior in each subclass should be extracted and lumped into a common parent class to avoid code duplication. First identify the differences in the existing code and separate the differences into new operations. Finally, replace these different code with a template method that invokes the new operations.
3) controls the subclass extension. The template method invokes only the "hook" operation at a specific point, so that only extensions are allowed at those points.

5. Structure


6. Composition of the pattern

Abstract class (AbstractClass): Defines an abstract primitive operation (primitive operation), the specific subclasses will redefine them to implement an algorithm, implement a template method, and define the skeleton of an algorithm. The template method invokes not only primitive operations, but also the definition
Specific subclasses (Concreteclass): Implements primitive operations to complete the steps associated with a particular subclass in an algorithm.

7. Effect

advantages of Template method patterns:
1 The template method pattern defines the algorithm formally in a class, and its subclasses implement the processing of the details.
2 Template method is a basic technique for code reuse. They are particularly important in class libraries, which extract public behavior from the class library.
3 The template method pattern leads to a reverse control structure, this structure is sometimes referred to as the "Hollywood Law", that is, "Don't look for us, we'll find you." Calling its subclasses through a parent class (instead of invoking the parent class by the opposite subclass), adding new behavior through the extension of the subclass, in line with the "open and closed" principle

Disadvantages of Template Method patterns:
Each implementation needs to define a subclass, which causes the number of classes to increase, the system larger, the design more abstract, but more in line with the "single responsibility Principle", so that the cohesion of the class to improve.

8. To achieve

We use the banking business implementation, add the hook method, the customer feel the evaluation service themselves.

<?php  /**   *  Template method mode:   *    *  @author  guisu    */     /**   *  abstract class    */   Abstract class  AbstractBank   {       private $_number ;       /**       * Template Method        *   Because subclasses cannot override a method that is defined as final. This ensures that the logic of subclasses is always controlled by the parent class.  

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.