Study Notes Series 3 of objective Java

Source: Internet
Author: User

It is increasingly common to discover that this is a good book. It is a pity that Java programmers do not read this book. This chapter describes the issues related to classes and interfaces. These items are very important.

Item 12: Minimize the accessibility of classes and members

A good module design should encapsulate its internal information as much as possible, so that the coupling between modules can be minimized. Parallel Development will undoubtedly speed up development and facilitate system maintenance. In Java, the access controller is used to solve this problem.

  1. Public indicates that this class is available in any range.
  2. Protected indicates that only classes in the subclass and package can be used.
  3. Private-package (default) indicates that the package is available.
  4. Private indicates that only the class can be used

When designing a class, you should try to design it in the order of 4321. If a class is only used by another class, you should consider designing it as the internal class of this class. Generally, public classes should not have public fields, but we usually use a class to define all constants, which is allowed. However, it must be ensured that these fields are either of the basic data type or the objects to which they are referenced cannot be modified. Otherwise they may be modified. For example, data in the following definition is unreasonable, and there are no problems in the next two.
Public class con
{
Public static final int [] DATA = {1, 2, 3}; // It is bad
Public static final string Hello = "world ";
Public static final int I = 1;
}

Item 13: unmodifiable classes are more favored

Unmodifiable classes mean they will not change once they are created, such as the string class. Their design and implementation are both convenient and secure-they are thread-safe. The design class cannot be modified with the following rules:

  1. Do not provide any methods to modify objects
  2. Make sure that no method can be overwritten. You can declare it as final.
  3. All fields are designed as final
  4. All fields are designed as private
  5. Make sure that you do not have access to the modifiable components of the class.
    The disadvantage of a class that cannot be modified is that different objects must be created when different worthy classes are created. This is the case with string. Generally, a solution is to provide a help class, such as the stringbuffer class.

Item 14: combination is more worth considering than 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. 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 combine to replace inheritance and solve the problem using packaging and forwarding methods. Use the class to be extended as a private final member variable of the class. 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-.

Item 15: if inheritance is to be used, both the design and documentation must have quality assurance; otherwise, do not use it.

To avoid the problems caused by inheritance, you must provide accurate documents to describe the problems that may occur when overwriting related methods. Do not call methods that can be overwritten in the constructor, because problems may occur when the subclass overwrites the methods.
Import java. util .*;

Public class subclass extends superclass
{
Private final date;
 
Public subclass ()
{
Date = new date ();
}
 
Public void M ()
{
System. Out. println (date );
}
 
Public static void main (string [] ARGs)
{
Subclass S = new subclass ();
S. M ();
}
 
}

Class superclass
{
Public superclass ()
{
M ();
}
 
Public void M ()
{

}
}
Since Super () has been called before date is initialized, null is output for the first time instead of the current time.
Since the functions of the constructor are similar to those of clone () or serialization, it is best not to include methods that can be overwritten in readobject () and clone () methods.

Item 16: the former is preferred between the interface and the abstract class.

Both interfaces and abstract classes are used to implement polymorphism, but we should give priority to interfaces. Do you know? James said that if he wants to redesign Java, he will design all interfaces. The advantage of an abstract class is that it is easy to expand, because it is inherited, and the method can be implemented in the abstract class, but the interface does not work.

Item 17: The interface should only be used to define the type

Collection c = new xxxx (); is the most common interface. Do not use interfaces to do other things, such as the definition of constants. You should define a class that contains the public final static field.

Item 18: select the former between static and non-static internal classes

If a class is defined inside another class, it is a nested class, which can be divided into static internal class, non-static internal class, and Anonymous class.
Static member class is designed to serve the enclosing class. If there are other purposes, it should be designed as top-level class. Nonstatic member class is associated with the enclosing class instance. If you do not need to access the enclosing class instance, you should design it as static, otherwise it will waste time and space. Anonymous class declares and initializes at the same time. It can be placed anywhere in the code. Typical applications are listener and process object, such as thread.

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.