Java architecture decryption--using interfaces to transform AOP

Source: Internet
Author: User

Optimization is an endless work, on the path of AOP, we go very far, but there are a lot of work, we did not do, for example, the business of the AOP package into a container, the service part of the AOP transformation into an interface-oriented, so that the specific formal restrictions! So the optimization of AOP, one step further, is in line with our concept of interface-oriented programming, and below with me to study how to integrate the idea of the interface into the implementation of AOP.

Review:

In the previous blog, we encapsulated the AOP service in a container:



The benefit is that we can extend the service class using the configuration file, and the service class is no longer limited to specific forms and methods, and the service classes and dynamic proxies we encapsulate constitute an AOP implementation:



And this implementation, we also rely on specific containers, not conducive to our expansion, if we want to change the container to do? We are going to refactor our code, which is not what we want in programming, we can use interfaces to block out the use of specific containers, so that AOP can be customized containers, how to implement? Actually very simple, we see:

1, Class Diagram transformation


2, Public service interface:

Public interface Iproxymehds {public       void Beforebean ();    public void Afterbean ();   }
3, Public service container class:

public class Proxymehds implements IPROXYMEHDS {//The container of the object before execution of the method is private hashmap<string,object> Beforbeans;    Private hashmap<string,object> Afterbeans;    What to do before the preparation method is executed private hashmap<string,string> beformethods;            Private hashmap<string,string> aftermethods; public void Beforebean () {try{for (hashmap.entry<string, object> entry:beforBeans.entry        Set ()) {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<string, object> entry:afterBeans.ent        Ryset ()) {String Objectkey = Entry.getkey ();        Object objectvalure = Entry.getvalue (); MethodBeformehod = Objectvalure.getclass (). GetMethod (Aftermethods.get (Objectkey));    Beformehod.invoke (objectvalure);    }}catch (Exception ex) {ex.printstacktrace (); }}public hashmap<string, object> Getbeforbeans () {return beforbeans;} public void Setbeforbeans (hashmap<string, object> beforbeans) {This.beforbeans = Beforbeans;} Public hashmap<string, Object> Getafterbeans () {return afterbeans;} public void Setafterbeans (hashmap<string, object> afterbeans) {This.afterbeans = Afterbeans;} Public hashmap<string, String> Getbeformethods () {return beformethods;} public void Setbeformethods (hashmap<string, string> beformethods) {this.beformethods = Beformethods;} Public hashmap<string, String> Getaftermethods () {return aftermethods;} public void Setaftermethods (hashmap<string, string> aftermethods) {this.aftermethods = Aftermethods;}}

3, Client

The client code has not changed anything:

public class Client {public static void main (string[] args) {hashmap<string,object> Beforbeans; Hashmap<string,object> Afterbeans;    Hashmap<string,string> Beformethods;        Hashmap<string,string> Aftermethods;    Beformethods=new HashMap ();    Beforbeans=new HashMap ();    Beforbeans.put ("AspectClass1", New AspectClass1 ());       Beforbeans.put ("AspectClass2", New AspectClass2 ());    Beformethods.put ("AspectClass1", "SayHello");        Beformethods.put ("AspectClass2", "Saygoodbye");    Aftermethods=new HashMap ();    Afterbeans=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 Agent class Cglibdynamicproxy Cglib =cglibdynamicproxy.getinstance ();                Accept Cglib.setproxymehds (PROXYMEHDS);                Accept the object to be proxied greeting greeting = cglib.getproxy (Greetingimpl.class);      Executes a method of the object Greeting.sayhello ("Jack"); } }

Summarize:

We look at the changes in this way, we can not find that in our AOP optimization on the road, we walk and other blogs are not quite the same, we are here to actually do one thing, the coupling is untied, will untie the coupling of the class encapsulated into the container, the container is abstracted into an interface, such a path, is not and which ideas close? Right? Is our design pattern, in the design pattern, we abstract 23 design patterns, in fact, to understand the coupling and, and then the coupled package to run-time assembly!

Avenue to Jane, in the optimization of the road, the better the guiding ideology is to simplify, fool, like a swarm, every bee do a simple thing, a colony can complete an extremely complex social activities!


Java architecture decryption--using interfaces to transform AOP

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.