Skip to content

Instantly share code, notes, and snippets.

@afrael
Last active December 21, 2015 12:49
Show Gist options
  • Save afrael/6308769 to your computer and use it in GitHub Desktop.
Save afrael/6308769 to your computer and use it in GitHub Desktop.
The power of protocols to avoid concrete implementations
// Model
// OAStack.m
@protocol OAStack
- (void) pushData:(id)data;
- (id) popData;
@optional
- (void) purgeStack;
@end
// Model
// OAStackWithArray.m
@implementation OAStackWithArray : NSObject <OAStack>
@property (nonatomic, strong) NSMutableArray *containerArray;
@property (nonatomic, assign) NSUInteger currentIndex;
- (id)init
{
self = [[super alloc] init];
if(self){
self.containerArray = [NSMutableArray array];
self.currentIndex = 0;
}
return self;
}
- (void) pushData:(id)data
{
self.containerArray[self.currentIndex] = data;
self.currentIndex++;
}
- (id) popData
{
NSUInteger idx = self.currentIndex;
self.currentIndex--;
self.currentIndex = self.currentIndex < 0 ? 0 : self.currentIndex;
return self.cointainerArray[idx];
}
@end
// Model
// OAStackWithDictionary.m
@implementation OAStackWithDictionary : NSObject <OAStack>
@property (nonatomic, strong) NSMutableDictionary *containerDictionary;
@property (nonatomic, assign) NSUInteger currentIndex;
- (id)init
{
self = [[super alloc] init];
if(self){
self.containerDictionary = [NSMutableDictionary dictionary];
self.currentIndex = 0;
}
return self;
}
- (void) pushData:(id)data
{
[self.containerDictionary setObject:data forKey:[NSString stringWithFormat: @"%d", self.currentIndex]];
self.currentIndex++;
}
- (id) popData
{
NSUInteger idx = self.currentIndex;
self.currentIndex--;
self.currentIndex = self.currentIndex < 0 ? 0 : self.currentIndex;
return [self.containerDictionary objectForKey:[NSString stringWithFormat: @"%d", idx]];
}
- (void) purgeStack
{
self.currentIndex = 0;
self.containerDictionary = [NSMutableDictionary dictionary];
}
@end
// Model
// OAStackHelper.m
@implementation OAStackHelper : NSObject
+ (id<OAStack>) createArrayBasedStack
{
return [[OAStackWithArray alloc] init];
}
+ (id<OAStack>) createDictionaryBasedStack
{
return [[OAStackWithDictionary alloc] init];
}
@end
// Controller
// OAStackViewController.m
@implementation OAStackViewController : UIViewController
@property (nonatomic, strong) id<OAStack> stack;
- (void) viewDidLoad
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *stackType = [defaults objectForKey:@"stackType"];
if([stackType isEqualToString:@"ARRAY"]){
self.stack = [OAStackHelper createArrayBasedStack];
}
else if ([stackType isEqualToString:@"DICT"]){
self.stack = [OAStackHelper createDictionaryBasedStack];
}
}
- (IBAction) pushDataToStack:(id)sender
{
UIButton *addButton = (UIButton*)sender;
[self.stack pushData:addButton.tag];
}
- (IBAction) popDataFromStack:(id)sender
{
id data = [self.stack popData];
}
- (IBAction) clearData:(id)sender
{
if(self.stack respondsToSelector:@selector(purgeStack)){
[self.stack purgeStack];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment