Re: 'Pseudo' Singleton in Objective C

2013-03-15 Thread Pax
Thank you to everyone whose helped me with this problem.  I now have the 
solution I needed, and (as usual) I've learned a whole lot that will be useful 
in the future.

As always, this is an incredibly useful resource!

On 15 Mar 2013, at 02:16, Seth Willits  wrote:

> On Mar 14, 2013, at 11:22 AM, Pax wrote:
> 
>> I don't really know how to describe what I'm trying to do except as a 
>> 'Pseudo' Singleton.  I have a class with an NSWindow, which displays 
>> information.  It is run by selecting an NSMenuItem called 'More 
>> Information…' 
>> 
>> My issue is that I only want one instance of the Information class to be 
>> loaded at a given time (and therefore only one information window on screen 
>> at a given time).  If the information window is already loaded and it is 
>> then reselected from the menu then the existing window needs to be brought 
>> to the front - nothing more.
>> 
>> I have currently resolved it by making my class a singleton…
> 
> I don't see anything to "resolve" so for the sake of clarification…
> 
> There's no reason to make your class a singleton just because you only want 
> one instance of it. Just don't create a second instance if you already have 
> one. Your "More Information" menu item is hooked up to some controller 
> already — presumably the application delegate. All that controller has to do 
> is maintain and reuse a reference to your InformationWindow.
> 
> 
> 
>> … but that's not the right answer because it means that I can't release 
>> information window…
> 
> 
> Right. This is the reason you don't want a singleton or "pseudo" singleton. 
> There's no fancy pattern needed:
> 
> 
> - (IBAction)showInformation:(id)sender
> {
>   if (!_infoWindowController) {   
>   _infoWindowController = [[InformationWindowController alloc] 
> init];
>   [[NSNotificationCenter defaultCenter] addObserver:self 
> selector:@selector(infoWindowWillClose:) name:NSWindowWillCloseNotification 
> object:_infoWindowController.window];
>   }
>   
>   [_infoWindowController showWindow:nil];
> }
> 
> 
> - (void)infoWindowWillClose:(NSNotification *)notification;
> {
>   [[NSNotificationCenter defaultCenter] removeObserver:self 
> name:NSWindowWillCloseNotification object:_infoWindowController.window];
>   [_infoWindowController autorelease];
>   _infoWindowController = nil;
> }
> 
> 
> --
> Seth Willits
> 
> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> https://lists.apple.com/mailman/options/cocoa-dev/45rpmlists%40googlemail.com
> 
> This email sent to 45rpmli...@googlemail.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: 'Pseudo' Singleton in Objective C

2013-03-14 Thread Oliver Fuchs
Hi,

Firstly, I'm assuming the more information button is the only location
which triggers displaying/redisplaying. 

if thats the case I would just store the instance of your class in a property 
and make sure it will be created only once. 

You can accomplish this by simply overwriting the getter method like this:

-(your class) foo {
 if (!_foo) {
_foo = [[yourclass alloc]init];
}
return _foo;
}

be careful about mem managment. above is good for a retain property!

best regards,
oliver


Von meinem iPhone gesendet

Am 14.03.2013 um 19:22 schrieb Pax <45rpmli...@googlemail.com>:

> I don't really know how to describe what I'm trying to do except as a 
> 'Pseudo' Singleton.  I have a class with an NSWindow, which displays 
> information.  It is run by selecting an NSMenuItem called 'More Information…' 
> 
> My issue is that I only want one instance of the Information class to be 
> loaded at a given time (and therefore only one information window on screen 
> at a given time).  If the information window is already loaded and it is then 
> reselected from the menu then the existing window needs to be brought to the 
> front - nothing more.
> 
> I have currently resolved it by making my class a singleton - but that's not 
> the right answer because it means that I can't release information window and 
> its class when it gets closed, resulting in a minor leak (only minor because, 
> as a singleton, the instance is only ever loaded once anyway).  Even so, this 
> is one leak too many - and I'be grateful if someone could tell me the correct 
> way to do what I am trying to do.
> 
> Currently, I am setting to be a singleton as follows:
> 
> static informationWindow *singletonManager = nil;
> 
> @implementation informationWindow
> 
> //Code to handle singleton
> + (id)singleton
> {
>@synchronized(self)
>{
>if(singletonManager == nil)
>singletonManager = [[super allocWithZone:NULL] init];
>}
>return singletonManager;
> }
> 
> + (id)allocWithZone:(NSZone *)zone
> {
>return [[self singleton] retain];
> }
> 
> - (id)copyWithZone:(NSZone *)zone
> {
>return self;
> }
> 
> - (id)retain
> {
>return self;
> }
> 
> - (unsigned)retainCount
> {
>return UINT_MAX; 
> }
> 
> - (oneway void)release
> {
> 
> }
> 
> - (id)autorelease
> {
>return self;
> }
> 
> 
> I'm not using ARC.  All suggestions gratefully received!
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> https://lists.apple.com/mailman/options/cocoa-dev/info%40entwicklerfuchs.de
> 
> This email sent to i...@entwicklerfuchs.de

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: 'Pseudo' Singleton in Objective C

