7

Why does cocoa use delegates rather than inheritance?

Anurag
  • 140,337
  • 36
  • 221
  • 257
Casebash
  • 114,675
  • 90
  • 247
  • 350
  • 2
    I don't know enough about obj-/cocoa to answer this question in debt (surely great answers will follow) but iirc the main argument is that using delegates allows for 'looser' coupling then using (multiple) inheritance (impossible in obj-c). – ChristopheD Jan 14 '10 at 23:06
  • What is iirc? I haven't heard of it. – Casebash Jan 14 '10 at 23:12
  • that is a perfect answer ChristopheD, you should post it as such – Anurag Jan 14 '10 at 23:28

3 Answers3

6

With delegates, you can have one object be the delegate of many other objects. For example, you can have your MyController instance be the delegate of an NSTableView, an NSTextField, an NSWindow, and any other objects that compose your interface. This gives a compact place to put all of your user interface code related to one section of your UI.

If you'd done that with subclassing, you'd have to create one subclass every object you wanted callbacks from.

Also, this is a classic inheritance vs composition question

Jon Hess
  • 14,237
  • 1
  • 47
  • 51
5

In general creating a subclass can be a time consuming process, requiring a lot of groundwork, and overriding various template methods.

Meanwhile, using a delegate allows you to create a simple object that answers a few specific question or reacts in various ways.

Now when you combine this with the dynamism that you can achieve by swapping out delegates on the fly it can create a very flexible robust system that promotes more code reuse.

There is some general discussions regarding these things here and here. You can also find some older SO questions here and here.

Community
  • 1
  • 1
Bryan McLemore
  • 6,438
  • 1
  • 26
  • 30
  • 1
    How does a template take less effort to create than a subclass? On the other hand, the ability to swap out on the fly could be useful in some circumstances, but I don't see why it is necessary for Cocoa. – Casebash Jan 14 '10 at 23:17
  • I'm not even so much sure that it's less work, but fewer levels of inheritence leads to a generally simpler design. Simplicity is a Good Thing (tm). – Bryan McLemore Jan 15 '10 at 02:06
  • As to Necessity. Nothing is absolutely necessary, it was the opinion of the framework designers that given the constraints of the language that composition via delegation was the best design choice they could make. After working in it for around a year I'm inclined to agree that there choice was a good one. But it was a little odd for me when I first came to Cocoa from other languages. – Bryan McLemore Jan 15 '10 at 02:09
0

Discussed at length here: http://www.cocoadev.com/index.pl?ExtendsIsEvil

And Java guys know this too: http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html

bbaassssiiee
  • 6,013
  • 2
  • 42
  • 55
  • Might as well put a comment if you're not going to elaborate. What happens if these links become dead, like the first one? – pqsk Aug 08 '13 at 03:34
  • Delegates simplify the customization of object behavior while minimizing coupling between classes. Inheritance is defined at compile time, delegation is not restricted like that and can be done at runtime. – bbaassssiiee Sep 09 '13 at 07:28