在多个对象之间路由数据的简单设计?

发布于 2025-01-03 06:46:27 字数 1058 浏览 2 评论 0原文

在我当前的设计中,我使用了几个类,每个类代表一些 外围设备。每个类都提供输入方法和 输出回调。一个类的一些输出应用作输入 到另一个类,所以一个简单的设计就是让客户端实现所有 回调并处理对象之间的所有路由。

这可以用下面的简化示例来说明。在我的 应用程序的类数以及输入和输出都更高。

interface CentralLockControlCb
{
  void onCentralLockStateChanged(bool on);
}

class CentralLockControl
{
  void setDoorLockedIndicator(bool locked);
}

interface DoorControlCb
{
  void onDoorLockedStateChanged(bool locked);
}

class DoorControl
{
  void setCentralLockState(bool on);
}

class Main : public CentralLockControlCb, DoorControlCb
{
  private CentralLockControl centrallock;
  private DoorControl door;

  public static void main(String[] args)
  {
    clc = new CentralLockControl(this);
    dc = new DoorControl(this);
  }

  void onCentralLockStateChanged(bool on)
  {
    door.setCentralLockState(on);
  }

  void onDoorLockedStateChanged(bool locked)
  {
    centrallock.setDoorLockedIndicator(locked);
  }  
}

上面的解决方案避免了类需要相互了解, 但另一方面,它们现在似乎与 Main 类非常耦合。

作为替代方案,我考虑让每个类允许多个观察者, 并让班级互相观察。作为我考虑的另一种选择 类似于发布者/订阅者模式,但

对于这样的问题,您会推荐什么设计?

In my current design I use several classes which each represents some
peripheral device. Each class provide both methods for input as well as
callbacks for output. Some output from one class shall be used as input
to another class, so a simple design is to let the client implement all
callbacks and handle all routing between objects.

This can be illustrated with the following simplified example. In my
application the number of classes and inputs and outputs are higher.

interface CentralLockControlCb
{
  void onCentralLockStateChanged(bool on);
}

class CentralLockControl
{
  void setDoorLockedIndicator(bool locked);
}

interface DoorControlCb
{
  void onDoorLockedStateChanged(bool locked);
}

class DoorControl
{
  void setCentralLockState(bool on);
}

class Main : public CentralLockControlCb, DoorControlCb
{
  private CentralLockControl centrallock;
  private DoorControl door;

  public static void main(String[] args)
  {
    clc = new CentralLockControl(this);
    dc = new DoorControl(this);
  }

  void onCentralLockStateChanged(bool on)
  {
    door.setCentralLockState(on);
  }

  void onDoorLockedStateChanged(bool locked)
  {
    centrallock.setDoorLockedIndicator(locked);
  }  
}

The solution above avoids that the classes need to know about each other,
but on the other hand, they now seem very coupled to the Main class.

As an alternative, I considered letting each class allow multiple observers,
and letting the classes observe each other. As another alternative I considered
something similar to a publisher/subscriber pattern, but it seems

What design would you recommend for such a problem?

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

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

发布评论

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

评论(1

感情废物 2025-01-10 06:46:27

听起来您想要的是 Event Aggregator 您可以使用它,而不是在该功能中使用 Main 。
一旦你走这条路,你就能够注册你的设备,以一种非常好的方式监听其他设备的特定事件。此外,这种设计可能会引导您概括事件以携带有用的状态,以便您的设备可以在需要时忽略某些事件。它将使您的事件聚合器不那么复杂(不会决定事件发生时需要调用谁)。
最后,您将能够异步通知感兴趣的各方,这对于您的情况可能很重要。

It sounds like what you want is Event Aggregator that you could use instead of Main being used in that capacity.
Once you go that route, you'd be able to register your devices to listen to specific events about other devices in a very nice way. Moreover, this design will likely lead you towards generalizing your events to carry useful state so that your devices can ignore certain events when needed. It will keep your Event Aggregator less complex (there will be no decisions as to who needs to be called when an event happens).
Finally, you would be able to notify interested parties asynchronously, which may or may to be important in your case.

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