OCaml 中的中介者设计模式
我试图在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我修复了所有编译问题。 当您从同事那里继承时,您需要提供其类型参数。 因此,我相信creteColleague也需要一个类型参数。 另外,您忘记将构造函数参数 mIn 传递给继承的类。 我使用对象的类型为 myColleague 的类型保护添加了一个类型参数。
但是,我对中介者模式不熟悉,因此我不确定实现中介者模式的正确方法是什么。 您确定需要对象和虚拟类之类的东西吗?
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.
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?