Question

I have created simple observer prototype:

class CObservable<T> {

    public function new() {
        mObservers = new Array<T>();
    }

    public function sign(aObserver:T):Bool {
        for (observer in mObservers)
            if (observer == aObserver)
                return false;
        mObservers.push(aObserver);
        return true;
    }

    private var mObservers:Array<T>;

}

And create interface with some callback functions:

interface IConcreteObserver {

    function onStart(aObservable:CConcreteObservable):Void;

    function onStop(aObservable:CConcreteObservable):Void;

    // ... more callbacks with same signature that differs only by names ...

}

And then I extend prototype with some concrete class:

class CConcreteObservable extends CObservable<IConcreteObserver> {

    public function new() {
        super();
    }

    public function notifyStart():Void {
        for (observer in mObservers)
            observer.onStart(this);
    }

    public function notifyStop():Void {
        for (observer in mObservers)
            observer.onStop(this);
    }

    // ... more notify methods for every callback ...
    // ... more and more ...
    // ... and even more ...

}

All these copy-pastes looks quite impractical for me especially when I have over than a dozen callback methods. Is there any neat trick to declare one simple notify method or may be macro in prototype and call them like notify(onStart), notify(onStop) and so on?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top