How to Use Object Oriented Design in iOS Apps

By Rajiv Ramnath

Object-oriented design in iOS is a different way of designing programs, where the behavior is associated with objects. To illustrate, consider examining the actions involved in making tea and soup. However, don’t immediately leap into action instead, act like a detective entering a crime scene. Step back, survey the domain (the kitchen), and try to understand what’s in it. In other words, think objects first.

  • You see a kettle. You consider the kettle for a while and then say, “I can add water to it; I can pour water from it.”

  • You look at a teapot. As it looks back, you think, “I can add water to this, too, and pour water from it, as well, just like I can with a kettle.”

Both the kettle and teapot appear to be able to take in and pour out water. They both also have state (the water being held). Therefore, you generalize that each of these objects is a vessel. You also think about the characteristics of these objects, that is, their attributes — such as their volume, color, and material.

You think about the other items in the kitchen, namely, the stove, the faucet, the cups and bowls, the various sorts of spoons, and the food items: soup, tea, and bouillon, even the kitchen clock. You think about their capabilities, as well. You try to generalize the kinds of objects into higher-level concepts, such as vessel.

Rather than thinking of a recipe only as a sequence of actions, you realize that a recipe is also an object. After all, you can create recipes, tear up recipes, and share recipes. A recipe is indeed something tangible; therefore, it’s an object, an object with capabilities. When you follow a recipe, you can imagine that you’re running it.

So, you make run a capability of a recipe. You also make setting up and cleaning up responsibilities of a recipe object. Does a recipe have attributes? What about its author (a string)? Or how many people it serves (an integer)? Finally, recipes clearly have a generalization hierarchy, with Making Tea and Making Soup being specializations of a generic Recipe.

The table below lists the various kinds of objects, their capabilities, their attributes, and their generalizations.

Kind of Object Capabilities Attributes Generalizations
Vessel Add liquid; Pour out liquid Volume, color, material
Teapot The capabilities of a Vessel (that is, Add and Pour) The attributes of a Vessel Vessel
Tureen All the capabilities of a Vessel The attributes of a Vessel Vessel
Kettle All the capabilities of a Vessel; Boil The attributes of a Vessel Vessel
Cup All the capabilities of a Vessel; Drink from The attributes of a Vessel Vessel; You can drink from
Bowl All the capabilities of a Vessel; Eat with spoon The attributes of a Vessel Vessel; You can consume liquids it holds, using a spoon
Stove Put a Vessel on; Turn on; Turn off; Set level Color
Faucet Open; Close Color
Tea Steep Type Food item
Bouillon Steep Flavor Food item
Clock Wait
Recipe Set up; Run; Clean up Level of difficulty; Creator; Number of people served
Make Tea Responsibilities of a Recipe Attributes of a Recipe; Number of cups Recipe
Make Soup Responsibilities of a Recipe Attributes of a Recipe; Number of bowls Recipe

An entry like All the capabilities of a Vessel for the Cup object means that a Cup is understood to automatically have the Add liquid and Pour out capabilities because it is a Vessel. In addition, a Cup has the Drink from capability. In other words, you can do more with a Cup than you can with a plain old Vessel.

To put it in OO terminology, a Vessel is a generalization of a Cup, whereas a Cup is a specialization of a Vessel.

The Object-Oriented MakeTea Recipe

@interface TeaRecipe : Recipe // Make Tea is a kind of Recipe
@implementation TeaRecipe
+ Setup; {...} // Sets up the recipe
+ Run:(int) NumberOfCups;
{ // this is an instance method
  NSArray *TeaCups; // Declare an array of cups
  [Faucet Open]
  [Kettle AddLiquid:(NumberOfCups * CUPVOLUME)];
  [Stove PutVesselOn:Kettle];
  [Stove TurnOn];
  For (i=1; i <= NumberOfCups; i++){
   [TeaPot Add:Tea Quantity:@”1 spoon”];
  While(![Kettle IsBoiling]); //keep looping till the water boils
  [Kettle PourWaterFrom:Teapot];
  [Clock Wait:@”5 minutes”];
  For (i=1; i<= NumberOfCups; i++)[Kettle Pour:[TeaCups objectAtIndex:i]];
  NSLog("Enjoy Tea");
} // End MakeTea
+ Cleanup; {...} // cleans up after the recipe

You make two cups of tea using this program:

MakeTea teaRecipeInstance = [[TeaRecipe alloc];
[teaRecipeInstance Setup];
[teaRecipeInstance Run:2];
[teaRecipeInstance Cleanup];