0

Example:

The MacOS API known as Cocoa uses Delegation to specify various behaviors within the app. A Delegate is a separate object that implements a set of methods that are called by an original object. For example an NSApplication calls the applicationWillTerminate method in the NSApplicationDelegate (which is overridden by the programmer to add custom cleanup code) when the application is about to terminate.

The primary benefit to Delegation is the minimization of duplicate method tables if multiple objects would otherwise have the same methods/behavior. Having them all reference the same delegate is better than each having their own copy of the methods.

However as far as I can tell NSApplication is a singleton and only one instance is ever created thus negating the aforementioned benefit.

Is there any benefit to using a delegate to specify behavior of a singleton rather than having the user override the singleton class and providing their own methods all in one place?

3
  • 1
    Is the delegation pattern being used in relation to singleton benefits though? It seems more like the NSApplicationDelegate exists to provide limited overridability in terms of how the NSApplication works. That would be unrelated to whether NSApplication happens to be a singleton or not. In short, I don't think you can assume that individually applied patterns explicitly account for one another, these might simply be two separate design decisions where the tiny bit of overlap that they do have was simply ignored because it was not considered important (maybe not even considered). Commented Mar 25, 2024 at 2:46
  • @Flater Apple does allow you to subclass NSApplication and override methods directly though. Commented Mar 25, 2024 at 2:50
  • You need to keep in mind that NSApplication might be singleton in your code base, but the OS deals with thousands of them from all applications running. You're comparing things in different scopes. Commented Mar 25, 2024 at 14:32

2 Answers 2

5

Is there any benefit to using a delegate to specify behavior of a singleton rather than having the user override the singleton class and providing their own methods all in one place?

The singleton class can be final. With no requirement to be overrideable. That can simplify the design and make the code easier to read.

It also simplifies creating your own class which only has to satisfy an interface not deal with states and behaviors from the inherited class. This helps eliminate the yo-yo problem.

The draw back is delegating involves more boilerplate code. Each delegated method has to be explicitly called.

All this is true even with a singleton. That just means you only create one instance of it. But you’re free to make as many types as you like. Even when they are singletons.

For more on this see the adage: prefer composition to inheritance.

2

Nobody cares about whatever table size.

But if you create a subclass, then a developer who knows the behaviour of the original class has no clue what your subclass will be doing. A macOS / iOS developer is expected to know exactly what NSApplication does. They cannot know what your subclass does.

The delegate pattern where you store a delegate object is very powerful yet focused: You can change only what the class wants you to change. On the other hand you have the full capabilities of an object available to you when you create the delegate. (Nowadays you would often use a closure instead of a delegate).

(A subtle effect of delegates as used in macOS and iOS is that the code that is supposed to call the delegate can detect the absence of the delegate and act accordingly. Say I have a “Color” menu which asks a delegate for the Color of an object, or asks it to change it. Absence of the delegate responsible for getting the Color means the object has no Color, absence of the “set Color” delegate means the Color cannot be changed directly. And the delegate can be reused. For example your menu item and your scripting language implementation could use the exact same delegate).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.