Minimize the access capability of classes and members and minimize the access capability of members
Address: http://leihuang.org/2014/11/17/minmize-accessibility/
Information Hiding
To distinguish a well-designed module from a poorly-Designed module, the most important factor is that this module is for other external modules, whether internal data and other implementation details are hidden. In other words, whether the module designer encapsulates it well.
For top-level (non-nested) classes and interfaces, there are only two access levels: package-private and public ). If you select package-level private, it is only part of the implementation of this package, not part of the API that the package provides external services. In a later version, you can modify, replace, or even delete it without worrying about hurting existing users. If you select a public one, you are obligated to always support it to maintain compatibility.
For members (domains and methods), there are four access levels:
● Private: only the top-level class of the member can be accessed.
● Protected (protected)-any class in this package and its subclass can be accessed.
● Public-accessible anywhere
The instance domain cannot be public.
If an instance domain is final or a final reference pointing to a mutable object, if you declare such a class as public. Therefore, you cannot restrict the values in the domain or force the data to remain unchanged. At the same time, you cannot do anything when the instance domain is modified, so:
Classes with public mutable fields are not thread-safe.
The static final array with public attributes is almost always incorrect. Note that this sentence has four attributes: public, static, final, and array.
// This is definitely the wrong public static final Type [] VALUES = {...}; // This should be changed to private static final Type [] PRIVATE_VALUES = {...}; public static final List VALUE = Collections. unmodifiableList (Arrays. asList (PRIVATE_VALUES); // or this write (may result in a loss of performance), provided that Type implements the clone interface, and you are not disgusted with cloning private static final Type [] PRIVATE_VALUES = {...}; public static final Type [] values () {return (Type []) PRIVATE_VALUES.clone ();}
00:48:43
Brave, Happy, Thanksgiving!