Objective-C 中的访问者模式
我一直在寻找在 Objective-C 中实现访问者设计模式的最佳方法。由于该语言不支持方法重载,因此“传统”实现(例如 Java 中的实现)似乎是不可能的。
在我当前的实现中,我有一个 Visitor 协议、一个 Visitor 类、该 Visitor 类的几个子类,以及要访问的各种对象。一旦被访问的对象接受访问者,它们就会调用访问者的访问方法,并将自己作为参数传递。 Visit 方法采用一个 id,然后对其进行类型转换并
[self performTasksOnObjectClass: (ObjectClass *)object];
作为 if/elseif/else 块的一部分进行调用。这些调用由相关的 Visitor 子类接收,并且 Visitor 在对象上执行它需要的任何任务。
还有比这更好的实现访客模式的方法吗?我不喜欢在 if/elseif/else 块内调用“isKindOfClass”或“isMemberOfClass”。它看起来笨重且不优雅。此外,以这种方式实现 Visitor 方法仍然“值得”吗?被访问的对象仍然可以保持对访问者一无所知,但是还有其他方法可以实现这一点。
已经有人建议委托或类集群可能是访问者模式更合适的替代方案。我很想知道大家的想法!
编辑:实际上,我在子类中调用了不同名称的方法,我已经更清楚地说明了这一点。
I've been looking at the best way to implement the Visitor design pattern in Objective-C. Since the language doesn't support method overloading, a 'traditional' implementation such as one might find in Java seems impossible.
In my current implementation, I have a Visitor protocol, a Visitor class, and several subclasses of that Visitor class, along with the various objects to visit. Once a visited object accepts the Visitor, they call the visit method of the Visitor, passing themselves as an argument. The visit method takes an id, then type-casts it and calls
[self performTasksOnObjectClass: (ObjectClass *)object];
as part of an if/elseif/else block. These calls are them picked up by the relevant Visitor subclass and the Visitor performs whatever tasks it needs to on the object.
Is there a better way of implementing the Visitor pattern than this? I dislike resorting to 'isKindOfClass' or 'isMemberOfClass' calls inside if/elseif/else blocks. It just seems clunky and inelegant. Additionally, is it still 'worth' implementing a Visitor method in this way? The visited objects can still remain ignorant of the Visitor, but there are other ways in which this can be achieved.
It has already been suggested that either delegation or class clusters might be more suitable alternatives to the Visitor pattern. I'd be interested to see what you all think!
Edit: I actually had differently named methods being called in the subclass, I've made this clearer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用一些内省/反思来使其更清晰一些。您不能重载方法名称,但可以避免编写如下所示的 switch 语句:
您的实际执行任务方法将命名如下:
注意:如果您使用 ARC,您会得到虚假的结果通过以这种方式从字符串创建选择器来警告,因为它无法在编译时告诉方法参数的保留规则应该是什么。您可以使用 #pragma 使这些警告静音,如下所示:
You can use some introspection/reflection to make this a bit cleaner. You can't overload method names but you can avoid writing a switch statement like this:
Your actual performTasks methods would then be named as follows:
Note: If you're using ARC, you'll get spurious warnings by creating selectors from strings in this way because it can't tell at compile time what the retain rules should be for the method parameters. You can silence these warnings using a #pragma, as follows:
您可以采用以下方法将选择器映射到 objc 类型,然后让此实现执行“动态重载”的方法查找。这样的实现将消除您实际使用中的大部分噪音(请参见演示 - 向下 8 页):
我们的包括:
我们的基本类型:
MONVisitorEntry 将类与选择器关联:
MONVisitorEntry.h:
MONVisitorEntry.m:
MONVisitorMap 是
MONVisitorEntry
对象的映射。这种类型没有类型安全——您应该重新引入它。MONVisitorMap.h:
MONVisitorMap.m:
演示:
创建一些测试类型(在此处添加一些
.m
文件):创建访问者:
MONZoo。 h:
MONZoo.m:
现在尝试一下:
这将带来我们的动物园之旅:
注释:
You could take the following approach to map selectors to objc types, and then let this implementation do the method lookups for the "dynamic overloading". Such an implementation would remove the majority of the noise for your actual uses (see Demo -- 8 pages down):
Our includes:
Our basic types:
MONVisitorEntry associates a Class to a selector:
MONVisitorEntry.h:
MONVisitorEntry.m:
MONVisitorMap is a map of
MONVisitorEntry
objects. this type has no type safety -- you should reintroduce it.MONVisitorMap.h:
MONVisitorMap.m:
Demo:
Create some test types (add some
.m
files here):Create the visitor:
MONZoo.h:
MONZoo.m:
Now try it out:
Which yields our trip to the zoo:
Notes: