I am not sure which section this should be in so I am posting it under the "Everything else" section.
Basically around 2 months back when I first started writing my 1st game, I was searching around the web on how to use Singletons to save important information (e.g gameState) and use them across different scenes.
I didn't want to populate the AppDelegate with more variables and such that will only complicate its role in the whole of the application.
Then I came across Matt Gallagher's article on Singletons, AppDelegates and top-level data.
Matt's article made me understand how Singletons in Cocoa work. However, I needed to save multiple types of data which were implemented as different classes. Coming from a PHP background, I of course turned to the registry pattern which is commonly utilized in PHP.
However, i was not able to find any good tutorials/articles on the registry pattern in Cocoa~
Therefore, based on my own understanding on how to implement the registry pattern in PHP, i wrote two classes as below:
Registry.h
#import <Foundation/Foundation.h>
@interface Registry : NSObject {
NSMutableDictionary *objects;
}
@property (nonatomic,retain) NSMutableDictionary *objects;
+(Registry *) sharedRegistry;
-(void)set :(NSObject *)value :(NSString *)key;
-(id)get :(NSString *)key;
-(void)remove: (NSString *)key;
@end
Registry.m
#import "Registry.h"
#import "SynthesizeSingleton.h"
@implementation Registry
SYNTHESIZE_SINGLETON_FOR_CLASS(Registry); //make Registry a singleton
@synthesize objects = objects;
-(id) init {
if (self == [super init]) { objects = [[NSMutableDictionary alloc] init]; }
return self;
}
-(void) set :(NSObject *)value :(NSString *)key { [objects setValue:value forKey:key]; }
-(id) get :(NSString *)key {
if ([objects valueForKey:key]!=nil) { return [objects valueForKey:key]; }
else { return nil; }
}
-(void) remove:(NSString *)key { [objects removeObjectForKey:key]; }
@end
Please note that this implementation of the registry pattern requires the usage of Matt's singleton implementation. Kudos to him for it!
And to use the registry, all you have to do is to import it and set an object to the registry singleton instance like so:
#import "Registry.h"
Class *class = [[Class alloc] init];
[[Registry sharedRegistry] set:class :@"Class"];
To get back the class instance stored inside the registry, import the registry anywhere and get the object like so:
#import "Registry.h"
if ([[Registry sharedRegistry] get:@"Class"] != nil) { Class *class = [[Registry sharedRegistry] get:@"Class"]; }
I know that each and every one of us has our own method of saving important information and some people don't recommend the singleton pattern (or registry).
I won't recommend storing many things in the registry because it is a static instance and it takes up memory in a long term sense. For myself, I only store things that should really leverage on the registry like a PersistentStoreController for core data functionality.
I hope this simple implementation of the registry pattern would be of help to anyone who has faced the same situation (e.g passing variables between scenes) as me before.