紧耦合类:在我的情况下什么是更好的设计

发布于 2024-12-29 00:44:28 字数 855 浏览 4 评论 0原文

对于我的情况,更好的解决方案是什么?如何设计类以使它们不是非常耦合?

我有一个库(API),它提供了一些功能(例如,使用 subscribe 方法订阅流式外汇价格)。我有一个 API 客户端,它告诉 API 它想要获得哪些价格。 API 通过 SubscribeSuccess(Subscription) 和 SubscribeFailed(Subscription) 方法通过某些接口(例如 SubscriptionStatus)提供反馈。在 API 客户端中,我有一个活动订阅列表 (ListactiveSubscriptions)。我希望 API 客户端仅对订阅成功做出反应(只需将订阅添加到列表中)。在其他情况下 - 只需打印消息到日志。 组织订阅侦听器和 API 客户端之间关系的最佳方式是什么? 选项可以是:

  1. 将 API 客户端实例传递给订阅侦听器,以便它可以调用 apiClient.addSubscription(subscription)
  2. API 客户端实现 SubscriptionStatus 接口并管理这些事件(失败、成功)内部:activeSubscriptions.add(subscription))。 Contra:有很多类型的操作,每个操作都有它自己的侦听器。所以 Api Client 将是一个非常大的类。
  3. 用一个方法SubscriptionSuccess(subscription)定义自己的接口并让API客户端实现它?
  4. 你的选择?

对主题的任何想法表示赞赏!

谢谢!

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:

  1. Pass API client instance to the subscription listener so it can call apiClient.addSubscription(subscription)
  2. API client implement implement SubscriptionStatus interface 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.
  3. Define own interface with one method SubscriptionSuccess(subscription) and let API client implement it?
  4. Your option?

Any thoughts on topic are appreciated!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

死开点丶别碍眼 2025-01-05 00:44:28

我会选择选项2,但有一个问题。如果 SubscriptionStatus 接口真的很大,并且您知道某些客户端只想实现其中的一部分,那么您可以提供一个基本的空超类,然后让客户端扩展它(使其成为抽象的) 来强制它们)

类似 BaseSubscriptionStatus 的所有方法都有空实现,并让用户覆盖它想要的方法。另一种选择是

throw UnsupportedOperationException("This method is not supported by your implementation of SubscriptionStatus. Please override it");

为每个基本方法而不是空实现。

当然,您可以保留 SubscriptionStatus 接口以实现适当的依赖项注入和可测试性,只需让 BaseSubscriptionStatus 实现它即可。

I would go option 2, with a catch. If the SubscriptionStatus interface 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 it abstract to force them)

Something like BaseSubscriptionStatus that has empty implementations for all methods, and let the user override the ones it wants. Another option is to

throw UnsupportedOperationException("This method is not supported by your implementation of SubscriptionStatus. Please override it");

for each base method instead of the empty implementation.

Of course, you can keep the SubscriptionStatus interface for proper dependency injection and testability, only make BaseSubscriptionStatus implement it.

幸福还没到 2025-01-05 00:44:28

我会选择第二个。这将为最终用户提供最大的灵活性,并能够在其情况下更有效地响应流媒体问题。

I would go with option two. This would give the end use the most flexibility and be able to respond to issues with the streaming more effectively in their situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文