• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How should my controller objects reference eachother?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How should my controller objects reference eachother?


  • Subject: Re: How should my controller objects reference eachother?
  • From: Ryan Britton <email@hidden>
  • Date: Mon, 23 Jul 2007 17:36:31 -0700

I don't know whether your project is document-based or not, but in one that isn't, I typically use the Singleton design pattern with global controller objects rather than maintaining references inside the program.

A few Cocoa classes that use this approach:
NSFileManager
NSUserDefaults
NSWorkspace
NSApplication

Hopefully this isn't too large for the list, but the following is the class I subclass with my singleton controllers.

static NSMutableDictionary *singletons = nil;
static NSRecursiveLock *singletonLock = nil;

@implementation MSSingleton

+ (void)initialize
{
@synchronized(self)
{
if (!singletons) singletons = [[NSMutableDictionary dictionary] retain];
if (!singletonLock) singletonLock = [[NSRecursiveLock alloc] init];
}
}


+ (id)singleton
{
	//Find a singleton if it exists, otherwise make it
	[singletonLock lock];
	NSString *className = [self className];
	id result = [singletons objectForKey:className];
	if (!result)
	{
		result = [[[self class] alloc] init];
		[singletons setObject:result forKey:className];
	}
	[singletonLock unlock];

	return result;
}

+ (BOOL)isInitialized
{
	[singletonLock lock];
	NSString *className = [self className];
	id result = [singletons objectForKey:className];
	BOOL isInitialized = (result != nil);
	[singletonLock unlock];
	return isInitialized;
}

+ (id)alloc
{
	id result;

	[singletonLock lock];
	if ([[self class] isInitialized])
	{
		result = [[self class] singleton];
	}
	else
	{
		result = [super alloc];
	}
	[singletonLock unlock];

	return result;
}

- (id)init
{
	id result;

	[singletonLock lock];
	if ([[self class] isInitialized])
	{
		result = [[self class] singleton];
	}
	else if (result = [super init])
	{
		[self initializeSingleton];
		[singletons setObject:result forKey:[result className]];
	}
	[singletonLock unlock];

	return result;
}

- (void)initializeSingleton
{
	//No-op, override in subclasses
}

- (oneway void)release
{
	//Do nothing
}

- (id)autorelease
{
	return self;
}

- (id)retain
{
	return self;
}

@end



On Jul 23, 2007, at 5:23 PM, Carter R. Harrison wrote:

Good evening everybody,

I'm in the middle of my first big Cocoa project. I've been working hard to keep all of my various controller objects as concise as possible. As part of this effort, I've begun to realize that my controllers need to reference each other so that they can easily send messages to one another. What's the best way to do this?

My first inclination was to have my application's delegate maintain references to each controller object, and then each of my controller objects could get a reference to one another by getting it from the app delegate via accessor methods that I would create. This doesn't seem to be working out - I keep getting warnings from the compiler that these accessor methods don't exist.

Here's an example. Let's say I have three controllers: FileController (extends NSArrayController), HTTPSubsystem (extends NSObject), GUIController (extends NSObject). All three controllers need to reference each-other. In the example below if I call "http = [[NSApp delegate] httpSubsystem];" from within FileController.m, I receive the following compiler message: "no '-httpSubsystem' method found. I hope all this makes sense, and thanks very much in advance.

AppDelegate.h

@interface AppDelegate : NSObject
{
IBOutlet FileController *fileController;
IBOutlet GUIController *guiController;
HTTPSubsystem *http; //This controller is alloc'd when the AppDelegate awakes from nib.
}


// Accessor methods.
- (FileController *)fileController;		//returns fileController
- (GUIController *)guiController;		//returns guiController
- (HTTPSubsystem *)httpSubsystem;	//returns http


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >How should my controller objects reference eachother? (From: "Carter R. Harrison" <email@hidden>)

  • Prev by Date: How should my controller objects reference eachother?
  • Next by Date: How should my controller objects reference eachother?
  • Previous by thread: How should my controller objects reference eachother?
  • Next by thread: Re: How should my controller objects reference eachother?
  • Index(es):
    • Date
    • Thread