 |
 |
need some explenations...
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2001
Location: Seattle, WA
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
|
|
|
The spirit of resistance to government is so valuable on certain occasions, that I wish it always to be kept alive.
- Thomas Jefferson, 1787
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|