Second, Java object-oriented (12) _ tool class design

2018-05-03

 

tool design

 

Tool class: A class that stores a certain class of things and stores tool methods.

  Name: XxxUtil, XxxUtils, XxxTool, XxxHelper, XxxHelpers, etc. For example: JdbcUtil

Tool class storage package: storage tool class

  Naming: util, utils, tool, helper, helpers, etc.

 

How are the tool classes designed?

  Tools only need to exist in one copy during development

  1) If the tool class is not decorated with static, it means that the tool method should be called by the object of the tool class.

    At this point, the tool class is designed as a singleton pattern.

  2) If the tool class is decorated with static, it means that the tool method only needs to be called with the class name of the tool class.

    At this time, the constructor of the tool class must be privatized (to prevent the object of the tool class from calling static methods).

The second is generally preferred, simple, and the tool classes provided in the JDK are the second. Such as java.Util.Arrays class .

 

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297943&siteId=291194637