Software Architect-AOP

Source: Internet
Author: User
Software Architect-AOP

To be a qualified software architect, AOP is a technology that must be known. So what is AOP? This is what we have discussed today (it is also my recent learning summary, and I hope you can give more advice ).

AOP, full nameAspect Oriented Programming, Chinese nameAspect-Oriented Programming, Also calledAspect-Oriented Programming.

In the actual project development process, we often notice that some modules/functions, such as permissions and caches, need to exist in various business modules of the software, these modules/functions have nothing to do with the business module. Even when designing the business module, we do not have to consider the existence of these modules/functions, but we discovered these functions during the development process.Modules/functions will bring us endless troubles. Because the starting point of the traditional OOP method is to solve the problem itself and extend itIn this case, the traditional OOP method is difficult to solve. However, when analyzing business modules and such modules, we will find that they are essentially the same, but they solve different problems and focus on a software, as shown in:

Based on your experience, we can see from the figure that the position of a log, security, transaction, and other modules in a software project is the same as that of AOP. AOPThe goal is to decouple these "cross-cutting concerns" from business modules to improve software stability and scalability.

AOP generally includes the following main concepts:

      1. Aspect (aspect): modularization of a focus, which may cause cross-cutting of multiple objects. Transaction Management is a good example of horizontal attention in J2EE applications.
      2. Join point (joinpoint ):ProgramA specific point in execution, such as method calling or a specific exception, is thrown.
      3. Advice: The action performed by the AOP framework at a specific connection point. Various types of notifications include "around", "before", and "throws. The notification type is discussed below. Many AOP frameworks use the interceptor as the notification model to maintain an interceptor chain around the connection points.
      4. Pointcut: Specifies a set of connection points that a notification will be triggered. The AOP framework must allow developers to specify the entry point, for example, using a regular expression.
      5. Introduction: add methods or fields to the notification class.
      6. Interface (ismodified) to simplify the cache.
      7. Target object: The object that contains the connection point. It is also used to reference notification or proxy objects.
      8. AOP Proxy: the object created by the AOP framework, including notifications.
      9. Weaving: Creates notification objects for assembly. This can be completed during compilation (for example, using the aspectj compiler) or at runtime. Spring and some other pure Java AOP frameworks are woven at runtime.

      AOP notification types include:

          1. Und und notification: notifications that enclose a connection point, such as method calls. This is the most powerful notification. The aroud notification completes the custom action before and after the method call. They are responsible for choosing to continue executing the connection point or directly returning their own return values or throwing an exception to short-circuit execution.
          2. Before notification: a notification that is executed before a connection point, but this notification cannot prevent the process from continuing to the connection point (unless it throws an exception ).
          3. Throws notification: the notification that is executed when the method throws an exception.
          4. After returning notification: the notification executed after the connection point is normal. For example, if a method returns normally, no exception is thrown.
          5. Around und notification is the most common notification type. Most Interceptor-based AOP frameworks such as Nanning and jboss4 only provide around-und notifications.

          Usually, AOP focuses on the following aspects:

              1. Permission (authentication)
              2. Cache)
              3. Context passing)
              4. Error Handling)
              5. Lazy Loading)
              6. Debug)
              7. Log)
              8. Tracking, optimization, calibration (tracing, profiling and monitoring)
              9. Performance Optimization)
              10. Persistence)
              11. Resource Pool)
              12. Synchronization)
              13. Transactions)

              I understand the application scenarios of AOP. The following uses permissions as an example to compare the implementation of traditional methods and AOP methods, and describe the application of AOP technology.

              Traditional method:

               
              Businemclassicspermission () {If (TAG = "pass") {return New businemedia ();} Throw new exception ("You are not authorized to operate businemedia .");}

              This is a common oo practice and brings about the following problems:

              1. Business Logic redundancy: The permission verification process is not part of the business logic execution. This operation belongs to the system. However, in this case, we had to mix the system permission verification process with the business logic execution process, resulting in confusion.

              2,CodeRedundancy: to use this method, all business logic code must contain the permission Verification Code, so that the same verification code is flooded throughout the software, resulting in code redundancy.

              3. Tight coupling: using this method may affect other components when the business or permissions change, resulting in coupling. All of the following problems come from this.

              4. Not easy to expand: and so on. If you want to add other horizontal modules, code redundancy and coupling will occur, and at the end of the process, it may even be unmanageable;

              5. Inflexible: using this method, any horizontal change may lead to code modification;

              However, we useAOPThe above problems can be easily solved as follows::

               
              [Permissionvalidate] class businease {}

              The above is the implementation method of AOP. It can be seen that the business has almost no relationship with permissions. the redundancy and coupling problems mentioned above can be solved.

              We can see from the above description that the AOP method solves the horizontal problem, so how is it implemented? The answer is proxy. Agents are transparent to the customer and the business. They only include the control, which can be flexibly changed based on the needs without affecting the customer and business. The basic principle of proxy is as follows:

              As shown in the figure, AOP focuses on aproxy, which places horizontal businesses and changes in the middle of customers and businesses without affecting them.

               

              To sum up, AOP is an idea to solve horizontal problems (Public modules) in software, which provides an effective method for software architects to solve such problems.

               

              Note: Some content in this article is collected during my learning.

              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.