Re: Global variable for whole application.
Re: Global variable for whole application.
- Subject: Re: Global variable for whole application.
- From: Uli Kusterer <email@hidden>
- Date: Fri, 20 Apr 2007 09:29:16 +0200
Am 20.04.2007 um 01:54 schrieb Brian Willoughby:
Uli's approach is also good, and would be necessary for a library
of code that might be reused in more than one application. Without
multiple inheritance, it would be unwieldy for every framework to
subclass or categorize NSApplication, so it is better in those
cases to use a singleton.
That's exactly why I do it. Object-Oriented programming promises to
make code reusable. With most OOP languages, you still write so much
code over and over again. Cocoa and the whole structure of Objective
C are designed in a way that, if you just apply some discipline when
it comes to encapsulation, you actually *can* write code that is
generic enough to be reusable, while still concrete enough to be
easily understood.
You don't decide "now I need a reusable class, so I'll write it as
a separate piece". Rather, you write all code as if you wanted to
reuse it, and then when the second time comes you find yourself doing
something similar, you remember you already wrote that code and just
drop it in, maybe with a few adjustments.
Don't be paranoid about globals: Globals are a Good Thing(tm), it's
just that people used to use them to not have to pass around
parameters, and then changed them from two dozen places. And using a
global is definitely not as much a mistake as violating encapsulation
or needlessly establishing tight coupling between unrelated classes.
If you attach all your globals to NSApplication, that is tight
coupling that isn't necessary.
Cocoa is very nice in this regard: Where you used to have two dozen
statements in StartUp() and ShutDown() methods of your application
class, these days you have completely distinct classes that all
listen to NSApplicationDidFinishLaunching and
NSApplicationWillTerminate notifications. You just drop them into
your project, instantiate them in a NIB and they're there and they work.
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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