In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to realize the Template Method template mode". In the daily operation, I believe many people have doubts about how to realize the Template Method template mode. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the Template Method template mode". Next, please follow the editor to study!
As an example
If you do not understand the above intention introduction, it does not matter, the design pattern needs to be used in daily work, combined with examples can deepen your understanding, below I have prepared three examples to let you experience in what scenarios this design pattern will be used.
Template file
The documents we print are template documents. We only need to write down the basic personal information and then sign it. We don't need to do too much repetitive work, because most of the content can be solidified in some scenarios. For example, when buying and selling houses, most of the terms of Party An and Party B are fixed, and the biggest change is the difference between Party An and Party B. when we sign on the template, we use the template model to reduce a lot of time to write the terms.
Instantiation
Instantiation can also be thought of as a form of template pattern, because for factory methods, we pass in different initial values that may give different results, so we actually pry a large piece of function with very little code. played an abstract role.
Vue template
Vue templates are more in line with our intuitive understanding of templates. In this scenario, a template refers to a HTML template. We only need to describe some variables in the form of {} in the template to generate a template DOM with only local variable changes, which is very convenient.
Intention interpretation
Intent: define the skeleton of an algorithm in operation and defer some steps to subclasses. TemplateMethod enables subclasses to redefine certain steps of an algorithm without changing the structure of the algorithm.
This design pattern is originally intended for object-oriented, so the consideration is how to use the template pattern in the class. First of all, define a parent class, implement some algorithms, and then put forward the methods that need to be overloaded by the subclass. after the subclass overloads these methods, you can use the parent class to achieve some functions.
For example, the parent class method function a () {b () + c ()}, in which case the subclass only needs to redefine the b and c methods to reuse a's algorithm (b and c add). Of course, this example is relatively simple, when the algorithm is more complex, the benefits of template mode will be highlighted.
Structure diagram
ConcreteClass: the specific parent class. You can see that TemplateMethod is implemented in the parent class, which calls primitiveOperation1 and primitiveOperation2, so the subclass only needs to overload these two methods to enjoy the algorithm provided by TemplateMethod.
Assuming that TemplateMethod is the role of OpenDocument to open the document, then primitiveOperation1 may be a CanOpen check, and primitiveOperation2 may be a method for ReadDocument to read the document.
As long as we focus on implementing the specific details, we don't need to care about how they interact with each other, and the parents will help us achieve it. Then we can call the OpenDocument implementation of the subclass to open the document.
Code example
The following example is written in typescript.
Class View {
DoDisplay () {}
Display () {
This.setFocus ()
This.doDisplay ()
This.resetFocus ()
}
}
Class MyView extends View {
DoDisplay () {
Console.log ('myDisplay')
}
}
Const myView = new MyView ()
MyView.display ()
In this example, doDisplay represents the method that the parent class wants the subclass to overload, usually starting with the do convention.
Malpractice
The template pattern is essentially a fixed and immutable structure in a class, which further narrows the scope of the rewriting method, and the smaller the scope of rewriting, the higher the reusability of the code, so be sure to use it when you have a general algorithm that can be extracted. don't overuse it to save lines of code.
In addition, in the front-end development, HTML itself fits the template pattern very well, because there are a large number of tags in HTML describing the ever-changing UI structure, and there are too many reusable places, so it is very suitable for the template pattern, so do not think that the template pattern can only be used in the class, the template pattern can also be used in scaffolding, such as filling in some forms to automatically generate code.
When learning this design pattern, be careful not to solidify your thinking in the frame of its defined class, because the design pattern was written in 1994, and the patterns mentioned in it have been migrated and used massively. Whether you can identify and make appropriate knowledge transfer is the key to learning design patterns more than 20 years later.
At this point, the study on "how to achieve the Template Method template mode" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.