10 Object-oriented design principles that Java programmers should understand

Source: Internet
Author: User

Object-Oriented design principles:

Is the core of programming for Oops (object-oriented programming System, Object-oriented program design systems), but most Java programmers chase design patterns like Singleton, Decorator, and Observer, Rather than focusing on object-oriented analysis and design. Even experienced Java programmers have not heard of oops and solid design principles, they don't know the benefits of design principles, and they don't know how to program them according to these principles.

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.

Although the actual case is the best way to learn the principles or patterns of design, but through the introduction of this article, no contact with these principles or the learning phase of the Java programmer can also understand the 10 object-oriented design principles. In fact, every principle requires a lot of space to be clear, but I will try to be concise.

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: 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 4: 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 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: 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 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: 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 9: Programming for interfaces, not for implementation

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: The Principle of Delegation

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, and more importantly, the need for developers to use and experience in practice.

Tags: Java, Object-oriented reprint http://www.cnblogs.com/whyhappy/p/5538035.html

10 Object-oriented design principles that Java programmers should understand

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.