Class ECPObserverBusImpl

  • All Implemented Interfaces:
    ECPObserverBus

    public class ECPObserverBusImpl
    extends java.lang.Object
    implements ECPObserverBus
    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 ESObserver
     A a = new A() {
    
            public void foo() {
                    System.out.println("A says: go!");
            }
     };
    
     // B extends A and is IObserver
     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
     ObserverBus.register(b);
     ObserverBus.register(a);
    
     ObserverBus.notify(A.class).foo();
    
     ObserverBus.notify(B.class).say("w00t");
    
     // Output:
    
     // B says: h??
     // A says: go!
     //
     // B says: w00t
    
     
    Author:
    wesendon
    • Constructor Summary

      Constructors 
      Constructor Description
      ECPObserverBusImpl()
      Default constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T extends ECPObserver>
      T
      notify​(java.lang.Class<T> clazz)
      This method allows you to notify all observers.
      <T extends ECPObserver>
      T
      notify​(java.lang.Class<T> clazz, boolean prioritized)
      This method allows you to notify all observers.
      void register​(ECPObserver observer)
      Registers an observer for all observer interfaces implemented by the object or its super classes.
      void register​(ECPObserver observer, java.lang.Class<? extends ECPObserver>... classes)
      Registers an observer for the specified observer interfaces.
      void unregister​(ECPObserver observer)
      Unregisters an observer for all observer interfaces implemented by the object or its super classes.
      void unregister​(ECPObserver observer, java.lang.Class<? extends ECPObserver>... classes)
      Unregisters an observer for the specified observer interfaces.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ECPObserverBusImpl

        public ECPObserverBusImpl()
        Default constructor.
    • Method Detail

      • notify

        public <T extends ECPObserver> T notify​(java.lang.Class<T> clazz)
        This method allows you to notify all observers.
        Specified by:
        notify in interface ECPObserverBus
        Type Parameters:
        T - class of observer
        Parameters:
        clazz - class of observer
        Returns:
        call object
      • notify

        public <T extends ECPObserver> T notify​(java.lang.Class<T> clazz,
                                                boolean prioritized)
        This method allows you to notify all observers.
        Type Parameters:
        T - class of observer
        Parameters:
        clazz - class of observer
        prioritized - sort observer after ECPPrioritizedIObserver
        Returns:
        call object
      • register

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

        public void register​(ECPObserver observer,
                             java.lang.Class<? extends ECPObserver>... classes)
        Registers an observer for the specified observer interfaces.
        Parameters:
        observer - observer object
        classes - set of classes
      • unregister

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

        public void unregister​(ECPObserver observer,
                               java.lang.Class<? extends ECPObserver>... classes)
        Unregisters an observer for the specified observer interfaces.
        Parameters:
        observer - observer object
        classes - set of classes