Getting Started with Object-Oriented iOS Programming - dummies

Getting Started with Object-Oriented iOS Programming

By Rajiv Ramnath

Part of Beginning iOS Programming For Dummies Cheat Sheet

iOS programming, Objective-C, object-orientation, and OO design go together. Read on for some helpful quick tips for OO Design. It’s time to get up to speed!

Designing “good” classes

Your classes must be highly cohesive and loosely coupled. Here is a checklist to quickly test whether a class is good: A “No” answer to any of the questions means that you may need to rethink your class.

  • Can you give the class a name that captures its reason for being?

  • Can you give it a description that says that it does just one thing?

  • Does it have responsibilities (methods)? Are all the responsibilities in alignment with the description?

  • Does it have collaborators?

  • Does it — or its components — maintain long-lived state?

Making a type a subclass of an existing class or use delegation

Say that you need a type named Tiger and it seems to share the responsibilities of an Animal. Therefore, you’re trying to decide whether you should declare Tiger as a subclass of a class named Animal, or perhaps declare a member variable of type Animal and delegate animalistic responsibilities to this member variable.

Say these two sentences out loud:

  • Tiger IS-A an Animal.

  • Tiger CAN-BE an Animal.

If the first makes more sense (which in this case it does), Tiger should be a subclass of Animal.