Article 12: Minimize the accessibility of classes and members

Source: Internet
Author: User

Concept:
The most important factor between a well-designed module and a poorly-Designed module is whether the module hides its internal data and other details from other external modules.
A well-designed module hides all implementation details and clearly separates its API from its implementation. Then, modules communicate with each other through their APIs. One module does not need to know the internal conditions of other modules.
Information Hiding or encapsulation is one of the basic principles of software design.

Advantages:
Information Hiding is very important for many reasons, most of which are due to the fact that:
It can effectively remove the coupling relationship between modules of the system. Yes, these modules can be independently developed, tested, optimized, used, understood, and modified.
This will speed up system development because these modules can be developed concurrently.
It also reduces the maintenance burden, becauseProgramDevelopers can understand these modules more quickly and do not affect other modules when debugging them.
Although information hiding itself does not bring better performance both internally and externally, it can effectively adjust the performance:
Once a system is completed and the system performance is affected by analyzing which modules can be further optimized without affecting the correctness of other modules.
Information Hiding improves software because modules are not closely linked. Apart from the bad environment used for developing these modules, they are often useful in other bad environments.
Finally, information hiding reduces the risk of building a large system, because even if the entire system is unavailable, these independent modules may be available.

Java programming provides many mechanisms (facility) to help hide information.
Access Control determines the accessibility of classes, interfaces, and members ).

For top-level (non-nested) classes and interfaces, there are only two possible access mechanisms: Package-private and public ).
If it is private, you can replace, modify, or delete it without worrying about affecting the existing client program.
However, if you set it to common, you have the responsibility to always support it to ensure their compatibility.

Then, reducing the accessibility of unnecessary public classes is more important than reducing the package-level private top-level classes:
The public class is part of the package API, and the package-level private top-level class is already part of the implementation of this package.

There are four possible access levels for members (domains, methods, nested classes, and nested interfaces:
PRIVATE: only the Member can be accessed within the top-level class of the member.
Package-PRIVATE: declares that any class in the package of this Member can access this member. Technically speaking, it is also called "default access level ".
Protected (protected)-it can be accessed by a subclass that is more private than the package.
Public -- this member can be accessed anywhere.

The instance domain must not be public. If the domain is non-final, or a final reference pointing to a mutable object, once the domain becomes common, it gives up its ability to limit the value stored in this domain.
Classes that contain a common mutable domain are NOT thread-safe.

Class has a common static final array domain, or returns the access method of this domain, which is almost always wrong.

You can convert a shared array to private and return a public unchangeable list.

Private Static final thing [] private_values = {...}; public static final list <ting> values = collecations. unmodifiablelist (arrays. aslist (private_values); or: Private Static final thing [] private_values = {...}; public static final thing [] values () {return private_values.clone;} Demo: Package CN. partner4java. test; import Java. util. using list; import Java. util. list; public class listtest {private list <string> names = new names list <string> (); public list <string> getnames () {return names ;} public void setnames (list <string> names) {This. names = names;} public list <string> getclonenames () {list <string> namesclone = new canonical list <string> (); namesclone. addall (names); Return namesclone;} public static void main (string [] ARGs) {listtest = new listtest (); listtest. getnames (). add ("Wang"); listtest. getnames (). add ("Chang"); listtest. getnames (). add ("long"); List <string> namesg = listtest. getnames (); List <string> namescg = listtest. getclonenames (); namesg. set (2, "Ming"); system. out. println (listtest. getnames (); system. out. println (namesg); system. out. println (namescg); // background print: // [Wang, Chang, Ming] // [Wang, Chang, Ming] // [Wang, Chang, long]}

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.