10 Java programmers familiar with object-oriented design principles

Object oriented design principle is the core of oops programming, but most Java programmers I have seen are enthusiastic about design patterns such as singleton, decorator and observer, and do not pay enough attention to learning object-oriented analysis and design. It is important to learn the basic knowledge of object-oriented programming such as "abstraction", "encapsulation", "polymorphism" and "inheritance", but it is equally important to understand these design principles in order to create concise and modular design. I often see Java programmers with different levels of experience. Some of them don't know these oops and solid design principles, some just don't know what benefits a specific design principle will bring, and even don't know how to use these design principles in coding.

(design principle) the bottom line is always to pursue high cohesion and low coupling coding or design. Apache and sun's open source code is a good example of learning Java and oops design principles. They show us how design principles are used in Java programming. The Java JDK uses some design principles: the factory pattern in the borderfactory class, the singleton pattern in the runtime class, and Java Decorator Pattern in io class. By the way, if you are really interested in Java coding principles, please read Joshua Bloch's effective Java, who has written Java APIs. My personal favorite about object-oriented design patterns is Kathy Sierra's head first design pattern, and others about object-oriented analysis and design. These books are of great help to write better code and make full use of various object-oriented and solid design patterns.

Although the best way to learn design patterns (Principles) is to use real examples and understand the inconvenience caused by violating design principles, the purpose of this paper is to introduce object-oriented design principles to Java programmers who have not contacted or are in the learning stage. Personally, I think oops and solid design principles need articles to clearly introduce them. I will try my best to do this here, but now please prepare to browse the following design patterns (Principles):) dry C don't repeat yourself

Our first object-oriented design principle is: dry. As can be seen from the name, dry (don't repeat yourself) means not to write duplicate code, but to abstract it into reusable code blocks. If you have more than two identical code blocks, consider abstracting them into a separate method; Or if you have used hard coded values multiple times, set them to public constants. The advantage of this object-oriented design principle is easy to maintain. It is important not to abuse this principle. Repetition is not for code, but for function. It means that if you use common code to verify OrderID and SSN, it does not mean that they are the same or that they will remain unchanged in the future. By using common code to implement two different functions, or you closely connect the two different functions; When your OrderID format changes, your SSN validation code will break. So be careful of this coupling and don't combine similar code that has no relationship with each other.

Encapsulate frequently modified code

Encapsulate What Changes

In the software field, what will never change is "change", so encapsulate the code you think or suspect will be modified in the future. The advantage of this object-oriented design pattern is that it is easy to test and maintain properly encapsulated code. If you are programming in Java, please follow the following principles: the access permissions of variables and methods are set to private by default, and gradually release their access permissions, such as "private" to "protected" and "not public". Some design patterns in Java use encapsulation. Factory design pattern is an example. It encapsulates the code for creating objects and provides the following flexibility: subsequent generation of new objects does not affect the existing code.

On / off design principles

OpenClosed Design Principle

Classes, methods / functions should be open to extensions (new functions) and closed to modifications. This is another elegant solid design principle to prevent someone from modifying the code that passes the test. Ideally, if you add new features, your code will be tested, which is the goal of the on / off design principle. Incidentally, the letter "O" in solid refers to the on / off design principle.

Single responsibility principle

Single Responsibility Principle(SRP)

The single responsibility principle is another solid design principle, and the letter "s" in solid refers to it. According to SRP, there should be only one reason for a class modification, or a class should always implement a single function. If you implement multiple functions in a class in Java, there will be a coupling relationship between these functions; If you modify one of the functions, you may break the coupling relationship, so another round of testing is needed to avoid new problems.

Dependency injection / inversion principle

Dependency Injection or Inversion principle

Don't ask what benefits the dependency injection function of the framework will bring to you. The dependency injection function has been well implemented in the spring framework. The elegance of this design principle is that any class injected by the di framework is easy to test with simulated objects and easier to maintain, Because the code for creating objects is centralized in the framework and isolated from the client code. There are many ways to implement dependency injection, such as using bytecode tools, some AOP (aspect oriented programming) frameworks, such as pointcut expressions or proxies used in spring. To learn more about this solid design principle, see examples in IOC and di design patterns. The letter "d" in solid refers to this design principle.

Prefer composition to inheritance

Favor Composition over Inheritance

If possible, use composition first rather than inheritance. Some of you may argue about this, but I find composition more flexible than inheritance. Composition allows you to modify the behavior of a class by setting properties at run time, and realize the composition relationship between classes in the form of interface by using polymorphism, which provides flexibility for modifying the composition relationship. Even effective Java recommends that composition be preferred over inheritance.

Richter substitution principle

Liskov Substitution Principle LSP

According to the Richter replacement principle, where the parent class appears, it can be replaced with a subclass. For example, there should be no problem that the method or function of the parent class is replaced by a subclass object. LSP is closely related to the principle of single responsibility and interface isolation. If a parent class has more functions than its subclasses, it may not support this function, and it also violates LSP design principles. In order to follow the LSP solid design principles, derived classes or subclasses (compared with their parent classes) must be enhanced, not reduced. The letter "L" in solid refers to the LSP design principle.

Interface isolation principle

The interface isolation principle means that if the function of an interface is not required, the interface should not be implemented. Most of this happens when an interface contains multiple functions, and the implementation class only needs one of them. Interface design is a tricky job, because once an interface is published, you can't modify it, otherwise it will affect the classes that implement it. Another advantage of this design principle in Java is that the interface has a feature that any class must implement all the methods of the interface before using it, so using a single function interface means implementing fewer methods.

Programming is centered on interfaces, not implementation objects

Programming is always centered on the interface (rather than the implementation object), which will make the structure of the code flexible, and any new interface implementation object can be compatible with the existing code structure. Therefore, in Java, please use the interface for the data types of variables, method return values and method parameters. This is the suggestion of many Java programmers, as well as books such as effective Java and head first design pattern.

Agency principle

Don't expect a class to complete all functions, you can appropriately hand over some functions to the proxy class. Examples of proxy principles are the equals () and hashcode () methods in Java. In order to compare whether the contents of the two objects are the same, we let the class used for the comparison complete the comparison itself rather than their callers. The advantage of this design principle is that there is no repeated coding and it is easy to modify the behavior of the class.

summary

All the above object-oriented design principles can help you write flexible and elegant code: code structure with high cohesion and low coupling. Theory is only the first step. More importantly, we should acquire the ability to find out when to use these design principles. To find out whether we have violated any design principles and affected the flexibility of the code, but nothing in the world is perfect. We can't always use design patterns and design principles when solving problems. They are mostly used in large enterprise projects with long maintenance cycles.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>