Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have the concept of a SlowLoading thing:

public interface SlowLoading {
  boolean hasLoaded();
}

I also have a component MyComponent:

public interface myComponent{
  void doSomething();
}

My implementation covers both:

public class MyComponentImpl implements SlowLoading, MyComponent {
   // omitted for brevity
}

I have a container (interface omitted for brevity) which returns MyComponent:

public class MyContainerImpl implements MyContainer{
  @Inject private MyComponent component;

  @Override    
  public MyComponent doSomething(){
    // magic happens
    return myComponent;
  }
}

I also have an AOP component that intercepts the return of slow loading things and implicitly waits for them to load before returning:

@Pointcut("execution(SlowLoading+ *(..) )")
  public void returnsSlowLoadingThing() {
}

However, at present the MyContainer method is not proxied, since the return value does not implement SlowLoading.

What is the best way to encapsulate this behaviour?

Options I've considered:

  • Make MyComponent extend SlowLoading. This feels like a code smell and breaking of encapsulation - bleeding implementation into the interface. A client does not care if it is slow loading or not.
  • Inject MyComponentImpl directly into my container and return it explicitly (co-variant returns). Again, this feels like it is breaking encapsulation and the point of programming to interfaces, but feels better than the above, since it is done at a lower level and hidden behind the interface (implementation detail). However it is breaking the encapsulation of the AOP functionality which should be orthogonal and transparent.
  • Perform the AOP wait before invoking a method on a SlowLoading thing. This seems the most appropriate, but I've not been able to get this working so far.
share|improve this question

migrated from codereview.stackexchange.com May 15 at 3:51

This question came from our site for peer programmer code reviews.

add comment

1 Answer

i think this is not the best example for AOP.

It seems it would have to break encapsulation, possible workarounds may be:

A. Use 2 more interfaces that cover the 2 cases of SlowLoading (either wait or not-wait) and use one of these interfaces in the AOP cut

B. Not use AOP at all for this functionality and use 2 interfaces (as above) that handle different cases of SlowLoading (wait and block or not-wait and proxy). This would also make use of some factory pattern to get the correct SlowLoading instance (either wait or non-block)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.