"Effective Java" reading notes-minimizing the variability of classes

Source: Internet
Author: User

Item 15 The variability of the minimized class

Effective Java

How do I make a class immutable?
    1. Does not provide a way to change the state of an object. (mutators)
    2. Ensure that the class is not inherited, that is, the class is qualified with final.
    3. Make all fields (field) final.
    4. Make all the domains private.
    5. Ensure all mutually exclusive access to the mutable component (ensure exclusive access to all mutable components)

Example:

 Public Final classcomplex{//final class    Private Final DoubleRe//private final Field    Private Final DoubleIm Public Complex(DoubleReDoubleIM) { This.Re= RE; This.im= IM; } Public Double Realpart(){returnRe;} Public Double Imaginarypart(){returnim;} PublicComplexAdd(Complex c) {return New Complex(Re+c.Re, Im+c.im);//return New Object instead of modify itself}//other Arithmetic method ommit ...}
Why use immutable objects (immutable objects)?
    1. Immutable objects are simple. There is no need to consider whether an object will behave strangely in change. Immutable objects are always valid as long as there is a limit in the construction method.
    2. Immutable objects are inherently thread-safe and do not require synchronization (synchronization). Immutable objects are encouraged to share without worrying about security. For example, the following:

      publicstaticfinalnewComplex(0,0);
    3. The interior of an immutable object is also encouraged to be shared.
    4. Immutable objects are excellent as artifacts of other complex objects (building blocks). Its invariable nature reduces the difficulty of designing complex objects.

Disadvantages of immutable objects and optimization
    1. The only disadvantage of immutable objects is that each operation requires the creation of new objects that cause performance problems in multi-step operations.
    2. Optimization:
      1. Use the variable "adjoint class" (mutable "companion class") in multi-step operations. For example, the string class and the string buffer class.
      2. predict which multi-step operations are often used, and provide an official method for optimizing these operations.
Best practices
    1. Provides public static factory methods (static Factory method) and makes the constructor private.
      1. You can detect the validity of input data in advance, and avoid creating objects directly if they are not legal.
      2. You can cache some of the commonly used objects to optimize performance.
      3. You can add some useful features.
    2. The conditions that make immutable classes can be weakened to any method that does not result in changes in the external representation of the object . This allows us to create some internal mutable components for caching, which optimizes performance.
Summarize
    1. When designing a class, consider first whether you can make it immutable.
    2. If a class cannot be made completely immutable, its variability is limited to a minimum.
    3. Do not provide any initialization methods outside of the static factory method and the constructor.

"Effective Java" reading notes-minimizing the variability of classes

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.