In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about the concept of Java hook method, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The hook method originates from the template method (Template Method) pattern in the design pattern. The concept of the template method pattern is to define the skeleton of an algorithm in a method and defer some steps to subclasses. The template method enables subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm. It is mainly divided into two categories: template method and basic method, and the basic method is divided into: abstract method (Abstract Method), concrete method (Concrete Method), hook method (Hook Method).
Basic definitions of the four methods (premise: defined in an abstract class):
(1) Abstract method: declared by abstract class, implemented by concrete subclass, and identified by abstract keyword.
(2) concrete methods: declared and implemented by abstract classes, subclasses do not implement or do override operations. Its essence is a universally applicable method, which does not need to be implemented by subclasses.
(3) Hook methods: declared and implemented by abstract classes, and subclasses can optionally be extended. Usually an abstract class gives an empty hook method, that is, an extension that is not implemented. It is no different from a concrete method in code, but a conscious difference, and it is sometimes no different from an abstract method, when subclasses need to be implemented. The difference is that abstract methods must be implemented, while hook methods can not be implemented. In other words, the hook method provides an option for you to implement an abstract class, which is equivalent to providing a default configuration in advance.
(4) template method: a method is defined, which defines the basic skeleton of the whole logic.
The code is as follows:
/ * abstract class, define template method and basic method * / abstract class abstractClass {/ * concrete method, declare and implement, inherit this abstract class without implementing this method * / public void concreteMethod () {System.out.print ("this is a concrete method") } / * abstract method, identified by the abstract keyword, is only declared and not implemented. Inheriting this abstract class must implement this method * / protected abstract void abstractMethod () / * hook method, declared and implemented (either empty implementation or define related content). Subclasses that inherit this abstract class can extend the implementation or not implement * / public void hookMethod () {/ / define a default action, or empty / / System.out.print ("this hook method has a default action")} / * template method, skeleton of the entire algorithm * / public void templateMethod () {abstractMethod (); concreteMethod (); hookMethod ();}} public class childClass1 extends abstractClass {@ Override protected void abstractMethod () {System.out.print ("subclass implements abstract methods in the parent abstract class") } / * ReFactor hook method * / public void hookMethod () {/ / System.out.print ("subclasses can be extended based on the implementation of the parent hook method"); / /}} public class childClass2 {public void bond (abstractClass abstractClass) {abstractClass.templateMethod ();}} public class Test {public static void main (String [] args) {childClass2 childClass2=new childClass2 () ChildClass2.bond (new abstractClass () {/ / anonymous inner class implementation callback @ Override protected void abstractMethod () {System.out.print ("subclass implements abstract methods in the parent class abstract class");} / * * refactoring hook methods * / public void hookMethod () {/ / System.out.print ("subclasses can be extended based on the implementation of parent hook methods"; / /}})}}
The above is what the concept of Java hook method is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.