The difference between interceptors and decorators

Are there any differences between hooks and decorators in Java? Strictly speaking, can I implement things with decorators that are impossible with interceptors and vice versa?

Except for the problem with which I would have to examine the method name to add the method behavior in the interceptor:

interceptor:

@Nice @Interceptor public class NiceGreeterInterceptor { @AroundInvoke public Object decorate(InvocationContext ic) throws Exception { Method method = ic.getMethod(); String methodName = method.getName(); Object result = ic.proceed(); if (methodName.equals("greet")) { return "NEW " + result; } } } 

Decorator:

 @Decorator public class GreeterDecorator implements Greeter { @Inject @Any @Delegate private Greeter greeter; @Override public String greet() { return "NEW " + greeter.greet(); } } 

Or is it legal to say that I can reproduce all the behavior of decorators using interceptors, but is it more convenient to use decorators?

+8
java java-ee design-patterns decorator interceptor
source share
3 answers

One of the differences would be, as your example shows, with a decorator you usually write 1 decorator per 1 decorated class / interface.

With interceptors that are part of the AOP concept, you write 1 interceptor for a bunch of classes / methods, for example. you intercept all DAOs and make sure that the transaction is open before the call and closed after it.

An example of an interceptor in spring AOP, firstly, you declare a pointcut (what you need), here you match any method from the MyDao class that starts with the insert , contains any arguments and any return type.

 @Pointcut("execution(* com.example.dao.MyDao.insert*(..))") public void insertPointcut() { } 

Then you post around a board that references pointcut

 @Around(value = "com.example.SystemArchitecture.insertPointcut()") public void interceptMethod(ProceedingJoinPoint pjp) { // do pre-work Object retVal = pjp.proceed(); // do post work return retVal; } } 

However, each part has its own pro / cons. For example. interceptors are more flexible, but imagine that you change the name of the method, if you use a decorator, you will probably get a compiler error, with interceptors, it just won't match and won't execute your logic around.

+8
source share

In general, a decorator is used to add new functions or modify existing functions. He uses composition as an alternative to inheritance. Decorators often provide additional APIs (methods) that are not available in decorated classes.

AOP (for example, an interceptor), on the other hand, is used to improve existing behavior. It does not add additional APIs and, as a rule, does not change existing functions. It starts by invoking existing functionality and responds by taking some action; but the existing functionality as well as the existing API remain unchanged.

I am not familiar with JEE implementations, so they can blur the lines between these two patterns. Important points to compare would be,

  • Can @Interceptor introduce new methods or execute only existing methods?
  • Can @Interceptor override existing methods or add only extra behavior?
  • Can @Decorator be applied to packages and class hierarchies, or is it limited to one of them?

Besides the functional differences between the two patterns, it may also be interesting to consider potential differences in performance. I would expect @Interceptor be significantly slower, as it should check method calls at runtime, while @Decorator calls can be resolved at compile time.

+2
source share

Decorators are very similar to interceptors with two interesting differences:

  • The decorator must implement the interface that it decorates (and yet it can be abstract, so it does not need to implement methods)

  • The decorator may have a link to the object that he decorates. This is done by injection.

Link: https://blog.frankel.ch/cdi-an-overview-part-2

0
source share

All Articles