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--
Summary of template patterns
In the previous article "template pattern introduction to Design patterns", we used daily scenes of making coffee and tea to explain the principle of template patterns. In this article, we will combine this small case to summarize the template pattern.
This article comes from a series of tutorials of "Kaige Learning Design patterns". Author: Kai GE Java (kaigejava)
Kaige personal blog: www.kaigejava.com
One: template pattern definition (or content)
In the previous article, we know that making tea and coffee requires five steps. These five steps are actually the skeleton of the algorithm.
When it comes to making coffee or tea, adding milk or lemon, these two specific steps are delayed to a specific real subclass. In this way, specific steps can be implemented without modifying the algorithm structure of making tea or coffee.
Summary template pattern definition:
First define the algorithm skeleton in an operation, and delay the implementation of one or some steps of the algorithm to the subclass, so that the subclass can redefine some specific steps of the current algorithm without modifying the structure of the current algorithm.
Template pattern is a kind of behavior pattern.
Two: roles
Abstract base class
A subclass that implements some concrete steps
Key code: common code is implemented in the base class (for example, boil water, pour into a cup, and send a guest these three steps are implemented in the base class hotdrink)
Other steps are implemented in a specific subclass (for example, whether to make tea or coffee powder is implemented in a specific subclass)
Three: use the scene
In software design, some functions are similar, but different in some aspects. When most of the links are the same, you can use template mode.
Extract general algorithms or steps into abstract classes and implement specific operations in concrete subclasses
Four: advantages and disadvantages
Advantages:
Encapsulate the unchanged part and extract the immutable part
Extend the variable part to set the variable abstract method and let the concrete subclass implement it.
Extracted common code for later maintenance
The behavior is controlled by the base class, and the specific operation is implemented by the subclass.
Disadvantages:
Each different implementation needs to have a subclass to implement, which will lead to a great increase in the number of classes and make the system larger.
Points to pay attention to:
In order to prevent the algorithm skeleton from being destroyed or operated maliciously, the template method is usually limited by the keyword final when using the template pattern.
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.