Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java Design pattern-template method pattern

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Define Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.Template

Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's

Structure.

Define a framework for the algorithm in the operation, and defer some steps to subclasses. So that the subclasses can not be changed.

Changing the structure of an algorithm can redefine some specific steps of the algorithm.

The template method pattern is very simple, mainly using the inheritance mechanism of Java, without saying much, directly to the code.

Implement abstract template class public abstract class AbstractClass {/ * basic method * / protected abstract void doSomething (); / * basic method, which can have default implementation * / protected void doAnything () {System.out.println ("AbstractClass doAnything ()") } / * template method, in order to prevent malicious operations, general template methods add the final keyword, which is not allowed to be overwritten * / public final void templateMethod () {doSomething (); doAnything ();}} specific template class public class ConcreteClassA extends AbstractClass {@ Override protected void doSomething () {System.out.println ("ConcreteClassA doSomething ()") } @ Override protected void doAnything () {System.out.println ("ConcreteClassA doAnything ()-> I don't want to use the default implementation of the parent class, I want to override it");}} public class ConcreteClassB extends AbstractClass {@ Override protected void doSomething () {System.out.println ("ConcreteClassB doSomething ()") } / / use the default implementation of the parent class doAnything ()} client code public class Client {public static void main (String [] args) {AbstractClass a = new ConcreteClassA (); a.templateMethod (); AbstractClass b = new ConcreteClassB (); b.templateMethod () }} advantages encapsulate the invariant part, extend the variable part to extract the common part of the code, so that the maintenance behavior is controlled by the parent class, and the subclass implementation deficiency subclass affects the parent class.

According to our design custom, abstract classes are responsible for declaring the most abstract and general properties and methods of things, and implementing the completion of the class.

Specific attributes and methods of things. But the template method pattern is reversed, and the abstract class defines part of the abstract method, which is defined by the subclass

Implementation, the result of the execution of the subclass affects the result of the parent class, that is, the subclass has an impact on the parent class, which in complex projects

It will not only make it difficult to read the code, but also make novices feel uncomfortable.

The template method uses inheritance to reuse the code, and if you want to add a step to the basic algorithm, and the step is abstract, each subclass has to modify the code to implement this step. Multiple subclasses of the scenario have public methods, and the logic is basically the same. For important and complex algorithms, the core algorithm can be designed as a template method, and the relevant details around each

Subclass implementation.

When refactoring, the template method pattern is a frequently used pattern, extracting the same code into the parent class, and then passing through

The hook function (see the extension example below) constrains its behavior.

Expansion

The extension of the template method pattern is mainly the addition of the Hook Method method, so what is the "hook method"?

In an abstract template class, you can define a method and allow subclasses to override it as appropriate to change the execution of the basic method (such as determining whether certain steps need to be performed)

The function of the hook method

Let the subclass implement the optional part of the algorithm. Some of the steps in the algorithm are optional, and the subclass can decide whether these steps are needed. If the hook is not important to the implementation of the subclass, the subclass can ignore the hook.

The following is the common code for the template method pattern after adding the hook method:

Abstract template class public abstract class AbstractClass {/ * basic method * / protected abstract void doSomething (); / * basic method * / protected void doAnything () {System.out.println ("AbstractClass doAnything ()");} / * basic method dependent on hook method * / protected abstract void dependOnHook () / * template methods. In order to prevent malicious operations, general template methods add the final keyword, which is not allowed to be overwritten * / public final void templateMethod () {doSomething (); doAnything (); if (hook ()) {dependOnHook () }} / * hook method: empty implementation or default implementation, subclasses can be overridden The execution result of the public part is determined by the return value of a method of the subclass * @ return * / protected boolean hook () {System.out.println ("AbstractClass hook ()"); return true;}} concrete template class public class ConcreteClassA extends AbstractClass {@ Override protected void doSomething () {System.out.println ("ConcreteClassA doSomething ()") } @ Override protected void doAnything () {System.out.println ("ConcreteClassA doAnything ()-> I don't want to use the default implementation of the parent class, I want to override it");} @ Override protected void dependOnHook () {System.out.println ("ConcreteClassA dependOnHook ()") } / / without overriding the hook method, using the default implementation, dependOnHook () will be called} public class ConcreteClassB extends AbstractClass {@ Override protected void doSomething () {System.out.println ("ConcreteClassB doSomething ()");} / / use the default implementation of the parent class doAnything () @ Override protected void dependOnHook () {System.out.println ("ConcreteClassB dependOnHook ()") } / * override the hook method, change the default implementation, and change the behavior of the public part (template method). DependOnHook () will not be called * @ return * / @ Override protected boolean hook () {System.out.println ("ConcreteClassB hook ()"); return false;}}

Source code address: https://gitee.com/tianranll/java-design-patterns.git

Reference "Zen of Design patterns"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report