Valid Java notes (1)

Source: Internet
Author: User
List of related Reading Notes

Clarity and profile are the most important

The user of a module should never be confused by the behavior of the module (that is not clear). The module should be as small as possible, but not too small. [module ): any reusable software component, from a single method to a complex system containing multiple packages, can be a module ]. Code should be reused rather than copied. The dependency between modules should be minimized as much as possible, and errors should be detected as soon as possible, ideally at the time of compilation.

No.1
Consider replacing constructors with static factory methods

Static factory method advantages:

① Constructors have naming restrictions, while static methods have their own names, making it easier to understand.

② Do not create a new object each time the static factory method is called. This method can greatly improve the performance of a program that frequently creates the same object. It ensures that a class is a Singleton, so that the non-variable class can ensure that "no two equal instances exist ".

③ Static factory methods have greater flexibility in selecting the return type. With the static factory method, you can use different parameters to create instances of different classes when calling the method, and create non-public class objects, which encapsulates the implementation details of the class.
Disadvantages of static factory methods:

① If a class obtains an instance through a static factory method, and the class constructor is not public or protected, then the class cannot have subclasses (inherited ), the constructor of the subclass must first call the constructor of the parent class. Because the constructor of the parent class is private, even if we assume that the inheritance is successful, then the subclass does not have the permission to call the private constructor of the parent class, so it cannot be inherited.
.

② After all, it is advocated by Sun to create an instance through constructors. The static factory method is not much different from other static methods. Who knows that this static method is used to create an instance? The remedy is: the static factory method name uses valueof or getinstance.

Summary: both static factory methods and public constructors have their own purposes. We need to understand their respective strengths and avoid using constructors as soon as possible. Generally, static factory is more suitable. If there are no other factors that strongly influence our selection, we 'd better simply choose constructors. After all, they are the specifications provided by languages.

No. 2
Use a private constructor to enhance the singleton attribute

There are two methods to implement singleton:

Method 1: A Public static member is a final domain. The declaration of the member clearly states that this class is a singleton.
Public class Elvis {</P> <p> Public static final Elvis instance = new ELVIS (); </P> <p> private ELVIS () {}</P> <p> Public void leavethebuilding () {</P> <p> system. out. println ("who a baby, I'm outta here! "); </P> <p >}</P> <p> // This code wocould normally appear outside the class! </P> <p> Public static void main (string [] ARGs) {</P> <p> elvis = Elvis. instance; </P> <p> Elvis. leavethebuilding (); </P> <p >}</P> <p>} 


Method 2: provide a public static method instead of a public static final domain. This method provides greater flexibility. without changing the API, you can change this class to singleton or non-Singleton.
Public class Elvis {</P> <p> Private Static final Elvis instance = new ELVIS (); </P> <p> private ELVIS () {}</P> <p> Public static Elvis getinstance () {return instance ;}</P> <p> Public void leavethebuilding () {</P> <p> system. out. println ("who a baby, I'm outta here! "); </P> <p >}</P> <p> // This code wocould normally appear outside the class! <Br/> Public static void main (string [] ARGs) {</P> <p> elvis = Elvis. getinstance (); </P> <p> Elvis. leavethebuilding (); <br/>}< br/>}
In general, the first method is a little more efficient. Then, after the first method is used to implement Singleton, there is no room for change. When you want to change this class to non-Singleton, obviously, it cannot. So, unless you are sure that this class is a Singleton, use the first method. If the class implements the serializable interface in the 2nd method, you should overwrite the (override) readresolve () method. Otherwise, a new instance will be generated during deserialization, this is against Singleton!

No. 3
Strengthen the non-instantiation capability through private constructor
In object-oriented programming, if there are too many classes with only static attributes and static methods, the idea of object-oriented may be lost. However, it cannot be said that only static attributes and static methods should not appear in object-oriented programs. On the contrary, sometimes we must write such classes as tool classes. How can this class be implemented? Someone may define this class as an abstract class (Abstract
Class), indeed, the abstract class cannot be instantiated, but don't forget to inherit from it. When the subclass that inherits the abstract class is instantiated, by default, the constructor (Super ();) of the parent class without parameters will be called first. At this time, isn't the parent class also instantiated? In fact, we can do this by defining the constructor of this class as private, and the class will succeed if it does not call this constructor internally. The consequence is that
Final cannot be inherited by any class. To be inherited, you must provide a public or protect constructor to call the quilt class.

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.