Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > need some explenations...

need some explenations...
Thread Tools
Addicted to MacNN
Join Date: Mar 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Jul 2, 2002, 03:02 AM
 
okay, I'm digging my way into Obj-C and cocoa this summer and I'm reading up all I can on the language and I came across this.

"Objective-C uses + and - to differentiate between factory and instance methods, C++ uses static to specify a factory method."

And this almost puts down one of my questions about the language (the diff between a '+' & '-' method) but it raises another. I have seen functions that began with static before but never knew what was different about them.

So I guess my question is, what is a factory method? And how does it differ from a normal method?
The spirit of resistance to government is so valuable on certain occasions, that I wish it always to be kept alive.
- Thomas Jefferson, 1787
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 2, 2002, 07:26 AM
 
Well, that isn't an entirely accurate distinction you're quoting there, I think.

In Objective-C, there is always one object from each class floating around--it's called the class object. This object doesn't have any instance variables, and it only has the methods you mark with a +, which are referred to as class methods. This class object is what you're sending messages to when you write something like [NSArray array]. Methods marked with a - are used by instances of the class, which are created by the class object.

Now that we've cleared that up, I can answer your question directly: A factory method is a method that instantiates objects--a common Obj-C example is [NSArray array]. They get their name from the fact they "make" objects like a factory. Since a class object is generally used for creating instances of that class, it's often considered to be a factory object. But actually, it can be used for more than just creating things (as with NSObject's +conformsToProtocol: method).

Oh, and a static variable is one that isn't destroyed when a function returns. It stays between calls.
I did a bit of digging and found <a href="http://home.twcny.rr.com/amantoan/cweb/statglob.htm" target="_blank">this page</a> if you want an example of what the "static" keyword does. But you'll probably want to read up on C if you plan on coding Obj-C, because this is the sort of thing Obj-C tutorials tend to assume you already know.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 2, 2002, 11:52 AM
 
Both static and factory methods have exactly one implementation which stays at the same memory location for the lifetime of your program. All other methods have one implementation per instance of the object, which can be at any location in memory.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Jul 2, 2002, 01:09 PM
 
Could we please get some more descriptive thread titles around here.

Here's an explanation:

The reason we need decriptive thread titles is so people know what a thread is about before clicking on it. For example, I have no idea what this thread is about--other than a plea for explanations. Therefore, I feel inclined to post this explanation of why useless thread titles suck. Hopefully, from my explanation, people will see that descriptive titles actually help make the forums more powerful by enabling those with the answers to your problems to find your post.
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
Addicted to MacNN
Join Date: Mar 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Jul 2, 2002, 03:56 PM
 
Thanks Chuck!
The spirit of resistance to government is so valuable on certain occasions, that I wish it always to be kept alive.
- Thomas Jefferson, 1787
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:56 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2