Delegation is a basic design pattern. This allows you to share responsibilities between parts of your program. The idea is that part of your program, which, for example, draws on the screen, probably should not talk to your database. There are several reasons for this:
Performance: if the same object that accesses the screen accesses your data warehouse, you are faced with performance issues. Period. Full stop.
Code Rationale: It is easier to conceptualize code that is properly modular. (For me, anyway)
Flexibility: if you are a subclass in your code, this is great - until you start working in monolithic classes that have all kinds of undesirable behavior. You will reach the point where you have to overload the behavior to disable the action, and the property namespace may become dirty. Try categories, delegation, and blocks for alternatives.
However, I did encounter a situation where a subclass was appropriate. I have a kiosk app in which I wanted to automatically reject a specific settings menu if it has not interacted with it for a certain period of time. To do this, I had to have access to touch events throughout the application. I subclassed UIApplication
and redid sendEvent:
Then it was appropriate, although this is an extreme case. As King Solomon says in Ecclesiastes, to paraphrase: there is time and place for everything under the sun.
To make writing, reading, repeating and maintaining your program easier, it is highly recommended that you follow certain practices. You can subclass many classes, and Apple will not give up your application for bad code, provided that it works as advertised. However, if you do not follow specific proven practices, you dig your own grave. The subclass is not inherently bad, but the categories, protocols and blocks are so fascinating that I would prefer them anyway.
Moshe source share