[Reading notes] Effective Java Chapter Fourth

Source: Internet
Author: User

minimizing the accessibility of classes and membersThe rule is simple: make every class or member non-accessible to the outside world as much as possible. The instance domain (non-final) must never be public. When an immutable instance needs to be exposed, the instance is often made immutable or turned into a private instance, providing a backup of that instance. using access methods instead of public domains in public classesThis is often called getter and setter methods that provide the necessary methods for out-of-package access, limiting the behavior of the client so that the presentation can be changed internally in the future. minimization of variabilityImmutable analogy mutable classes are easier to design, implement, and use. They are not error-prone and more secure. In order for the class to become immutable, Follow these five rules: 1. Do not provide any method that modifies the state of the object; 2. Ensure that the class is not extended (prevents subclasses from changing the state of the class, generally making the class final) 3. Make all fields final 4. Make all domains private 5. Ensure mutually exclusive access to any mutable component Immutable objects are inherently thread-safe, and they do not require synchronization. compound takes precedence over inheritanceBecause the superclass will be modified in a future new release, if the subclass conflicts with the superclass at this point, it cannot be compiled, and here we will introduce the concept of compounding. Composite is both the need to use the superclass encapsulated into a new wrapper class, the members of the wrapper class contains the need to use the superclass, so that the wrapper class retains the functionality of the superclass, but also provides the wrapper class successor inheritance extensibility. either design for inheritance, provide documentation, or disallow inheritanceThis is only to illustrate that inheritance should have a strict functional description, the document describes what the superclass has done, requires subclasses to override the principle that the superclass needs to follow, and if the class written is not considered for subsequent inheritance, then the class should not be allowed to be inherited. interface is better than abstract classBecause Java only allows single inheritance, abstract classes are greatly constrained as type definitions. To implement a type defined by an abstract class, a class must be a subclass of an abstract class, with many limitations on flexibility. While interfaces are different, existing classes can easily be updated by implementing new interfaces. interface is only used to define typesWhen a class implements an interface, the interface acts as a type that can reference instances of the class. Therefore, the class implements the interface, which indicates that the client can implement certain actions on instances of this class. It is inappropriate to define an interface for any other purpose. The constant interface pattern is a bad use of the interface. class hierarchy is better than label classThe core idea is to break the lengthy superclass into a more readable multilevel sub-class implementation, preserving the code domain that each specific implementation class needs to use for its own characteristics. To represent a policy with a function objectIn Java, where there is a similar set of sequencer, the Sequencer object only represents the strategy of sorting, such instances of the class is functionally not different, mutual equivalence, suitable as a singleton, saving unnecessary object creation overhead. prioritize static member classesStatic member classes are the simplest kind of nested classes. If the member class does not require access to the perimeter instance, always place the static modifier in its declaration, making it a static member class, not a non-static member class. If the static modifier is omitted, each instance will contain an additional reference to the perimeter object. Saving this reference consumes time and space and can cause the perimeter instance to remain in compliance with garbage collection.

[Reading notes] Effective Java Chapter Fourth

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.