In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to write a Java tool class, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Today, I also take how to write a Java tool class as the topic, to give you a specification of how to write a tool class.
Recently, when I was working on code review, I found some minor problems for developers when writing tool classes, so I'll briefly talk about my personal understanding here.
First of all, to be clear, I did not define the specification of this tool class, but discovered it with reference to many well-known open source frameworks.
In the first case, if all the methods in the utility class are static methods, you can treat the tool class as an abstract abstract class. There is a lot of such use in the Spring framework.
Then, if there is an exception in the tool class, throw it and don't try-catch it yourself. Not to mention e.printStackTrace () after try.
There is the question of whether or not to print logs in the tool class, try not to print, and do not use some third-party logging frameworks such as log4j. Reduce the dependence on third-party class libraries.
Another situation is that the methods in the utility class are not static. Then you can define the utility class as final class, considering that the utility class should not be inherited. In privatizing its constructor, a singleton is provided.
The naming of the utility class should end with Util, such as LogUtil.
Be sure to pay attention to the following tool class.
There is no unified standard as to whether methods in utility classes should be designed to be static or non-static. Each has its own advantages, participate in most open source frameworks, static a little more.
For the design of tools, I recommend you to take a look at Guava and Apache Commons.
Like the famous Hutool, it mentions six design ideas of the tool class. The method takes precedence over object, automatic recognition is better than user definition, convenience and flexibility coexist, adaptation and compatibility, optional dependence principle, non-invasion principle.
The utility class in Hutool does not use the abstract class abstract class or final class. So I prefer the source code of Spring, Guava, Apache Commons and other frameworks.
After reading the above, do you have any further understanding of how to write a Java utility class? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.