Java architecture decryption-using interfaces to transform AOP

Source: Internet
Author: User

Java architecture decryption-using interfaces to transform AOP

Optimization is an endless task. On the way to AOP, we have gone a long way, but there is still a lot of work we have not done. For example, we have encapsulated the business part of aop into a container, transform the service part of aop into interface-oriented, so that it is not subject to the specific form! In this way, the optimization of AOP is a step forward, which is also in line with our idea of interface-oriented programming. Next we will study with me how to integrate the idea of interfaces into the implementation of aop.

Review:

In the previous blog, We encapsulated the aop service into a container:

 

The advantage is that we can use the configuration file to expand the service class. The service class is no longer limited by the specific form and method, but the encapsulated service class and dynamic proxy constitute such an AOP implementation:

 

This implementation depends on specific containers, which is not conducive to our expansion. What should we do if we want to change the container? We need to refactor our code. This is not what we want in program design. We can use interfaces to block the use of specific containers, so that aop can customize containers. How can we achieve this? It's actually very simple. You can see:

1. class chart Transformation

2. Public service interface:

 

public interface IProxyMehds {       public void beforeBean();    public void afterBean();   }
3. Public service containers:

 

 

Public class ProxyMehds implements IProxyMehds {// container private HashMap of the object before the method is executed
 
  
BeforBeans; private HashMap
  
   
AfterBeans; // methods to be executed before execution of the preparation method private HashMap
   
    
BeforMethods; private HashMap
    
     
AfterMethods; public void beforeBean () {try {for (HashMap. Entry
     
      
Entry: beforBeans. entrySet () {String objectKey = entry. getKey (); Object objectValure = entry. getValue (); Method beforMehod = objectValure. getClass (). getMethod (beforMethods. get (objectKey); beforMehod. invoke (objectValure) ;}} catch (Exception ex) {ex. printStackTrace ();} // Method sAge = c. getMethod ("setAge");} public void afterBean () {try {for (HashMap. entry
      
        Entry: afterBeans. entrySet () {String objectKey = entry. getKey (); Object objectValure = entry. getValue (); Method beforMehod = objectValure. getClass (). getMethod (afterMethods. get (objectKey); beforMehod. invoke (objectValure) ;}} catch (Exception ex) {ex. printStackTrace () ;}} public HashMap
       
         GetBeforBeans () {return beforBeans;} public void setBeforBeans (HashMap
        
          BeforBeans) {this. beforBeans = beforBeans;} public HashMap
         
           GetAfterBeans () {return afterBeans;} public void setAfterBeans (HashMap
          
            AfterBeans) {this. afterBeans = afterBeans;} public HashMap
           
             GetBeforMethods () {return beforMethods;} public void setBeforMethods (HashMap
            
              BeforMethods) {this. beforMethods = beforMethods;} public HashMap
             
               GetAfterMethods () {return afterMethods;} public void setAfterMethods (HashMap
              
                AfterMethods) {this. afterMethods = afterMethods ;}}
              
             
            
           
          
         
        
       
      
     
    
   
  
 

3. Client

 

The client code has not changed:

 

Public class Client {public static void main (String [] args) {HashMap
 
  
BeforBeans; HashMap
  
   
AfterBeans; HashMap
   
    
BeforMethods; HashMap
    
     
AfterMethods; beforMethods = new HashMap (); beforBeans. put ("AspectClass1", new AspectClass1 (); beforBeans. put ("AspectClass2", new AspectClass2 (); beforMethods. put ("AspectClass1", "SayHello"); beforMethods. put ("AspectClass2", "SayGoodBye"); afterMethods = new HashMap (); afterBeans. put ("AspectClass3", new AspectClass3 (); afterBeans. put ("AspectClass4", new AspectClass4 (); afterMethods. put ("AspectClass3", "SayHi"); afterMethods. put ("AspectClass4", "Eat"); ProxyMehds proxyMehds = new ProxyMehds (); proxyMehds. setBeforBeans (beforBeans); proxyMehds. setBeforMethods (beforMethods); proxyMehds. setAfterBeans (afterBeans); proxyMehds. setAfterMethods (afterMethods); // instance proxy class CGLibDynamicProxy cglib = CGLibDynamicProxy. getInstance (); // accept cglib. setProxyMehds (proxyMehds); // accept the Greeting greeting = cglib. getProxy (GreetingImpl. class); // execute a method greeting of the object. sayHello ("Jack ");}}
    
   
  
 

Summary:

 

Looking at the changes, we can hardly find that we are on the road to AOP optimization, and we are not very similar to other blogs. Here we actually did one thing, undo coupling, encapsulate the uncoupled class into the container, and abstract the container into an interface. Which of the following concepts is approaching? Right? It is our design model. In the design model, we abstract 23 design models to decouple and encapsulate coupling into the running Fashion configuration!

The greatest truths are simple. On the road to optimization, a good guiding ideology is to simplify and become foolish. Just like a bee colony, every bee does a simple thing, A bee colony can complete an extremely complex social activity!

 

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.