Basic Structural and Behavioral Concepts in OO Design for iOS Apps - dummies

Basic Structural and Behavioral Concepts in OO Design for iOS Apps

By Rajiv Ramnath

Object-oriented design has both structural and behavioral concepts that are very useful in iOS applications. Object-oriented design is a different way of designing programs, where the behavior is associated with objects. The following define and explain the core OO terminology while bringing in additional concepts that complete the building blocks of OO.

Object

An object is an individual, identifiable item, either real or abstract, that contains data about itself and descriptions of its manipulations of the data. In other words, an object might be either

  • Something real and tangible, like the items in a kitchen

  • A pure concept, such as a mathematical concept, like an integral

Identifying objects is the first step in analyzing a domain.

Each object is considered an instance of a class.

Class

A class is a description of the organization and actions shared by one or more similar objects.

A type is a concept similar to that of class, and they’re often used interchangeably. Within programming languages, a class also has a technical meaning. It is a template of an object, through which new objects can be created. However, the concept of a type separate from that of a class is captured in many programming languages (but not Objective-C) by a programming construct called an interface.

Object-orientation also has the concept of a metaclass, whose instances are classes. There is one metaclass in Objective-C called Class.

Abstraction

When you identify the classes of objects and their capabilities don’t consider those classes or capabilities that weren’t relevant to the domain of the activity. For example, it’s unlikely you would have brought in a class to represent the capability of a teacup to be thrown if you were design a program about making tea.

This kind of modeling where you ignore what is irrelevant to focus on what is relevant for a particular domain is known as abstraction. Abstraction is the act of identifying the classes (and types) needed in order to pare down to those aspects that are relevant to the problem.

Generalization

The concept of generalization is exemplified in the relationship between the classes Vessel and Teacup. Generalization in a programming language is demonstrated through a mechanism known as inheritance, which allows the data and behavior of one class to be included in or used as the basis for another class.

Several programming languages have a root class from which all classes are inherited. In the Objective-C framework, the class is called NSObject. NSObject is equivalent to the class Object in Java.

A subclass is a class that inherits all the functionality from another class (known as the superclass). Furthermore, the subclass can override specific functionality by providing its own implementation of certain methods. The method of the superclass can still be called from the overriding method, if doing so makes sense. You can also extend the functionality of the superclass by providing additional methods, using additional member variables, and so on.

Similarly, a type (an interface) may also be extended using a subtype. Here, no functionality is overridden; it’s just that the subtype has more methods that an object must implement.

An object typically stores data, also known as its state. This state is stored in holders of data known as attributes.

Objects (as defined by classes) also have capabilities. A method is a way to access that capability — in other words, to execute a set of instructions that set or manipulate an object’s attributes.

Other objects cause these methods to be executed. They do so via message passing, which is the process by which an object sends data to another object or asks the other object to invoke a method. Message passing happens when another object calls the method of an object. Execution of a program typically begins with the calling of a function or method named main within a specific class.

The process of abstraction results in the creation of classes and types. Classes and types have one more characteristic: They constrain their use only through their methods and publicly accessible attributes. This is known as encapsulation, a technique for designing classes and objects that restricts access to the data and behavior by defining a limited set of messages that an object of that class can receive.

Encapsulation prevents access to the internals of a class and prevents the details on how the class is implemented from being known. Encapsulation enforces information hiding, another key concept in object-orientation.

Abstract classes serve only as specifications to be reused by other classes. You can’t create objects from abstract classes. Classes from which you can create objects are known as concrete classes. Of course, interfaces are always abstract.

An application consists of a collection of classes of various types. Objects of these classes interact with each other to implement the features of the application. The classes that a class interacts with are known as its collaborators.

Polymorphism is the ability of different classes to respond to the same message, with each class implementing the method appropriately. The following line taken from MakeDrinkableFood illustrates this concept:

  For (i=1; i<= NumberOfEatingVessels; i++)HeatingVessel.Pour(([EatingVessel objectAtIndex:i);

When tea is being made, EatingVessel is bound to a Kettle object. When soup is being made, EatingVessel is bound to a Tureen object.

It’s useful to separate these concepts into structural concepts that tell you something about the composition of the program and behavioral concepts that tell you something about how the program works. The concepts of abstraction, encapsulation, metaclass, class, type, object, inheritance, and interface are all structural concepts. The concepts of a method, message passing, and polymorphism are behavioral concepts.