Effective three classes and interfaces of Java reading notes

Source: Internet
Author: User

Minimizing the accessibility of classes and members

1, as far as possible to make each class or member is not accessed by the outside world.

2, the instance domain must not be common. Classes that contain public mutable domains are not thread-safe.

3. In addition to the special case of the public static final domain, the public class should not contain the public domain and ensure that the objects referenced by the public static final domain are immutable.

Ii. using access methods in public classes rather than public domains

1, if the class is a package-level private, or a private nested class, directly exposing its data field is not an essential error.

2. The public class should never expose a mutable domain.

Third, to minimize the variability of

Immutable classes are classes whose instances cannot be modified. To make the class immutable, you need to follow these five rules

1. Do not provide any method to modify the state of the object

2. Guarantee class will not be extended

3, so that all the fields become private

4, make all the fields become final

5. Ensure mutually exclusive access to any mutable component

Immutable objects are inherently thread-safe, and they do not require synchronization.

If the non-mutable class implements the Serializable interface and contains a field that points to a mutable object, you must provide an explicit ReadObject or Readresolve method.

Limit the variability of the class as much as possible, unless you have a reason to make the domain non-final, or you want each domain to be final.

Iv. compounding takes precedence over inheritance

1, inheritance has broken the encapsulation of

2, only when the subclass is really a subclass of superclass new, is and with inheritance.

3. Using inheritance propagates all the flaws in the superclass API into subclasses, while compounding allows new APIs to be designed to hide these flaws.

V. Either design for inheritance, provide documentation, or prohibit inheritance

1, pay attention to the mutual calls among the methods in the parent class, especially the methods that can be overridden. Be sure to describe your own personal use in the documentation.

2. The class must provide the appropriate hooks so that it can enter into its internal workflow. Can be a protected method, or it can be a protected domain.

3. For classes designed for inheritance, the only test method is to write subclasses. Experience has shown that three subclasses can often test an extensible class.

4. Constructors, static factory methods, and other methods that have the ability to construct objects must never invoke methods that can be overridden.

5, in order to inherit and design the class, cloneable and Serializable interface has encountered special difficulties.

6. You can also create classes that are "safe to subclass" by completely eliminating the self-use characteristics of the overridden methods in the class.

Vi. interface is better than abstract class

Abstract classes are inherently classes, and accept classes can only be inherited by a single constraint. This is the biggest difference between it and the interface. The advantages of the interface are:

1. The existing classes can be easily updated to implement the new interface.

2, interface is the ideal choice to define mixin (mixed type)

3. Interface allows us to construct a type frame of non-hierarchical structure

4, the interface can safely enhance the function of the class.

5, by providing an abstract skeleton implementation for the important interface, the advantages of the interface and abstract class together.

Abstract classes that define types that allow multiple implementations have a distinct advantage over interfaces: The evolution of an abstract class is much easier than the evolution of an interface.

VII. interfaces for defining types only

1, the constant interface mode is the bad use of the interface.

The class hierarchy is better than the label class.

1, Label class is too lengthy, error prone, and inefficient.

Ix. using a Function object to represent a policy

1, the use of object reference to implement function pointer function, you can implement the policy mode.

X. Prioritize static member classes

1. Each instance of a non-static member class implies that a perimeter instance of the enclosing class wants to be associated. When it is created, the association is also established, and the relationship cannot be modified.

  

Effective three classes and interfaces of Java reading notes

Related Article

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.