Skip to content

Instantly share code, notes, and snippets.

@sora0077
Created February 1, 2013 11:48
Show Gist options
  • Save sora0077/4690864 to your computer and use it in GitHub Desktop.
Save sora0077/4690864 to your computer and use it in GitHub Desktop.
同じ処理をテンプレート化
#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
#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