In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the js template pattern", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the js template pattern"!
What is template mode?
The template pattern is an abstract parent class that defines related methods that subclasses need to override. These methods, however, are still called through parent class methods. According to the description, the idea of "template" is embodied in the interface method defined by the parent class. In addition, calls to subclass methods are also controlled by the parent class.
Application scenario
Some system architecture or algorithm framework, by the "BOSS" to write abstract methods, specific implementation, to the "boys" to achieve. And definitely not using the "boys" method, or watching "BOSS" mood. It's not a very appropriate metaphor, huh?
ES6 implementation
Animal is an abstract class, and Dog and Cat implement the eat () and sleep () methods, respectively. Dog or Cat instances can call eat () and sleep () through the live () method.
Note: Cat and Dog instances are automatically added with the live () method. The purpose of not exposing live () is to prevent live () from being overwritten by the subclass and to guarantee control of the parent class.
Class Animal {
Constructor () {
/ / this points to the instance
This.live = () = > {
This.eat ()
This.sleep ()
}
}
Eat () {
Throw new Error (template class methods must be overridden)
}
Sleep () {
Throw new Error (template class methods must be overridden)
}
}
Class Dog extends Animal {
Constructor (. Args) {
Super (. Args)
}
Eat () {
Console.log ("dogs eat food")
}
Sleep () {
Console.log ("Dog sleeps")
}
}
Class Cat extends Animal {
Constructor (. Args) {
Super (. Args)
}
Eat () {
Console.log ("Cats eat food")
}
Sleep () {
Console.log ("cat sleeps")
}
}
/ * the following is the test code * /
/ / at this point, the this in Animal points to dog
Let dog = new Dog ()
Dog.live ()
/ / at this point, the this in Animal points to cat
Let cat = new Cat ()
Cat.live ()
At this point, I believe you have a deeper understanding of "what is the js template pattern". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.