In most commonly used
programming languages, delegation pattern operates based on conformation or
even similarity of objects. In this case it is very similar to delegation
pattern in Objective C in which it heavily relies on protocols. However, this
pattern can never be as effective in other languages as it is in Objective C.
The reasons behind this is that Objective C supports categories which allow
methods to be added to existing classes without having to subclass them or
having to have access to the source code. In addition, Objective C provides
@optional notation when creating protocols. Therefore, not all the methods in
protocols have to be implemented. This feature, plus the ability to check if an
object (in this case the delegate) responds to a specific message (using respondsToSelector),
make delegation pattern in Objective C very convenient and useful. In other
languages, for instance in Java, all the methods in an Interface should be
implemented. As a workaround for this, many Java interfaces come with an adapter
class, which is a common approach for not requiring the user to implement a lot
of empty methods. However, it limits inheritance and makes the design
inflexible. Using delegation pattern in Objective C, the relationship between
an object and other objects declared in it, can be bidirectional easily.
No comments:
Post a Comment