Chapter 5. Getting Other Objects to Do Your Work for You

In This Chapter

  • Using delegation to implement a new transaction

  • Defining formal and informal protocols

  • Using categories to extend a class

In Chapter 2 of this minibook, you find out how to use inheritance to create subclasses such as CashTransaction and CreditCardTransaction to implement more specific functionality that was defined generically as a superclass — such as the spend functionality in the Transaction class. I also show back in Chapter 2 how you can use inheritance to add new functionality, new methods, and new instance variables to a subclass.

If you were to add ATM transactions (which require a transaction fee) to the mix, it would make sense to use inheritance to create a new subclass for an ATM transaction. If you did that it would also mean (thanks to the wonders of polymorphism, as described in Chapter 2 of this minibook) that the only changes you would have to make besides defining the new class would be to add a new method to Destination (in addition to the existing spend- Cash: and chargeCreditCard: methods): useATM: to create the new ATM transaction.

Frameworks provide a good model for how to create extensible and enhance-able applications. As you start to work with the UIKit and AppKit frameworks, you'll often use inheritance to extend the behavior of framework classes and to add your own unique application behavior. There are times, however, when inheritance is not an option due to technical or architectural ...

Get iPhone® Application Development All-In-One For Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.