what is the better solution in my situation, how to design classes so they are not very coupled?
I have an Library (API) which provides some functionality (for example, subscribe for streaming FX prices with subscribe method). I have an API client, which tell to API which prices it want to get. API provides feedback with some interface (for example SubscriptionStatus) with methods SubscribeSuccess(Subscription) and SubscribeFailed(Subscription). In API client I have a list of active subscriptions (List<Subscription> activeSubscriptions). And I want API client only react on subscription success (just add subscription into list). In other cases - just print message to log.
What is the best way to organize relations between Subscription listener and API Client?
Options could be:
- Pass API client instance to the subscription listener so it can call
apiClient.addSubscription(subscription) - API client implement implement
SubscriptionStatusinterface and manage those events (fail, success internally: activeSubscriptions.add(subscription)). Contra: There are a lot of types of actions and every action has it's own listener.. So Api Client will be really big class. - Define own interface with one method
SubscriptionSuccess(subscription)and let API client implement it? - Your option?
Any thoughts on topic are appreciated!
Thanks!
I would go option 2, with a catch. If the
SubscriptionStatusinterface is really really big, and you know some clients only want to implement part of that, you can provide a base empty superclass, and you let clients extend it (make itabstractto force them)Something like
BaseSubscriptionStatusthat has empty implementations for all methods, and let the user override the ones it wants. Another option is tofor each base method instead of the empty implementation.
Of course, you can keep the
SubscriptionStatusinterface for proper dependency injection and testability, only makeBaseSubscriptionStatusimplement it.