如何在C#和java中实现这种可插拔机制?

发布于 2024-09-18 16:19:31 字数 452 浏览 8 评论 0原文

假设我有三个对象 A、B、C。
B 是以下接口的实现:

interface D
{
    event EventHandler<OrderEventArgs> OrderCreated;
    event EventHandler<OrderEventArgs> OrderShipped;
}

我将在 B 中添加两个函数,这将引发接口中的事件。

class B
{
  RaiseOrderCreated();
  RaiseOrderShipped();
}

A 是托管类,将调用 B 的函数来引发指定的事件。 我希望在CI中可以监听接口B或类C,每当B中的事件发生时,C可以得到通知,然后C可以对事件做出反应。

我正在考虑如何用C#和Java来实现这个机制。
任何帮助或提示将不胜感激!

Let's say I have three object A, B, C.
B is the implementation of following interface:

interface D
{
    event EventHandler<OrderEventArgs> OrderCreated;
    event EventHandler<OrderEventArgs> OrderShipped;
}

I would add two functions in B which will raise the events in the interface.

class B
{
  RaiseOrderCreated();
  RaiseOrderShipped();
}

A is the hosting class and will call B's functions to raised the specified events.
I hope in C I can listen to the interface B or class C, whenever the events in B are raised, C can get notified, then C can have reactive to the events.

I am thinking about how to implement this mechanism in C# and Java.
Any help or hints will be great appreciated !

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

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

发布评论

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

评论(2

风轻花落早 2024-09-25 16:19:31

因此,您将有一个名为 C 的 EventListener,一个名为 A 的对象,该对象具有 B 类型的字段,该字段是 D 的某种实现。

通常在 Java 中,您会以不同的方式设计 D:

interface D {
   static final String ORDER_PROPERTY="ORDER";
   void setOrder(Order order);
   Order getOrder();
}

class B implements D {
   // delegate object to implement support for event listeners and event firing
   private java.beans.PropertyChangeSupport propertyChangeSupport = new java.beans.PropertyChangeSupport(this);
   private Order order = null; // field that holds current order
   @Override
   public void setOrder(Order order) {
      Order old = this.order;
      this.order = order;
      propertyChangeSupport.firePropertyChange(ORDER_PROPERTY, old, order);
   }
   // more B code here
}

So you'd have an EventListener called C, some object called A which has a field of type B that is some kid of implementation of D.

Typically in Java you design D differently:

interface D {
   static final String ORDER_PROPERTY="ORDER";
   void setOrder(Order order);
   Order getOrder();
}

class B implements D {
   // delegate object to implement support for event listeners and event firing
   private java.beans.PropertyChangeSupport propertyChangeSupport = new java.beans.PropertyChangeSupport(this);
   private Order order = null; // field that holds current order
   @Override
   public void setOrder(Order order) {
      Order old = this.order;
      this.order = order;
      propertyChangeSupport.firePropertyChange(ORDER_PROPERTY, old, order);
   }
   // more B code here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文