Created
February 1, 2013 11:48
-
-
Save sora0077/4690864 to your computer and use it in GitHub Desktop.
同じ処理をテンプレート化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
typedef id(^STTemplateBlocks)(); | |
typedef void(^STTemplateBlocksVoid)(); | |
#define STTemplateExecute(instance, aKey, ...) \ | |
((STTemplateBlocks)[instance templateOf:aKey])(__VA_ARGS__) | |
#define STTemplateApply(instance, aKey, ...) \ | |
((STTemplateBlocksVoid)[instance templateOf:aKey])(__VA_ARGS__) | |
@interface STTemplate : NSObject | |
+ (id)appearance; | |
+ (id)logic; | |
- (void)addTemplate:(STTemplateBlocks)block forKey:(NSString *)aKey; | |
- (STTemplateBlocksVoid)templateOf:(NSString *)aKey; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "STTemplate.h" | |
@interface STTemplate () | |
@property (strong, nonatomic) NSMutableDictionary *templates; | |
@end | |
@implementation STTemplate | |
+ (id)appearance | |
{ | |
static id instance; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
instance = [[self alloc] init]; | |
}); | |
return instance; | |
} | |
+ (id)logic | |
{ | |
static id instance; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
instance = [[self alloc] init]; | |
}); | |
return instance; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
self.templates = [NSMutableDictionary dictionary]; | |
} | |
return self; | |
} | |
- (void)addTemplate:(STTemplateBlocks)block forKey:(NSString *)aKey | |
{ | |
NSAssert(block, @"block must be non null!"); | |
id inBlock = block(); | |
NSAssert(inBlock, @"block must be non null!"); | |
id copiedBlock = [inBlock copy]; | |
[self.templates setObject:copiedBlock forKey:aKey]; | |
} | |
- (STTemplateBlocksVoid)templateOf:(NSString *)aKey | |
{ | |
id block = [self.templates objectForKey:aKey]; | |
NSAssert(block, @"template is not registered!"); | |
return block; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment