In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the template method pattern of Java design pattern". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the template method pattern of Java design pattern".
I. what is the template method pattern
The template method pattern defines the skeleton of an algorithm in a method while delaying the implementation of some steps to subclasses. The template method enables the subclass to redefine the specific implementation of some steps in the algorithm without changing the structure of the algorithm.
We often feel unfathomable when we see the word "design pattern", but the template method pattern is an exception, and all you have to focus on is a method.
The template method pattern is indeed very simple, using only the inheritance mechanism, but it is a very widely used pattern.
Second, the use scene of template method mode
When the skeleton of the algorithm in the system is fixed, and there may be many kinds of implementation of the algorithm, it is necessary to use the template method mode.
Multiple subclasses have common 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 surrounding details are implemented by each subclass.
When refactoring, the template method is a frequently used method that extracts the same code into the parent class and then constrains its behavior through the constructor.
For example: need to do a report printing program, the user requires the header, text, footer. But the customer's needs will change, one will want to display the header this way, and the other will want to show it that way.
It is appropriate to use the template method at this time.
Third, the advantages and disadvantages of the template method model.
Advantages:
Encapsulate the invariant part and extend the variable part. The algorithm that considers the invariant part is encapsulated into the parent class, while the algorithm of the variable part can be extended through inheritance. Extract the common part of the code for easy maintenance. The behavior is controlled by the parent class and implemented by the subclass
Disadvantages:
The abstract class needs to be modified when the algorithm skeleton needs to be changed.
According to the design custom, the abstract class is responsible for declaring the most abstract and general thing properties and methods, and the implementation class is responsible for completing the specific transaction properties and methods, but the template approach is just the opposite, and the result of the execution of the subclass affects the result of the parent class. will make it more difficult to read the code.
Fourth, the realization of template method pattern.
AbstractClass class-abstract template class, defines and implements a template method.
This template is generally a concrete method that gives a skeleton of the top-level logic, while the composition steps of the logic are deferred to the subclass implementation in the corresponding abstract operation.
Top-level logic also has specific methods that can be called.
Abstract class AbstractClass {/ / some abstract behaviors are put into subclasses to implement public abstract void PrivateOperation1 (); public abstract void PrivateOperation2 (); / / template methods, which give the framework of logic, while the composition of logic is composed of some corresponding abstract operations, which are postponed until the subclass implements public void TemplateMethod () {PrivateOperation1 (); PrivateOperation2 (); Console.WriteLine (");}}.
The ConcreteClass class that implements one or more abstract methods defined by the parent class.
Each AbstractClass can have any number of ConcreteClass corresponding to it, and each ConcreteClass can give different implementations of these abstract methods, thus making the implementation of the top-level logic different.
Class ConcreteClassA: AbstractClass {public override void PrivateOperation1 () {Console.WriteLine ("concrete class A method 1 implementation");} public override void PrivateOperation2 () {Console.WriteLine ("concrete class A method 2 implementation");}} class ConcreteClassB: AbstractClass {public override void PrivateOperation1 () {Console.WriteLine ("concrete class B method 1 implementation") } public override void PrivateOperation2 () {Console.WriteLine ("concrete class B method 2 implementation");}}
Client code
Static void Main (string [] args) {AbstractClass c; c = new ConcreteClassA (); c.TemplateMethod (); c = new ConcreteClassB (); c.TemplateMethod (); Console.Read () Thank you for reading, the above is the content of "how to understand the template method pattern of Java design pattern". After the study of this article, I believe you have a deeper understanding of how to understand the template method pattern of Java design pattern, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.