"Onlookers" design pattern (18)--Behavioral Template Method mode (Templatemethod pattern)

Source: Internet
Author: User

The template method pattern defines the steps of an algorithm and allows a subcategory to provide its practice in one or more steps. Let the subcategory redefine some of the steps in the algorithm without changing the algorithm schema. ----WIKIPEDIA


Personal Understanding

Template method mode is relatively simple, generally is defined by the abstract class template method, and then, the subclass through the inheritance and implementation of its parent class in a defined template needs to execute the specific method, when invoking the template method of the subclass object, the implementation of the method of the specific implementation of the class. This mode I personally feel a bit like a process-oriented operation, the completion of a process, followed by the next process.


Case Analysis

Template method is relatively simple, here is only one example for everyone to refer to.

Introduction: The car, is nothing more than start, open, parking, on these several, for such a process basically fixed behavior we can consider using template method mode to design.

Structure diagram of the class

Main code

Abstract class

Public abstract class Car {public abstract void startUp ();p ublic abstract void Run ();p ublic abstract void Stop ();p ublic vo ID Templatemethod () {this.startup (); This.run (); This.stop ();}}
Implementation Class
public class Bydcar extends car{@Overridepublic void startup () {System.out.println ("Bydcar start!) ");} @Overridepublic void Run () {System.out.println ("Bydcar run up! ");} @Overridepublic void Stop () {System.out.println ("Bydcar Stop! ");}}
public class Hongqicar extends car{@Overridepublic void startup () {System.out.println ("Hongqicar start!) ");} @Overridepublic void Run () {System.out.println ("Hongqicar run up! ");} @Overridepublic void Stop () {System.out.println ("Hongqicar Stop! ");}}
Test Class

public class Maintest {public static void main (string[] args) {Hongqicar Hongqicar = new Hongqicar (); Bydcar BYD = new Bydcar (); Hongqicar.templatemethod (); Byd.templatemethod ();}}

Template Method Pattern Advantages

1. Encapsulate the invariant part, extend the variable part: encapsulate the invariant parts to the parent class, and the mutable parts can continue to be extended by inheritance.

2. Extract the public part code for easy maintenance.

3. The behavior is controlled by the parent class, the subclass is implemented, the basic method is implemented by subclasses, so the subclass can increase the corresponding function by extending the method, and conform to the open and closed principle.


Template Method Pattern disadvantage

The template method pattern reverses the reading order, and we want to know that the order in which the template methods are executed in the subclass needs to be viewed in the parent class, which makes code reading difficult in complex projects.


Template Method Pattern Usage Scenarios

1. Multiple subclasses have common methods, and the logic is essentially the same.

2. Refactor to extract the same code into the parent class.


Source Download

Source


Recommended Reading

"Onlookers" design pattern (16)--the combination pattern of structure type (Composite pattern)

"Onlookers" design pattern (17)--structure-type of the Flyweight pattern



"Onlookers" design pattern (18)--Behavioral Template Method mode (Templatemethod pattern)

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.