Why does cocoa use delegates instead of inheritance?

Why does cocoa use delegates rather than inheritance?

+6
inheritance cocoa delegates
source share
3 answers

With delegates, you can have one object that is a delegate to many other objects. For example, your MyController instance may be a delegate to NSTableView, NSTextField, NSWindow, and any other objects that make up your interface. This gives you a compact place to put all your user interface code in one section of your interface.

If you did this using a subclass, you need to create one subclass for each object from which you want to get callbacks.

Also, this is a classic question of inheritance and composition

+6
source share

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

Meanwhile, using a delegate allows you to create a simple object that answers several specific questions or reacts differently.

Now that you combine this with the dynamism you can achieve by replacing delegates on the fly, he can create a very flexible, robust system that facilitates code reuse.

There are several general discussions of these things here and here . You can also find some old SO questions here and here .

+5
source share
0
source share

All Articles