質問

Consider I have a Calculator class, it calculates an Integer. On higher parts of the system, say a MVP presenter, I want to work with an ReactiveX Observable that emits the Integer.

I could do the following:

public class Calculator {

    public Observable<Integer> doSomeVeryComplicatedCalculations() {

        return Observable.just(0);

    }

}

I am wondering if it is a good practice to keep the Calculator class free from ReactiveX and use a wrapper like this:

public class Calculator {

    public Integer doSomeVeryComplicatedCalculations() {

        return 0;

    }

}


public class RxCalculator {

    public Observable<Integer> doSomeVeryComplicatedCalculations() {
        return Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                subscriber.onNext(new Calculator().doSomeVeryComplicatedCalculations());
            }
        });
    }

}

This way the actual Calculator

a. can be tested without ReactiveX where imho ReactiveX adds some complexity to testing

b. the Calculator class can be used in projects that don't use ReactiveX.

Are there any downsides to this, besides of course that I'll end up with more code / classes?

役に立ちましたか?

解決

I am wondering if it is a good practice to keep the Calculator class free from ReactiveX and use a wrapper

Yes. You should not depend on your framework, library or tool. Depend on abstractions, not on implementations.

I am PHP programmer and once I was looking for a good http library to use in my project. I chose Guzzle. It's really good. It has tons of features from which I use only 1% ;) I used it in several projects of mine through all the code.

But the next version of Guzzle was released with backwards incompatible changes. They changed a lot (for the better). And I suddenly realized, that I depend on previous Guzzle version through all my code! To switch to a newer version means, that I need to rewrite a good bunch of it.

Now when I'm using some powerful external library, I always create a wrapper, that has only features enough to suit my needs and allows me to quickly swap implementation.

ライセンス: CC-BY-SA帰属
所属していません softwareengineering.stackexchange
scroll top