OCaml 中的中介者设计模式

发布于 2024-07-19 05:10:10 字数 626 浏览 11 评论 0原文

我试图在 OCaml 中实现两个类之间的相互绑定(中介模式),但在编译时出现错误。

class virtual ['mediator] colleague mIn = object 
 val m = mIn
 method virtual getmediator : 'mediator
end;;

class concreteColleague mIn = object inherit colleague 
 method getmediator = m
end;;

(* Some other classes here *)

class mediator = object (self)
 val mutable myColleague = (None:colleague option)
 initializer 
  myColleague <- Some (new concreteColleague self)
end;;

错误:类构造函数同事需要 1 个类型参数,但此处应用于 0 个类型参数。

我不能说我对类定义中的 ['foo] 语法非常熟悉,但已经诉诸它(但没有效果)试图让中介者保留对所有同事和每个同事的引用其各自的中介者,同时还试图克服源代码中类定义的重要性。 我该如何让同事保留对他们的调解员的参考?

I am trying to accomplish a mutual binding between two classes in OCaml (a la Mediator Pattern's) and am getting an error upon compilation.

class virtual ['mediator] colleague mIn = object 
 val m = mIn
 method virtual getmediator : 'mediator
end;;

class concreteColleague mIn = object inherit colleague 
 method getmediator = m
end;;

(* Some other classes here *)

class mediator = object (self)
 val mutable myColleague = (None:colleague option)
 initializer 
  myColleague <- Some (new concreteColleague self)
end;;

Error: The class constructor colleague expects 1 type argument(s), but is here applied to 0 type arguments.

I can't say that I'm all that familiar with the ['foo] syntax in the class definition but have resorted to it (with no avail) in trying to allow the mediator to keep a reference to all colleagues and each colleague to its respective mediator while also trying to overcome the importance of a class's definition in source code. How do I go about allowing the colleagues to keep a reference to their mediator?

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

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

发布评论

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

评论(1

深居我梦 2024-07-26 05:10:10

我修复了所有编译问题。 当您从同事那里继承时,您需要提供其类型参数。 因此,我相信creteColleague也需要一个类型参数。 另外,您忘记将构造函数参数 mIn 传递给继承的类。 我使用对象的类型为 myColleague 的类型保护添加了一个类型参数。

class virtual ['mediator] colleague mIn = object 
 val m = mIn
 method virtual getmediator : 'mediator
end;;

class ['mediator] concreteColleague mIn =
 object inherit ['mediator] colleague mIn
 method getmediator = m
end;;

(* Some other classes here *)

class mediator = object (self : 'self)
 val mutable myColleague = (None:'self colleague option)
 initializer 
  myColleague <- Some (new concreteColleague self)
end;;

但是,我对中介者模式不熟悉,因此我不确定实现中介者模式的正确方法是什么。 您确定需要对象和虚拟类之类的东西吗?

I fixed all the compilation problems. When you inherited from colleague, you need to provide its type parameter. As a result, concreteColleague I believe needs a type parameter too. Also, you forgot to pass the constructor argument mIn to the inherited class. And I added a type parameter to your type guard for myColleague, using the type of the object.

class virtual ['mediator] colleague mIn = object 
 val m = mIn
 method virtual getmediator : 'mediator
end;;

class ['mediator] concreteColleague mIn =
 object inherit ['mediator] colleague mIn
 method getmediator = m
end;;

(* Some other classes here *)

class mediator = object (self : 'self)
 val mutable myColleague = (None:'self colleague option)
 initializer 
  myColleague <- Some (new concreteColleague self)
end;;

However, I am not familiar with the Mediator pattern, so I am not sure what is the proper way to implement the Mediator pattern. Are you sure you need objects and virtual classes and stuff?

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