— boreal-kiss.net

Archive
October, 2011 Monthly archive

Why subclassing? Because I’m bored by preparing a bare-bone singleton from scratch.

+[NSObject initialize] is invoked only once in a thread-safe manner before a class object receives any message (for more detail see NSObject Class Reference). Therefore this method could be a good place for a singleton instance to be created.

Here is my singleton class. I won’t show the complete implementation here. To be robust you should control methods concerning allocation such as alloc, init, copy, etc. However these are not of importance here. To check these visit the following site:

//BKSingleton.h
 
#import <Foundation/Foundation.h>
 
@interface BKSingleton : NSObject{
 
}
 
/**
 * Returns a singleton instance.
 */
+(id)sharedInstance;
 
@end
//BKSingleton.m
 
#import "BKSingleton.h"
 
static NSMutableDictionary *_dictionary = nil;
 
@implementation BKSingleton
 
+(id)sharedInstance{
	NSString *className = NSStringFromClass(self);
	return [_dictionary objectForKey:className];
}
 
+(void)initialize{
	if (!_dictionary){
		_dictionary = [[NSMutableDictionary dictionary] retain];
	}
 
	NSString *className = NSStringFromClass(self);
 
	if (![_dictionary objectForKey:className]){
		[_dictionary setObject:[[[self alloc] init] autorelease] forKey:className];
	}
}
 
@end

When a subclass of this class is first received a message, the initialize method of this class is invoked (i.e., +[BKSingleton initialize]) unless this is overridden in the subclass.

The first trick here is that there is no hard-coded class names inside the initialize method (e.g., something like [MyClass alloc]). Thanks to this even the subclass is received its superclass’s initialize method, it is always instantiated by the correct class.

The second trick is that the instance of a subclass is stored in a static class dictionary associated with its class name if there is not such an object with the same key found in the dictionary. Doing this ensures that the dictionary holds a unique instance for each subclass.

Let’s see an example. There are two subclasses inherited from BKSingleton. They have nothing newly implemented.

//Singleton1
 
#import "BKSingleton.h"
 
@interface Singleton1 : BKSingleton{
 
}
 
@end
 
@implementation Singleton1
 
@end
//Singleton2
 
#import "BKSingleton.h"
 
@interface Singleton2 : BKSingleton{
 
}
 
@end
 
@implementation Singleton2
 
@end

Then run the following code.

//main.m
 
#import <Foundation/Foundation.h>
#import "BKSingleton.h"
#import "Singleton1.h"
#import "Singleton2.h"
 
int main(int argc, char *argv[]) {
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
	NSLog(@"BKSingleton: %@", [BKSingleton sharedInstance]);
	NSLog(@"Singleton1: %@", [Singleton1 sharedInstance]);
	NSLog(@"Singleton2: %@", [Singleton2 sharedInstance]);
 
	//Once again
	NSLog(@"BKSingleton: %@", [BKSingleton sharedInstance]);
	NSLog(@"Singleton1: %@", [Singleton1 sharedInstance]);
	NSLog(@"Singleton2: %@", [Singleton2 sharedInstance]);
 
	[pool release];
	return 0;
}

The output is the following, ensuring they are al unique, i.e., singletons.

Test[1153:207] BKSingleton: <BKSingleton: 0xe0a880>
Test[1153:207] Singleton1: <Singleton1: 0xe08d20>
Test[1153:207] Singleton2: <Singleton2: 0xe0b620>
Test[1153:207] BKSingleton: <BKSingleton: 0xe0a880>
Test[1153:207] Singleton1: <Singleton1: 0xe08d20>
Test[1153:207] Singleton2: <Singleton2: 0xe0b620>

Related links

Read More

wingen concept image

WinGen is a utility plugin for Goole Chrome. It offers the following functions for easier web browsing specifically concerning hyperlinks:

Function 1

WinGen navigates a hyperlink in a new window without using the context menu (i.e., control + click). As a Mac user, when you want to open a new window for links on a web page, you need to click the control key to popup the context menu to choose “open in new window”. This requires to use your both hands. With WinGen simply clicking the link and hold for a while navigates that page in a new window. Your another hand will be now free. Keep a cup of coffee.

Function 2

WinGen could disable forced new window specified by target=”_blank” attribution. Have you been bothered by lots of auto new window creation within one web site? With WinGen clicking such links won’t popup new windows.

Function 3

As a optional function, WinGen could disable hyperlinks themselves. Have you needed to copy some parts of the text within a hyperlink? I’m sure copying all the text from the hyperklink is easy but trying partial copying always navigates you into a different page for that link. With WinGen when you control+click on a link, WinGen gives you enable/disable hyperlink options (I’m sorry this requires the context menu. You need the both hands).

Let me show an example. I want to copy the word “spectator” from the link “What a spectator threw at Tiger Woods” (from today’s yahoo news).

wingen disabling hyperlinks example 1

First of all I turn on this function from the WinGen option page (the function is off by default).

wingen disable hyperlinks

Control+clicking on the link popups the WinGen menu. Then I select “disable hyperlink”.

wingen disabling hyperlinks example 2

Now the link is just a text so I can copy the word “spectator”. I could also restore the link by “enable hyperlink” after that.

wingen disabling hyperlinks example 3

Related links

Read More