Valid Java notes (5)

Source: Internet
Author: User

List of related Reading Notes

No. 13 Non-variability

To make a Class A non-mutable class, follow the following five rules:

① Do not provide any methods to modify the object;

② There is no way to rewrite quilt categories;

③ Make all domains final;

④ Make all the domains private;

⑤ Ensure mutex access to any variable component. (If a class points to the domain of a variable object, you must ensure that the user of the class cannot live to point to the reference of these objects, and never use the object reference provided by the customer to initialize such a domain, or return the reference of this object in any access method );

The above rules are a little more demanding than the real ones. In order to improve the performance, there are some ways to do this. For example, there is no way to make an external visible change to the object state, many non-mutable classes have one or more non-final redundant domains, caching an expensive computing result in these domains.
Non-mutable objects are essentially thread-safe and do not require synchronization. Non-mutable objects can be freely shared. You can not only share mutable objects, but also share their internal information. Non-mutable objects provide a large number of components for other objects, whether they are mutable or immutable.

The only drawback of a non-variable class is that a separate object is required for each different value. String is like this. Generally, a solution is to provide a help class, such as the stringbuffer class.
If a class cannot be made into a mutable class, you should try to limit its variability.
The constructor should create an object with full initial terms. All constraints should be established at this time. The constructor should not pass the constructor to other methods by "constructing only some instances to be viewed, A public initialization method should not be kicked out of the latter function.

There are three methods to make a class a mutable class:

① Declare a class as final;

② Make every method in this class final. The advantage of this method is that its subclass can continue to expand new methods;

③ Declare the class constructor as private or package-level private, and add a static factory method to replace the public constructor. (although this method is not commonly used, but it is the most recommended)

 

No. 14 combination takes precedence over inheritance
The most important way to implement code reuse is inheritance, but inheritance destroys encapsulation, resulting in insufficient software keys. If the subclass inherits the parent class, the method inherited from the parent class depends on the implementation of the parent class. Once it is changed, unpredictable results will occur. If the subclass and the superclass are in different packages and the superclass is not designed for expansion, inheritance will lead to the vulnerability. The author introduces the instrumentedhashset as an inverse example, because it does not understand the implementation of the parent class method. The solution provided by the author is to replace inheritance by merging, especially when an appropriate interface is used to implement a packaging class, the packaging class and the forwarding method are used to solve the problem. Use the class to be extended as a private
Final member variable. Pass the method parameter to this member variable and obtain the return value. The disadvantage is that such a class is not suitable for returning the framework. Although inheritance is good, we should not abuse it. It is used only when we can determine the relationship between them is-.

 

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.