Interface ECPObserverBus

  • All Known Implementing Classes:
    ECPObserverBusImpl

    public interface ECPObserverBus
    Bus for application wide notifications on events of various types. Check the type hierarchy of ECPObserver to discover possible types.
    This is a universal observer bus. This class follows the publish/subscribe pattern, it is a central dispatcher for observers and makes use of generics in order to allow type safety. It can be used as singleton or be injected through DI. Observers have to implement the ECPObserver interface, which is only used as a marker. Future use of Annotations is possible. by using notify(Class) (e.g. bus.notify(MyObserver.class).myObserverMethod()) all registered Observers are notified. This is implemented by using the java Proxy class. By calling notify(Class) a proxy is returned, which then calls all registered observers. The proxy can also be casted into ECPObserverCall, which allows to access all results by the different observers.
    Example code:
     // A is ECPObserver
     A a = new A() {
    
            public void foo() {
                    System.out.println("A says: go!");
            }
     };
    
     // B extends A and is ECPObserver
     B b = new B() {
    
            public void say(String ja) {
                    System.out.println("B says: " + ja);
            }
    
            public void foo() {
                    System.out.println("B says: h??");
            }
     };
    
     // B is registered first
     ECPObserverBus.register(b);
     ECPObserverBus.register(a);
    
     ECPObserverBus.notify(A.class).foo();
    
     ECPObserverBus.notify(B.class).say("w00t");
    
     // Output:
    
     // B says: h??
     // A says: go!
     //
     // B says: w00t
    
     
    Author:
    wesendon, Eugen Neufeld
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      <T extends ECPObserver>
      T
      notify​(java.lang.Class<T> clazz)
      This method allows you to notify all observers of type .
      void register​(ECPObserver observer)
      Registers an observer for all observer interfaces implemented by the object or its super classes.
      void unregister​(ECPObserver observer)
      Unregisters an observer for all observer interfaces implemented by the object or its super classes.
    • Method Detail

      • register

        void register​(ECPObserver observer)
        Registers an observer for all observer interfaces implemented by the object or its super classes.
        Parameters:
        observer - observer object
      • unregister

        void unregister​(ECPObserver observer)
        Unregisters an observer for all observer interfaces implemented by the object or its super classes.
        Parameters:
        observer - observer object
      • notify

        <T extends ECPObserver> T notify​(java.lang.Class<T> clazz)
        This method allows you to notify all observers of type . It returns a proxy object of type T which allows you to call the specific methods of your observer. Calling any method will be delegated to all registered observers of the given type .
        Type Parameters:
        T - type of observer
        Parameters:
        clazz - class of observer
        Returns:
        call object of type T