I’m going through the TVAnimationGestures example from WWDC 2010. I have a question regarding the code. I copied and pasted the .h and .m for the SectionInfo object below. What I don’t understand is the architecture since I’m new to programming. All the implementation of the methods declared seem to be instance methods for NSMutableArray. So why create this class? I see that the property rowHeights is declared as readonly. Is that why they have these helper methods? Is it to encapsulate the data away from the other classes or something? Sorry if my terminology is not correct. Thanks.
SectionInfo.h:
/*
File: SectionInfo.h
Abstract: A section info object maintains information about a section:
* Whether the section is open
* The header view for the section
* The model objects for the section -- in this case, the dictionary containing the quotations for a single play
* The height of each row in the section
Version: 1.1
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2010 Apple Inc. All Rights Reserved.
*/
#import <Foundation/Foundation.h>
@class SectionHeaderView;
@class Play;
@interface SectionInfo : NSObject {
}
@property (assign) BOOL open;
@property (retain) Play* play;
@property (retain) SectionHeaderView* headerView;
@property (nonatomic,retain,readonly) NSMutableArray *rowHeights;
- (NSUInteger)countOfRowHeights;
- (id)objectInRowHeightsAtIndex:(NSUInteger)idx;
- (void)insertObject:(id)anObject inRowHeightsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromRowHeightsAtIndex:(NSUInteger)idx;
- (void)replaceObjectInRowHeightsAtIndex:(NSUInteger)idx withObject:(id)anObject;
- (void)getRowHeights:(id *)buffer range:(NSRange)inRange;
- (void)insertRowHeights:(NSArray *)rowHeightArray atIndexes:(NSIndexSet *)indexes;
- (void)removeRowHeightsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceRowHeightsAtIndexes:(NSIndexSet *)indexes withRowHeights:(NSArray *)rowHeightArray;
@end
SectionInfo.m
/*
File: SectionInfo.m
Abstract: A section info object maintains information about a section:
* Whether the section is open
* The header view for the section
* The model objects for the section -- in this case, the dictionary containing the quotations for a single play
* The height of each row in the section
Version: 1.1
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2010 Apple Inc. All Rights Reserved.
*/
#import "SectionInfo.h"
#import "SectionHeaderView.h"
#import "Play.h"
@implementation SectionInfo
@synthesize open, rowHeights, play, headerView;
- init {
self = [super init];
if (self) {
rowHeights = [[NSMutableArray alloc] init];
}
return self;
}
- (NSUInteger)countOfRowHeights {
return [rowHeights count];
}
- (void)getRowHeights:(id *)buffer range:(NSRange)inRange {
[rowHeights getObjects:buffer range:inRange];
}
- (id)objectInRowHeightsAtIndex:(NSUInteger)idx {
return [rowHeights objectAtIndex:idx];
}
- (void)insertObject:(id)anObject inRowHeightsAtIndex:(NSUInteger)idx {
[rowHeights insertObject:anObject atIndex:idx];
}
- (void)insertRowHeights:(NSArray *)rowHeightArray atIndexes:(NSIndexSet *)indexes {
[rowHeights insertObjects:rowHeightArray atIndexes:indexes];
}
- (void)removeObjectFromRowHeightsAtIndex:(NSUInteger)idx {
[rowHeights removeObjectAtIndex:idx];
}
- (void)removeRowHeightsAtIndexes:(NSIndexSet *)indexes {
[rowHeights removeObjectsAtIndexes:indexes];
}
- (void)replaceObjectInRowHeightsAtIndex:(NSUInteger)idx withObject:(id)anObject {
[rowHeights replaceObjectAtIndex:idx withObject:anObject];
}
- (void)replaceRowHeightsAtIndexes:(NSIndexSet *)indexes withRowHeights:(NSArray *)rowHeightArray {
[rowHeights replaceObjectsAtIndexes:indexes withObjects:rowHeightArray];
}
- (void)dealloc {
[play release];
[rowHeights release];
[super dealloc];
}
@end
It does look like a pretty “thin” wrapper, if you will. However, think of it from the other side of the equation: getting data out of this class. What is easier to remember when you ask yourself “How do I remove the row height at index 8?”:
or
The biggest difference I see is that the second is much more explicit about what you’re doing. It isn’t something generic like “remove an object from an array”, you’re “removing a row height object from section info”.
The other big benefit I can see is maintainability. Lets say that you want to build out the
SectionInfoclass with some more functionality and fill in those methods with more stuff: manipulating other data or some other action. Everything is already there to work with and is encapsulated. The other classes that use it won’t even have to know that you changed the internal workings of this class.On the other hand, if you went the straight
NSMutableArrayapproach, you would have to deal with removing it from all existing classes, creating this class, building it out, and then having the other classes implement it. All without (you hope) creating numerous bugs along the way.You still have to be careful that you don’t go too far down the architecture astronaut road, but it can be very beneficial to plan ahead.