As we all know, the basic principle of Java programming is to pursue high-cohesion and low-coupling solutions and code module design. Viewing the open source code of Apache and Sun can help you discover the practical use of other Java design principles in these codes. The Java Development Kit follows the following pattern: The Factory mode in the Borderfactory class, and the single-piece mode in the runtime class. You can learn more by Joshua Bloch's "effective Java" book. Another object-oriented design pattern that I personally prefer is the head first and the head first object Oriented analysis and design of Kathy Sierra.
Principle 1:dry (Don ' t repeat yourself)
That is, instead of writing repetitive code, use the "abstraction" class to abstract public things. If you need to use a hard-coded value multiple times, you can set it to a public constant, and if you want to use a block of code in more than two places, you can set it to a separate method. The solid design principle has the advantage of being easy to maintain, but be careful not to misuse, duplicate not for code, but for functionality. This means that even if you use common code to validate OrderID and SSN, they will not be the same. The use of common code to implement two different functions, in fact, is to approximate the two of these features are always bundled together, if OrderID changed its format, SSN verification code will be interrupted. Therefore, use this combination with caution, and do not arbitrarily bundle similar but irrelevant functions.
Principle 2: Package changes
The only constant in the field of software is "change", so encapsulate the code that you think or guess will be changed in the future. The advantage of the Oops design pattern is that it is easy to test and maintain the encapsulated code. If you use Java encoding, you can default to privatize variables and methods, and gradually increase access rights, such as from private to protected and not to public. There are several Java design patterns that also use encapsulation, such as the factory design pattern, which encapsulates "object creation," with the flexibility to introduce new code without affecting existing code.
Principle 3: Single Responsibility Principle
Classes are much more likely to be modified, so you should focus on a single function. If you put multiple functions in the same class, there is a correlation between functions, changing one of them, and possibly aborting another, then a new round of tests will be needed to avoid any possible problems.
Principle 4: Opening and closing principle
That is, open to the extension and close to the modification. This is another great design principle that prevents others from changing code that has already been tested. Theoretically, the function can be extended without modifying the original module. This is also the principle of opening and shutting.
Principle 5: Dependency Injection or inversion principle
The highlight of this design principle is that any class injected by the DI framework is easy to test and maintain with mock objects, because the object creation code is set in the framework and the client code is not confusing. There are many ways to implement dependency inversion, such as the bytecode technology used by the AOP (Aspect Oriented programming) framework like ASPECTJ, or the proxy used by the spring framework.
Principle 6: Interface Separation principle
It is better to adopt multiple interfaces that are related to a particular customer class than to adopt a common interface that covers multiple business methods. Designing the interface is tricky because once you release the interface, you can't change it without interrupting execution. Another advantage of this principle in Java is that, before any class uses an interface, the interface is not conducive to implementing all of the methods, so a single function means fewer implementations.
Principle 7: The Richter Substitution principle (LSP)
According to this principle, subclasses must be able to replace their base classes, meaning that a method or function using a base class can successfully refer to a subclass object. The LSP principle is closely related to the principle of single responsibility and the separation of interfaces, and if an analogy subclass has more functionality, it is likely that some functions will fail, which violates the LSP principle. To follow this design principle, a derived class or subclass must enhance functionality.
Principle 8: Prioritize the use of composition rather than inheritance
If possible, prioritize the use of combinations rather than inheritance. Some people may question it, but I find that the combination is much more flexible than inheritance. A combination allows you to change the behavior of a class by setting the properties of the class during run time, or by using an interface to combine a class, which provides greater flexibility and can be implemented at any time. This principle is also recommended by the effective Java.
Principle 9: Programming for interfaces rather than implementing this principle can make the code more flexible so that it can be used in any interface implementation. Therefore, it is best to use variable interface types, method return types, method parameter types, and so on in Java. "Effective Java" and "head first design pattern" are also mentioned in the book.
Principle 10: Delegation principle the most typical example of this principle is the Equals () and Hashcode () methods in Java. To compare two objects equally, we use the class itself rather than the client class to do the comparison. The benefit of this design principle is that there is no duplication of code and it is easy to modify it. In conclusion, hopefully these object-oriented design principles will help you write more flexible and better code. Theory is the first step, more important is the need for developers to use and experience in practice
10 principles of the highest realm of Java programming