2013-03-14 Thread Seth Willits
On Mar 14, 2013, at 11:22 AM, Pax wrote:

> I don't really know how to describe what I'm trying to do except as a 
> 'Pseudo' Singleton.  I have a class with an NSWindow, which displays 
> information.  It is run by selecting an NSMenuItem called 'More Information…' 
> 
> My issue is that I only want one instance of the Information class to be 
> loaded at a given time (and therefore only one information window on screen 
> at a given time).  If the information window is already loaded and it is then 
> reselected from the menu then the existing window needs to be brought to the 
> front - nothing more.
> 
> I have currently resolved it by making my class a singleton…

I don't see anything to "resolve" so for the sake of clarification…

There's no reason to make your class a singleton just because you only want one 
instance of it. Just don't create a second instance if you already have one. 
Your "More Information" menu item is hooked up to some controller already — 
presumably the application delegate. All that controller has to do is maintain 
and reuse a reference to your InformationWindow.



> … but that's not the right answer because it means that I can't release 
> information window…


Right. This is the reason you don't want a singleton or "pseudo" singleton. 
There's no fancy pattern needed:


- (IBAction)showInformation:(id)sender
{
if (!_infoWindowController) {   
_infoWindowController = [[InformationWindowController alloc] 
init];
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(infoWindowWillClose:) name:NSWindowWillCloseNotification 
object:_infoWindowController.window];
}

[_infoWindowController showWindow:nil];
}


- (void)infoWindowWillClose:(NSNotification *)notification;
{
[[NSNotificationCenter defaultCenter] removeObserver:self 
name:NSWindowWillCloseNotification object:_infoWindowController.window];
[_infoWindowController autorelease];
_infoWindowController = nil;
}


--
Seth Willits



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: 'Pseudo' Singleton in Objective C

2013-03-14 Thread Alex Zavatone
Matt Galloway's example is the one to run with IMO.

http://www.galloway.me.uk/tutorials/singleton-classes/

You're pretty close though.  Matt's examples and explanation should put you in 
the right direction.

Any reason why you're not using ARC?

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: 'Pseudo' Singleton in Objective C

2013-03-14 Thread Jens Alfke

On Mar 14, 2013, at 11:22 AM, Pax <45rpmli...@googlemail.com> wrote:

> My issue is that I only want one instance of the Information class to be 
> loaded at a given time (and therefore only one information window on screen 
> at a given time).  If the information window is already loaded and it is then 
> reselected from the menu then the existing window needs to be brought to the 
> front - nothing more.
> I have currently resolved it by making my class a singleton - but that's not 
> the right answer because it means that I can't release information window and 
> its class when it gets closed, resulting in a minor leak (only minor because, 
> as a singleton, the instance is only ever loaded once anyway).

IMHO the full belt-and-suspenders implementation of a singleton you’ve chosen — 
overriding alloc, retain, release, etc. — is overkill. I just implement 
singletons using a +sharedInstance method that looks like your +singleton. If 
I’m feeling paranoid I put an assertion into the -init method that the 
singleton doesn’t yet exist.

An easy way to make a sort of “weak singleton” like what you want, is to (a) 
make the static singleton pointer variable non-retained, and (b) nil it out in 
the -dealloc method. That way your singleton object isn’t kept around 
permanently, and when it goes away it cleans up so that the next request will 
instantiate a new one. (Of course you do have to ensure that *something* 
retains your singleton object as long as it needs to stay around, or else it’ll 
get dealloced very soon after being created and lose its state.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com