10 object-oriented design principles that Java programmers should understand

Source: Internet
Author: User

The object-oriented design principle is the core of OOPS (object-oriented programming system) programming.
Design patterns such as Singleton, decorator, and observer do not focus on Object-Oriented Analysis and Design. There are even experienced
Java programmers have never heard of OOPS and solid design principles. They do not know the benefits of design principles, nor how to program based on these principles.

As we all know, the most 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 helps you discover the practical use of other Java design principles in these codes. Java Development Kit follows the following pattern: the factory pattern in the borderfactory class and the Singleton pattern in the runtime class. You can use
Java to learn more. Another object-oriented design model that I personally prefer is the head
First design mode and head
First Object Oriented Analysis and Design.

Although the actual case is the best way to learn design principles or models, java programmers who have never touched on these principles or are still learning can also understand these 10 object-oriented design principles. In fact, each principle requires a lot of space for clarity, but I will try my best to make it concise.

Principle 1: dry (don't repeat yourself)

That is to say, do not write repeated code, but use the "invalid action" class to abstract public things. If you need to use a hard-coded value multiple times, you can set it to a public constant. If you want to use a code block in more than two places, you can set it as an independent method. The advantage of the solid design principle is that it is easy to maintain, but do not abuse it. Duplicate is not for code, but for functions. This means that the public code is used to verify orderid and SSN, and the two will not be the same. Using public code to implement two different functions is essentially bundling these two functions together forever. If orderid changes its format, the SSN verification code will also be interrupted. Therefore, use this combination with caution. do not bind similar but unrelated functions at will.

Principle 2: encapsulation changes

The only thing that remains unchanged in the software field is "change", so it encapsulates the code that you think or guess will change in the future. The advantage of the oops design pattern is that it is easy to test and maintain the code for conversion. If you use Java encoding, You Can privatize variables and methods by default, and gradually increase access permissions, such as from private to protected and not public. Several Java design patterns also use encapsulation. For example, the factory design mode encapsulates "Object creation", and its flexibility makes it possible to introduce new code without affecting the existing code.

Principle 3: Open and Close principles

That is, it is open to the extension and closed to the modification. This is another great design principle to prevent others from modifying the tested code. Theoretically, functions can be extended without modifying the original modules. This is also the purpose of the principle of openness and closure.

Principle 4: single Responsibility Principle

Classes are highly likely to be modified, so you should focus on a single function. If you place multiple functions in the same class, the functions are associated. If you change one function, you may abort another function, in this case, a new round of testing is required to avoid possible problems.

Principle 5: dependency injection or inversion principle

The highlight of this design principle is that any class injected by the di framework can easily be tested and maintained using the mock object, because the client code is not chaotic when the object creation code is concentrated in the framework. There are many ways to implement Dependency inversion, such as the bytecode technology used by the aspectj (Aspect Oriented Programming) framework, or the proxy used by the Spring framework.

Principle 6: give priority to combinations rather than inheritance

If possible, use combinations instead of inheritance. Some may question, but I find that combinations are much more flexible than inheritance. Combination allows you to change the behavior of a class by setting the class attributes during running, or you can use interfaces to combine a class. It provides higher flexibility and can be implemented at any time. This principle is also recommended in objective Java.

Principle 7: LSP)

According to this principle, subclasses must be able to replace their base classes, that is, using methods or functions of the base classes can smoothly reference subclass objects. The LSP principle is closely related to the single responsibility principle and the interface separation principle. If an analogy subclass has more functions, it is likely that some functions will become invalid, which violates the LSP principle. To comply with this design principle, the function of a derived class or subclass must be enhanced.

Principle 8: interface separation principle

Using multiple interfaces related to a specific customer class is better than using a common interface that covers multiple business methods. Designing an interface is tricky, because once an interface is released, you cannot change it without interrupting the execution. In Java, another advantage of this principle is that interfaces are not conducive to implementing all methods before any class uses interfaces, so a single function means less implementation methods.

Principle 9: Interface Programming, not implementation programming

This principle can make the code more flexible, so that it can be used in any interface implementation. Therefore, it is best to use the variable interface type, method return type, method parameter type, and so on in Java. Impact
Java and head
First design pattern is also mentioned in the book.

Principle 10: Delegation Principle

The most typical examples of this principle are the equals () and hashcode () methods in Java. To compare two objects equally, we use the class itself rather than the client class for comparison. The benefit of this design principle is that there is no repeated code and it is easy to modify it.

In short, we hope these object-oriented design principles can help you write more flexible and better code. Theory is the first step. More importantly, developers need to use and understand it in practice.

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.