Announcement Announcement Module
Collapse
No announcement yet.
applying stateful mixins to any number of targets Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • applying stateful mixins to any number of targets

    Does anyone have a simple example of applying a stateful mixin to any number of targets that are created on the fly?

    I want to implement an observer as a mixin that adds an Observable interface to an object. Then I want to use it like this:

    Code:
    Pojo object1 = makePojoObservable(new TargetClass());
    ((Observable) object1).addObserver(someObserver);
    object1.doSomethingThatWillNotifyObservers();
    
    Pojo object2 = makePojoObserved(new TargetClass());
    // same for object2
    What I'm looking for is the makePojoObserved() code, not how to implement the ObservableMixin itself.

    Thanks!

    Felix

  • #2
    Using ProxyFactoryBean, I found a working solution myself:

    Code:
        ProxyFactory pf = new ProxyFactory(new Class[] {TargetClassInterface.class});
        pf.addAdvice(new ObservableMixin());
        pf.setTarget(new TargetClassImpl());
        PojoBean bean = (PojoBean) pf.getProxy();
    But I needed to introduce an interface for TargetClass. Can this be done without an interface for my TargetClass?

    Felix

    Comment


    • #3
      oops, PojoBean should read TargetClassInterface, here's the correct code:
      Code:
          ProxyFactory pf = new ProxyFactory(new Class[] {TargetClassInterface.class});
          pf.addAdvice(new ObservableMixin());
          pf.setTarget(new TargetClassImpl());
          TargetClassInterface obj = (TargetClassInterface) pf.getProxy();

      Comment


      • #4
        and here's the version without interfaces

        Code:
            ProxyFactory pf = new ProxyFactory();
            pf.setProxyTargetClass(true);
            pf.addAdvice(new ObservableMixin());
            pf.setTarget(new TargetClass());
            TargetClass obj = (TargetClass) pf.getProxy();

        Comment

        Working...
        X