AOP and Decorator learning experience

Source: Internet
Author: User
Experience recently this period of time are busy learning  springframework, that Dongdong good, just just learned struts,hibernate, have come to this, hehe, feel very tired. "How do you pull these two irrelevant things together?" "&NBSP, indeed!"   No matter from their design, implementation, use, etc. they are irrelevant,  but I think they are a bit similar in terms of understanding. Besides, we should learn from the comparative school. This will help the beginner to understand.       Important statement: I have written in my own language, there is no professional terminology and please point out. : -       said that the design pattern of  decorator, that is, decorative mode, which is not understood? To decorate the original thing is to decorate it. Since it is a decoration, you have to have the subject (the modified thing), but also have decorations. But no matter what you decorate to decorate, his essence is invariable. It's like a man with a mask, but he's still human. The wall was painted, but it was still a wall. You may think this is nonsense.   But it's important to understand this (this is the difference from the strategy of the Strategy model) and I think it's important to understand the  decorator model. In development you often want to enhance the function of a class, or to undo some of the functions of a class, but a class to decorate a lot of functions, perhaps the original class has been unrecognizable, you will often feel dazed, in fact, you mainly grasp his subject, Keep in mind that the job you're doing now is to dress up for the subject.       Research Jive Know,  inside  Filter  is using the decorator  design pattern, then in this design, its main body is what?  FORUMMESSAGE&NBSP, is undoubtedly  ForumMessage . Decorations Of course is filter , so we do this thing is always forummessage as the core of the,filter  to decorate the time, you must not forget that you are now working for Forummessage! For an article on Jive Research, a whole bunch of online, now look at a simpler  decorator: //such as an abstract widget: Public interface compOnent {   void operation ();} A specific component:public class concretecomponent implements component {    Public void operation ()  { //write your code here   }}// An abstract decorative:public class decorator implements component {   private  component component;  //Reference ...  .... .... ...   //business method, delegated to component     public void operation ()  {     component.operation ();    }}//a specific decoration: public class concretedecorator extends decorator {  /**     *  Business Method     */  public void operation () {// Some specific decorative code      ......      super.operation ();  //   Main body//some specific decorative code  .......  }  }......      we mainly look at the concretedecorator  in the operation () method, his parent class is delegated to the component build to complete, in fact it is the main, some decorative code will be in this method (main) before the implementation of " Preprocessing "or" finishing "after execution.       No, it is easy to see from above that the operation () method in the abstract decoration does not cover the "overall" in the concrete decoration class, because he uses super.operation ();   such a statement. In any case, you will find a way to do this in the decorative mode. Stating that his subject is still in. (This is the difference from the Strategy model strategy)       said so much, I don't know if you feel the operation () method in the Concretedecorator class with spring The Invoke () method of  InvocationHandler  interface in  AOP  is somewhat similar.       Let's take a look at the Invoke () method to rewrite when  InvocationHandler  is to be implemented:  public object  invoke (Object proxy, method method, object[] args)    throws  throwable {   .........................result = method.invoke (OriginalObject, args);   ...................................  return result;} The      InvocationHandler.invoke  method will be triggered before the method of the proxy class is invoked. In this way, we can do some processing before and after the invocation of the proxy class method, as shown in the code above, passing the currently invoked method in the parameters of the Invocationhandler.invoke method toAnd the parameters of the method being invoked. At the same time, we can use the Method.invoke method to invoke the original method of the proxy class implementation. In this way, we can make a big fuss before and after the method invocation of the proxy class. (Note: Reference to a section of the spring  Development Guide)       in the back to decorate mode, its method operation ()   before and after execution, can also make a fuss about it. So it seems, spring aop  and  decorator, from the understanding level, indeed a bit like, note: Just understand the level,  and their realization completely two horse thing!       in fact, the main thing is how we look at this problem, according to my understanding,spring aop  can also be said to be a broad sense of decoration, but it is not decorative mode. It is also a restriction on a method, such as insert ()  , you have to do it before you open the transaction and commit or rollback such "decoration."   For example, you can also work with a person to do the resources after the log such as the decoration and so on. They have the same point.      AOP  is not something new, of course, if your AOP is a container (jboss 4.0) Implementation,  then according to the above, some of your methods will be the container " Decoration. "       so, more times can be understood as  Spring AOP  and some aop  containers are in system-level, container-level "decorations".   Decoration mode is a business-class decoration.   This is actually easy to understand from the client.       if it is aop&nbsp, programmers can safely write your code (even a bit stupid and not afraid, with AOP protection).   Because the system will "decorate" the code while the system is running. In other words, these decorations do not appear in the client's code, but decorator mode? Oh, this time the programmer to a little head, they want to assemble some decorative class, according to a certain mechanism of the main decoration. In other words, these cosmetic classes appear in the client's code. Look at the code to know  :--)  inputstreamreader  iNput = new inputstreamreader (system.in); Bufferedreader br = new bufferedreader (input);./the subject is system.in.

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.