[java basic] the principle of interface-oriented (AOP)

AOP principle:

The programming idea that dynamically cuts the code into the specified method and position of the class is aspect-oriented programming.

Scenes:

Aspect-Oriented Programming (AOP is the acronym for Aspect Oriented Program), we know that object-oriented features are inheritance, polymorphism and encapsulation. Encapsulation requires that functions be distributed into different objects, which is often called responsibility assignment in software design. In fact, that means having different classes design different methods. In this way, the code is scattered into the classes one by one. The advantage of this is to reduce the complexity of the code and make the class reusable.
But people have also found that while decentralizing the code, it also increases the duplication of the code. What does that mean? For example, we may need to do logging in each method in two classes. According to the object-oriented design method, we must add the log content to the methods of both classes. Maybe they are exactly the same, but it is because of object-oriented design that there is no connection between classes, and it is impossible to unify these duplicate codes.
Some people may say, that's easy to handle, we can write this code in a separate method of a separate class, and then call it in these two classes. However, in this way, these two classes are coupled with the separate classes we mentioned above, and its changes will affect both classes. So, is there any way that we can add code at will when we need it? This kind of programming idea that dynamically cuts the code into the specified method and position of the class at runtime is aspect-oriented programming.
Generally speaking, the code fragments that we cut into the specified methods of the specified classes are called aspects, and which classes and methods we cut into are called entry points. With AOP, we can extract the code shared by several classes into a slice, and then cut into the object when needed, thereby changing its original behavior.
In this way, AOP is actually just a supplement to OOP. OOP distinguishes each class from the horizontal, while AOP adds specific code to the object vertically. With AOP, OOP becomes three-dimensional. If the time dimension is added, AOP makes OOP change from two-dimensional to three-dimensional, and from plane to three-dimensional. Technically, AOP is basically implemented through a proxy mechanism.
AOP can be said to be a milestone in the history of programming, and it is a very useful supplement to OOP programming.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324528226&siteId=291194637