10 Design principles for Java

Source: Internet
Author: User

1. DRY (Don't repeat yourself)

This means not writing duplicate code, instead of using abstract commonalities. If you use hard-coded more than once, consider exposing it as a final-modified invariant, and if you have the same block of code in more than two places, consider making it a separate method. The benefits of solid design principles are in maintenance. It is important to note that repetition does not refer to code, but to functionality.

2. Package Changes

The only constant in the software field is "change", so encapsulate the code that you estimate might be changed in the future. The benefits of this design are easy to test and easy to maintain. If you are doing Java coding, make the variables and methods private. Several Java design patterns are encapsulated, and factory design patterns encapsulate object creation code and provide the flexibility to launch new products without changing existing code.

3. Opening and closing principle

Classes, methods, or functions should be open to extensions (new features) and closed to modifications. This is another beautiful object-oriented design principle that prevents attempts to modify code that has already been tested.

4. Single Responsibility

There should be more than one reason to modify the class, because a class can have only one function, if you add more than one function to a class, it is equivalent to introducing a tight coupling between two functions.

5. Dependency injection or reversal control

Do not ask for Reliance, as it has been provided to you by the framework. such as the spring Framework, the beauty of this design principle is that it is injected into the framework by DI injection matching required classes, it is easier to test maintenance, because the code to create the object is focused on the framework, and the client code is not involved.

6. Combination is better than inheritance

Combinatorial composition is always better than inheritance if possible. The combination has more flexibility than inheritance. Combinations allow properties to be set at run time, and by using interfaces, we can use polymorphism to change the behavior of classes as they run, providing a better interface implementation.

7. LSP Principles

According to the Richter substitution principle, a subtype must be an alternative superclass, that is, a method or function that uses the type of a superclass to be as free as the object of a subclass, and if an analogy subclass has more functionality and the subclass may not support some functionality, this does not violate the LSP. In order to follow the design principle of LSP, a derived class or subclass must be enhanced not to reduce it.

8. Interface Separation principle ISP

Interface isolation principle requires that the client should not implement an interface that it does not use. This can happen when an interface contains more than one function and the client only needs a single function, because once you release your interface, you cannot change it.

9. Interface-oriented rather than programmatic

Interface-oriented programming, rather than implementation-oriented subclasses, has the advantage of flexibility, especially when the same interface has different implementations of subclasses.

10. Principle of Delegation

Do not do all the things yourself, you can entrust to the corresponding class to complete.


10 Design principles for Java

Related Article

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.