Minimize the access capability of classes and members and minimize the access capability of members

Source: Internet
Author: User

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!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.