01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

134 CHAPTER 6 Extending classes<br />

Person *person = [[Person alloc]<br />

initWithName:@"Collin"<br />

age:[NSNumber numberWithInt:23]<br />

gender:Male];<br />

Student *student = [[Student alloc]<br />

initWithName:@"Collin”<br />

age:[NSNumber numberWithInt:23]<br />

gender:Male<br />

classes:[NSArray arrayWithObjects:@"English",<br />

@"Spanish",<br />

@"Math", nil]<br />

numberOfCredits:[NSNumber numberWithInt:12]<br />

major:@"CS"];<br />

Teacher *teacher = [[Teacher alloc]<br />

initWithName:@"Winslow"<br />

age:[NSNumber numberWithInt:30]<br />

gender:Male<br />

classes:[NSArray arrayWithObjects:@"ARM",<br />

@"Imerssive Gaming",<br />

@"Physical Computing", nil]<br />

salary:[NSNumber numberWithInt:60000]<br />

areaOfExpertise:@"HCI"];<br />

}<br />

NSLog(@"My person description is:\n%@", person);<br />

NSLog(@"My student description is:\n%@", student);<br />

NSLog(@"My teacher description is:\n%@", teacher);<br />

Here you create Person, Teacher, and Student objects. At the end, you place a %@<br />

placeholder in your NSLog input. When you pass an object in after the string, the %@ is<br />

replaced with what is returned from each object’s description: method. With this<br />

done, you’re going to look at another way to modify classes’ functionality behind the<br />

scenes through a design method called clusters.<br />

6.5 Class clusters<br />

Class clusters is the <strong>Objective</strong>-C answer for the idea of abstraction. Generally, an abstract<br />

superclass is declared with methods that are implemented in nonvisible, concrete subclasses.<br />

This type of design is called an abstract factory design in Apple’s documentation. It<br />

has the benefit of simplifying the complexity of public classes by revealing only the<br />

methods declared in the abstract parent class. The abstract superclass is responsible for<br />

providing methods that create instances of the private subclasses.<br />

6.5.1 Why use class clusters<br />

Class clusters is really just a fancy way of saying “secret subclassing.” They allow developers<br />

to create a single doorway into multiple rooms. There are two common motivations<br />

for taking this design approach:<br />

1 Performance/Memory<br />

This type of design allows a developer to make a single class that can represent<br />

the creation point for multiple objects that have significantly different memory

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!