1. Minimizing the accessibility of classes and members
A. Encapsulation (data privatization, method openness)/externally available callable, stable functions
B. Accessibility should be clear
Modifier |
This class |
Same Package class |
Sub-class |
Other classes |
Public |
√ |
√ |
√ |
√ |
Protected |
√ |
√ |
√ |
|
Default |
√ |
√ |
|
|
Private |
√ |
|
|
|
C. instance domains must not be public
D. Examples
-Fieldpublictest
-Point/dimension
E. Package-level private top-level classes are used only within a class and can be used as nested classes
2. Minimizing variability (immutable objects)
A. Follow the rules
1. Do not provide any method that modifies the state of the object
2. Ensure that classes are not extended
3. All domains are final and private
4. Ensure mutually exclusive access to any mutable component.
B. Advantages
1. Thread Safety
Immutable objects are thread-safe and can be shared between threads, without the need to use special mechanisms to guarantee synchronization problems, because the value of an object cannot be changed.
2. Easy to construct, use and test
Immutable objects provide a large number of artifacts for other objects
C. Shortcomings
1. Different objects need to be provided for each of the different values. If you create a larger object, the cost is a little higher.
2. String-StringBuilder (Variable companion Class)
D. Summary
1. Do not write a corresponding set method for each Get method
E. Examples
--Complextest
3. Compounding takes precedence over inheritance
A. Causes
1. Inheritance breaks the encapsulation. Subclasses trust the implementation details of specific features in their superclass. The implementation of the superclass may change depending on the release version, and subclasses may be corrupted
stack/
B. Composite benefits
1. Do not destroy the package, the whole class and the local class loosely coupled, relatively independent of each other
2. Good scalability
3. Support dynamic combination. At run time, the whole object can select different types of local objects
4. The whole class can wrap the local class, encapsulate the interface of the local class, and provide the new interface
C. Composite disadvantages
1. The whole class cannot automatically get the same interface as the local class
2. When creating an object of a whole class, you need to create an object of all local classes
D. Summary
1. It is appropriate to use inheritance only if the subclass and parent do have subtype relationships.
2. Inheritance will inherit all the defects of the parent class.
3. Composite is have a, inheritance is a.
E. Examples
--Forwardtest
4. Either design for inheritance, provide documentation, or prohibit inheritance
A. Note the invocation of each method in the parent class, especially the method that can be overridden. Be sure to describe your own personal use in the documentation. (Interator's Remove method)
B. The class must provide the appropriate hooks so that it can enter into its internal workflow. Can be a protected method, or it can be a protected domain. (Abstractlist's RemoveRange)
C. For classes designed for inheritance, the only test method is to write subclasses. Experience has shown that three subclasses can often test an extensible class.
D. Constructors, static factory methods, and other methods that have the ability to construct objects must never invoke a method that can be overridden (example:)
e. When designing classes for inheritance, there are special difficulties with cloneable and serializable interfaces. (Readresolve should be a protected method)
F. You can also create classes that are "safe to subclass" by completely eliminating the self-use characteristics of the overridden methods in the class. (The document Description/does not call a method that can be overridden)
5. Interfaces take precedence over abstract classes
A. Advantages
1. Existing classes can be easily updated by implementing a new interface
2. Interface is the ideal choice for defining mixed types
3. Interface allows us to construct a non-hierarchical type framework
1 Public Interfacesonger{2 voidsong ();3 }4 Public Interfacesongwriter{5 Song Writesong ();6 }7PulbicInterfaceSongerwriterextendssonger,songwrite{8 voidAct ();9}
B. Shortcomings
1. In version evolution it is difficult to add functionality by adding new interface methods, but abstract classes can easily handle this kind of thing
C. Summary
1. When the ease of evolution is more important than flexibility and functionality, you should define types with abstract classes
6. Interface only for defining types
A. Constant interface mode is a bad use of the interface.
B. It causes the user to blur the meaning of the interface, and with the option to implement the interface, we cannot delete the interface if the constants are not needed. (Binary compatibility)
1 Public Interfaceearthconstants{2 DoubleEarth_radius =xxxxx.xxxxx;3 }4 5 Public Final classearthconstants{6 Privateearthconstants () {}7 Public Static Final DoubleEarth_radius =xxxxx.xxxxx;8}
7. Class hierarchy takes precedence over label classes
A. Label classes are too lengthy, error-prone, and inefficient.
B. Class-level logic is clear and easy to maintain
8. Representing a policy with a function object
A. Using object reference to implement function pointer function, you can implement the policy mode.
9. Prioritize static member classes
A. Causes
1. Each instance of a non-static member class implies that a perimeter instance of the enclosing class wants to be associated. When it is created, the association is also established, and the relationship cannot be modified.
B. Advantages
1. Inner classes include: member class, local class, anonymous class.
2. Static classes cannot access non-static members and non-static methods of external classes (whether public or private);
3. An instance of a static class does not need to instantiate an instance of the barbarian member first, and can be instantiated directly.
C. Member classes, local classes, anonymous classes
--Member class
0. Technically, there are two types of members: the inner class and the member nesting class.
1. The member inner class is the member class that is said here, the full name is the non-static member inner class
2. member nesting class that is the static nested class above
3. Methods that can invoke external classes directly
4. Define a adapter that allows an instance of an external class to be considered an instance of another unrelated class (keyset, entryset in map)
1 Public class main{2 Public class memberclass{}// member inner class, often referred to as member class 3 }
--Partial class
1. A local class can be declared wherever a local variable is declared
2. Only used in non-static environments is associated with an external class instance
3. Notice that you can have the same name and compile it, such as: External class name +$+ the same name order + local class name (Test$1aa.class/test$2aa.class/test$3aa.class)
Public classTest {{classaa{//Local class in block }} PublicTest () {classaa{//local classes in constructors }} Public Static voidMain (string[] args) {} Public voidTest () {classaa{//local class in method }}}
--Anonymous class (member anonymous class/local anonymous Class)
1. No class name, cannot use inheritance, implement interface and other features
2. Initialize only when used and connect to external class instances
3. Where any allowed expression can appear in the code
4. Must be short to ensure good reading of the program
5. Common use: Create object procedure: Thead, Runnable, static factory method internal
Public classTest {Interfacea a=NewInterfacea () {//member Anonymous Class}; Public Static voidMain (string[] args) {Interfacea a=NewInterfacea () {//local Anonymous class};//The above two are implemented by implementing an interface anonymous class, called an interface anonymous class, can also be inherited by the classTest test =NewTest () {};//inherited anonymous Classes//can also be on the parameterNewThread (NewRunnable () {@Override Public voidrun () {}}). Start ();//belonging to a local anonymous class}Private Interfaceinterfacea{}}
D. Summary
1. Member class: A class that is inside the class but does not include a name in the block, constructor, method, and so on.
2. Local class: A named class that resides within a block, constructor, or method.
3. Anonymous class: There is no name class within the class, and can be subdivided into: Member Anonymous class and local anonymous class.
"Effective Java" classes and objects