什么是“模板方法模式”?在可可中与对象 C ? (语言比较思维)
这是模板方法模式,Java和C++可以通过虚函数轻松实现它。用Object C 来实现这个模式怎么样? cocoa touch (iOS) 有什么例子吗?
Here is template method pattern , Java and C++ can implement it easily with virtual function. How about Object C to implement this pattern ? Any example in cocoa touch (iOS) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为 jer 已经指出,所有Objective-C方法本质上都是虚拟的。这是该语言的一个特性,与其他类 C 语言不太吻合。话虽这么说,模板方法模式的基础知识仍然可以在 Objective-C 中通过“手动”强制子类实现某些功能来实现。例如(使用链接的维基百科文章中的约定):
上面的代码通过在调用基类实现之一时抛出异常来强制
Game
的子类覆盖“虚拟”方法。实际上,这将测试从编译器阶段(就像在 C++ 或 Java 中一样)转移到运行时阶段(在 Objective-C 中经常完成类似的事情)。如果您确实想要强制执行不允许子类重写
playOneGame:
方法的规则,您可以尝试(*)从内部验证正确的实现>init
方法:(*) 请注意,由于 Objective-C 的本质,此代码不会对重新实现
playOneGame:
的子类产生 100% 坚如磐石的防御将允许子类重写instanceMethodForSelector:
以产生正确的结果。As jer has already pointed out, all Objective-C methods are essentially virtual. It is a feature of the language which does not quite mesh with other C-like languages. That being said, the basics of the template method pattern can still be achieved in Objective-C, by "manually" forcing subclasses to implement certain functions. For example (using the convention in your linked Wikipedia article):
The above code forces subclasses of
Game
to override the "virtual" methods by throwing an exception the moment one of the base class implementations is called. In effect, this moves the test from the compiler stage (as it would be in C++ or Java) and into the runtime stage (where similar things are often done in Objective-C).If you really want to enforce the rule that subclasses are not allowed to override the
playOneGame:
method, you can attempt(*) to verify the correct implementation from within theinit
method:(*) Note that this code does not result in a 100% rock-solid defense against subclasses which re-implement
playOneGame:
, since the very nature of Objective-C would allow the subclass to overrideinstanceMethodForSelector:
in order to produce the correct result.在 Objective-C 中,所有方法都类似于 C++ 虚拟方法。
In Objective-C all methods are akin to C++
virtual
methods.在 Objective-C 模板方法模式中,当你有一个算法的骨架但可以用不同的方式实现时,就会使用模板方法模式。模板方法定义了执行算法的步骤,并且它可以提供可能对所有或某些子类通用的默认实现。
让我们举个例子,但先看一下图片
希望它能帮助您理解“模板方法模式”。
In Objective-C Template Method Pattern is Used When you have a Skeleton of an Algorithm but it can be Implemented in different ways. Template method defines the steps to execute an algorithm and it can provide default implementation that might be common for all or some of the subclasses.
Let's take an example but First Look at the Picture
Hope it will help you to Understand the "Template Method Pattern".