Reprint Please specify source: http://blog.csdn.NET/supera_li/article/details/44940563
Effective Java Series
1.Effective Java experience, creating and destroying objects
2.Effective Java experience, generics
3.Effective Java experience, classes and interfaces
4.Effective Java experience, common approach
5.Effective Java Experience, enumerations, annotations, methods, generic design, exceptions
6.Effective Java experience, concurrent programming
This chapter describes the chapters that you read when you are doing a system refactoring, giving you great help with architecture. Sum up some dry things to share ah ah Ah ~ ~ The main application scenario is to write classes and interfaces need to pay attention to the problem. Each chapter is not necessarily updated in order. Welcome to make Bricks Supplement.
1. the class and the member's access permissions are minimized . Public staticfinal If you point to an object (an array is also an object), it is often wrong to do so, "private static final a= points to the object, with public static final pointing to A.clone () (a copy of) The private staticfinal a= points to the object, and the public static final list returns a list by Collections.unmodifiablelist (Arrays.aslist (A)). Store Array "
2. use Access methods instead of open data domains in public classes, that is, public classes should not expose mutable domains . For example, JavaBean uses the Setter/getter method to manipulate Object data fields
3. Composite is better than inheritance , the inheriting class is put into a compound class as a component, and the inheriting class is implemented through a series of forwarding methods. This composite class is then called through an external function.
4. either design for inheritance, provide documentation, or prohibit inheritance. prohibition of inheritance can be done through final, as well as privatization of the constructor. If inheritance is required, it is necessary to consider that the inheriting class cannot invoke the overridden method at any time, and can mechanically eliminate the self-use characteristics of the overridden method by duplicating a private helper method with the overridden method code. Eliminate your own features by calling the private helper method.
5. interface is better than abstract class, abstract class is designed for inheritance , abstract class evolution is much easier than interface, for example, later need to add method, abstract class can, but interface is not.
6. interfaces are used only to define types , such as constant interfaces, which are bad for interfaces. For a constant design, if a constant is closely related to a class, it is written inside the class. If you can see the wrong enumeration constants, use enumerations instead. Otherwise, these constants will be exported using a non-instantiated tool class.
7. class hierarchy is better than label class . If the code has a different method result depending on a certain flag bit, that is, the code template, then you need to consider whether you can hierarchically code and extract the template code as a method in an abstract class to do class hierarchy.
8. using a Function object to represent the policy , the function object is passed as a parameter to the method, representing the "pointer" in Java, by wrapping the function object in the form of an interface into a policy, providing a "host class" that invokes a private static member through a public static method. This static member is how this policy is implemented.
When the system architecture is encoded, will precedence be unified for the interface? Can the class use compositing? This class is bound to be inherited, the use of abstract class can it? Can other classes inherit? Can the X attribute be public before the class is written? Roughly on these general thinking directions.
"Summary" Effective Java experience, classes and interfaces