Intro to Object-Oriented Design: Part 1/2

This tutorial series will teach you the basics of object-oriented design. In this first part: Inheritance, and the Model-View-Controller pattern. By Ellen Shapiro.

Leave a rating/review
Save for later
Share

Update note: Check out the latest version of this tutorial, updated for iOS 8 and Swift! Intro to Object-Oriented Design in Swift.

A huge piece of the programming puzzle in working with Cocoa and Objective-C is Object-Oriented Programming. Almost all modern programming languages use this approach, and wrapping your head around its concepts and patterns can be incredibly helpful when reading and writing code.

Underneath the relation between UITableView and UIScrollView or NSString and NSMutableString are the fundamental concepts of object-oriented design. By understanding these concepts you’ll have a better grasp on why things are organized the way they are in Cocoa and Cocoa Touch, and you’ll be a more thoughtful programmer when writing your own apps and frameworks.

In this series you’ll learn more about object-oriented design, including the following concepts:

  • The Basics Of Objects
  • Inheritance
  • Model-View-Controller
  • Polymorphism
  • Common Object-Oriented Patterns

This series is designed for developers who are fairly new to programming overall – much like I was when I first started out. You likely haven’t worked with other languages extensively, and you’re not quite sure why everything is being done in a particular way.

This tutorial will cover object-oriented design principles rather than specific syntax, so you should already know the basics of Objective-C and Xcode before reading on. If you need a refresher on the basics, check out our other Beginning Objective-C tutorials.

Getting Started

In order to try and understand some of these concepts in a more concrete manner, you’ll build an application called Vehicles. This uses one of the most common metaphors for translating real-world items into virtual objects: the “vehicle”, which could be a bicycle, a car, or really anything with wheels.

For instance, this is a vehicle:

Kleinwagen_16

But so is this:

Motorcycle on a white background

Or this:

B

Or this:

European 18-wheeler with canvas trailer

In this part of the tutorial, you’ll create a data model using basic object-oriented techniques to represent all of these vehicles, and a simple application which implements the data model and displays vehicle data to the user.

Download the starter project, which contains a basic framework for the application you’ll use to learn about Object-Oriented Programming.

The Basics Of Objects

In object-oriented programming, the basic goal is to break down the characteristics of a “thing” to create an object or objects that describe what that thing is and what that thing does.

Sometimes, as with vehicles, your “thing” has a real-world equivalent. Sometimes it doesn’t, as with the many different types of UIViewController objects. For the sake of simplicity, you’ll start out by creating objects that have real-world analogs.

In order to answer the question of what a “thing” is, you have to first determine what its defining characteristics are.

Other languages will refer to this as a “field”, a “member”, or even just a “variable”. However, in Objective-C the defining characteristics of an object are shown by its properties.

Think about the generic concept of a “vehicle” for a moment — something that describes all of the photos above. What common characteristics of a “vehicle” spring to mind?

  • It has a number of wheels greater than zero.
  • It has some sort of power source, whether human, gas, electric, or hybrid, which makes it move.
  • It has a brand*, like Ford, Chevy, Harley-Davidson, or Schwinn.
  • It has a model name like Mustang, Corvette, Sportster, or Fastback.
  • It has a year associated with its manufacture.

*- this is sometimes referred to in cars and trucks as a “Make”, but we’ll refer to it as a “brand” across the board for clarity.

Now that you have the basic characteristics of a vehicle, you can create an Object with these characteristics.

There are two files in the starter project: Vehicle.h and Vehicle.m, which together represent a subclass of NSObject. In a moment you’ll read more about subclasses and what they are.

Add the following code to Vehicle.h after the @interface line:

@property (nonatomic, assign) NSInteger numberOfWheels;
@property (nonatomic, copy) NSString *powerSource;
@property (nonatomic, copy) NSString *brandName;
@property (nonatomic, copy) NSString *modelName;
@property (nonatomic, assign) NSInteger modelYear;

These property declarations describe the characteristics of the object you want to keep track of.

A Minor Digression: Under The Hood With Properties

Whenever you declare a @property in Xcode 4.4 and above, Xcode automatically synthesizes a backing instance variable, a getter method, and a setter method for that property. This saves a ton of boilerplate code. If there were no automatic synthesis, you would have to write all of the following code for every single instance variable:

@interface Vehicle() {
    NSString *_brandName;
}
@end

@implementation Vehicle

//Setter method
-(void)setBrandName:(NSString *)brandName
{
    _brandName = [brandName copy];
}

//Getter method
-(NSString *)brandName
{
    return _brandName;
}

@end

Not having to add this code for every single @property keeps your code cleaner and much more readable. This also means that you can access a @property in a couple of different ways:

  • someVariableName = self.brandName; goes behind the scenes and calls the [self brandName]; getter method that was synthesized for you, returns whatever value is stored in the _brandName instance variable, and assigns it to someVariableName.
  • self.brandName = @"Some Brand Name"; also goes behind the scenes and calls the [self setBrandName:@"Some Brand Name"]; setter method, which in turn sets the value of the _brandName instance variable to @"Some Brand Name".

Describing the Object

On to the second critical question for every object — what exactly does the object do?

A programmatic description of what an object does is almost universally called a method. Think about the common actions of the vehicles in the photos above:

  • It can go forward
  • It can go backward
  • It can stop
  • It can turn
  • It can change gears
  • It can make some sort of noise (e.g. a horn or a bell)

Most often, you’d be using methods with a void return type: for example, -(void)nameOfMethod. This is useful when you don’t need to get any information back from the method and you simply want the method to execute. However, to make it a little easier to display what’s happening in your app, you’re going to use some methods that return NSString objects.

Contributors

Over 300 content creators. Join our team.