In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail the "Java design pattern template method pattern case analysis", the content is detailed, the steps are clear, the details are handled properly, I hope this "Java design pattern template method pattern example analysis" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
Template method mode
The template method pattern method (Template Method) defines the algorithm skeleton in an operation, and delays some steps of the algorithm to the subclass, so that the subclass can redefine some specific steps of the algorithm without changing the structure of the algorithm.
The template method pattern contains the following main roles:
Abstract class (Abstract Class): responsible for giving the outline and skeleton of an algorithm. It consists of a template method and several basic methods.
Abstract method (Abstract Method): an abstract method is declared by an abstract class and implemented by its own subclass.
Concrete method (Concrete Method): a concrete method is declared and implemented by an abstract class or concrete class, whose subclasses can be overridden or directly inherited.
Hook methods (Hook Method): have been implemented in abstract classes, including logical methods for judgment and empty methods that require subclass overrides. A general hook method is a logical method used for judgment, which is generally called isXxx, and the return type is of type boolean.
Template method: defines the skeleton of the algorithm and calls the basic methods it contains in a certain order.
Basic method: it is the method to realize each step of the algorithm. The basic methods can be divided into three types:
Concrete subclass (Concrete Class): implements the abstract and hook methods defined in the abstract class, which are the constituent steps of a top-level logic.
[case]
The steps of cooking are fixed, including pouring oil, hot oil, vegetables, seasoning, stir-frying and so on. But you can pour in different vegetables and different sauces. Now we use the template method mode to simulate with code.
Public class TemplateTest {public static void main (String [] args) {/ / Fried cabbage BaoCai baoCai = new BaoCai (); baoCai.cookProcess (); System.out.println ("-"); / / Fried cabbage BaiCai baiCai = new BaiCai (); baiCai.cookProcess () } abstract class AbstractClass {/ / abstract class / / template method defines public final void cookProcess () {this.pourOil (); / / pour oil this.heatOil (); / / hot oil this.pourVegetable (); / / pour vegetable this.pourSauce (); / / pour seasoning this.fry (); / / stir-fry} public abstract void pourVegetable () / / different vegetables (one cabbage and one cabbage) public abstract void pourSauce (); / / different seasonings public void pourOil () {System.out.println ("pour oil");} public void heatOil () {System.out.println ("hot oil");} public void fry () {System.out.println ("stir-fry") } class BaoCai extends AbstractClass {public void pourVegetable () {System.out.println ("add cabbage");} public void pourSauce () {System.out.println ("add chili sauce");}} class BaiCai extends AbstractClass {public void pourVegetable () {System.out.println ("add cabbage");} public void pourSauce () {System.out.println ("add salt and monosodium glutamate");}}
[running result]
Pour oil
Hot oil
Add cabbage
Add chili sauce
Stir-fry and fry
-
Pour oil
Hot oil
Add cabbage
Add salt and monosodium glutamate
Stir-fry and fry
Process finished with exit code 0
Advantages and disadvantages of template method pattern
Advantages:
1. Improve code reusability, put the same part of the code in the abstract parent class, and put different code into different subclasses.
2. Realize the reverse control, call the operation of its subclass through a parent class, extend different behaviors through the concrete implementation of the subclass, realize the reverse control, and conform to the "opening and closing principle".
Disadvantages:
1. A subclass needs to be defined for each different implementation, which will lead to an increase in the number of classes, a larger system, and a more abstract design.
2. The abstract method in the parent class is implemented by the subclass, and the result of the execution of the subclass will affect the result of the parent class, which leads to a reverse control structure, which improves the difficulty of code reading.
Working with scen
1. The overall step of the algorithm is very fixed, but when some parts of the algorithm are changeable, the template method pattern can be used to abstract the easily changed parts for subclass implementation.
2. It is necessary to use the subclass to determine whether a step in the parent class algorithm is executed or not, so as to realize the reverse control of the subclass to the parent class.
After reading this, the article "template method pattern example Analysis of Java Design pattern" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.
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.