In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Template method mode
Definition: the template method pattern defines the skeleton of an algorithm in a method, delaying some steps to subclasses. The template method enables subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm. The template method is a fixed-step "algorithmic" skeleton method. The mutable part of this algorithm is implemented by inheritance and overloaded in subclasses. In this way, when the skeleton of the algorithm remains unchanged, the detailed steps of the algorithm can be changed according to different requirements. The class diagram is as follows:
Advantages:
The ● template method pattern defines a set of algorithms and leaves the specific implementation to the subclass.
● template method pattern is a basic technology of code reuse.
The ● template method pattern leads to a reverse control structure, which invokes the operation of its subclass through a parent class and adds new behavior through the extension of the subclass, which conforms to the "open-close principle".
Disadvantages:
Each different implementation of ● requires a subclass to implement, resulting in an increase in the number of classes, resulting in a larger system.
Use the scene:
● implements an immutable part of an algorithm at once, leaving variable behavior to subclasses to implement.
Common behaviors in ● subclasses should be extracted and grouped into a common parent class to avoid code duplication.
● controls the extension of subclasses.
Case 1: there are many common steps for making tea and coffee, as follows:
● boiling water
● is added to brew tea (coffee powder)
● adds seasoning (such as honey, lemon, sugar) according to demand.
● poured the tea into the cup
The code for making tea is as follows (coffee is also similar, so it is omitted):
Public class Tea {/ / execute step void prepareRecipe () {boilWater (); steepTeaBag (); pourInCup (); addLemon ();} public void boilWater () {System.out.println ("Boiling water");} public void steepTeaBag () {System.out.println ("Steeping the tea");} public void addLemon () {System.out.println ("Adding Lemon") } public void pourInCup () {System.out.println ("Pouring into cup");}}
It is not difficult to find that the code in these two classes repeats a lot. The steps are the same. We can encapsulate them and modify them with the template method pattern, as follows:
Template method code:
Public abstract class CaffeineBeverage {final void prepareRecipe () {boilWater (); brew (); pourInCup (); addCondiments ();} abstract void brew (); abstract void addCondiments (); void boilWater () {System.out.println ("Boiling water");} void pourInCup () {System.out.println ("Pouring into cup");}}
Tea brewing code:
Public class Tea extends CaffeineBeverage {public void brew () {System.out.println ("Steeping the tea");} public void addCondiments () {System.out.println ("Adding Lemon");}}
Summary: subclasses can flexibly implement the specific logic of each step, and the execution steps are determined by the prepareRecipe () method in the abstract class.
Case 2: for the use of a hook, the hook is a boolean state that controls whether a step code needs to be executed in prepareRecipe () (the subclass modifies itself), as follows:
Public abstract class CaffeineBeverageWithHook {void prepareRecipe () {boilWater (); brew (); pourInCup (); if (customerWantsCondiments ()) {addCondiments ();}} abstract void brew (); abstract void addCondiments (); void boilWater () {System.out.println ("Boiling water");} void pourInCup () {System.out.println ("Pouring into cup") } / / Hook method boolean customerWantsCondiments () {return true;}}
Template method mode in Java:
● Applet
JFrame in ● Swing
All non-abstract methods in ● java.io.InputStream, java.io.OutputStream, java.io.Reader, and java.io.Writer.
● java.util.AbstractList, java.util.AbstractSet, and all non-abstract methods in java.util.AbstractMap.
Design principle: good rookie principle, do not look for us, we will find you.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.