Groovy Discovery Mop 16 using interceptor to implement a simple observer model

Source: Internet
Author: User
Tags object object stub

The observer model is one of the more commonly used patterns and the basis for other common patterns, such as the MVC pattern, which is a model based on observer mode.

In the Java language, the implementation of observer mode is very simple, that is, let the observer inherit the observable class, and let the observer implement the Observer interface. These basics are used in my words--"mantis, Siskin--from an idiom to the Observer model," which is no longer described here.

If we have used observer mode in the actual code, it is possible to discover the weakness of the observer model we implement in the Java language: that is, if our observer wants to inherit a class, it will find no way to inherit it, because as an observer, it must inherit the observable class , and the Java language is not allowed to inherit more. So that makes up one of the biggest weaknesses of our observer model.

In the groovy language, of course, we can also use the above methods to implement the observer model, and the weakness of this implementation is certainly inherited.

Once again, our groovy language has a powerful mop feature, and with it we are likely to break through some of the hurdles we encounter in the Java language. This article is about how to use interceptor to implement the observer model, and how this implementation breaks the vulnerability of the observer model implemented by the Java language we talked about earlier.

In fact, the idea of the observer model is very simple: the observer in doing a certain action, to inform the Observer, I did this action, so that the observer to do the corresponding action. The key to this idea is that the observer will notify the Observer after he has done some action. After we've learned the groovy language interceptor, we know that this kind of notification can actually be done using interceptors.

This is a simple idea that we use interceptors to implement the observer model. Here is an example to illustrate how to achieve this idea in detail.

This example is about the bee pollen, corresponding to the observer mode, that is, flowers in the flowering time, notify the bees, bees can come to collect powder. The following is the implementation of this example.

The first is the Flower class:

public class Flower{


def open()
{
println 'the flower is opening...'
}

}

In our realization, the implementation of the Flower class is very simple, it just blossoms on the line, do not need to do anything else, such as in the Java language for the Observer mode of implementation, it will need to inherit the observable class, and then after the flowers, and notify the Observer-bee class.

Next look at our Bee class:

public class Bee{

def eat()
{
println "it is bee's meal time..."
}

}

Its realization is also very simple, only need to realize the action of flowers, the other do not tube. In our implementation of the observer model for the Java language, our bee class needs to implement the Observer interface.

Since it is the use of interceptor to implement the Observer mode, then we still want to implement the Interceptor interface, otherwise, how do we use interceptors?

public class BeeObserver extends Bee implements Interceptor{



public Object beforeInvoke(Object object, String methodName, Object[] arguments){
// TODO Auto-generated method stub
return null
}

public Object afterInvoke(Object object, String methodName, Object[] arguments, Object result){
// TODO Auto-generated method stub
if(methodName=='open')
{
this.eat()
}
return result
}

public boolean doInvoke(){
// TODO Auto-generated method stub
return true
}

}

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.