Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjames white
78,399 PointsAttempted summary clarification for class factory methods in iOS..
Just be clear (and put it precisely as possible):
Objective-C does not have constructor methods that are available in other programming languages.
Thus Factory methods are essentially Objective C's constructor methods.
..and per the iOS docs:
"Class factory methods are implemented by a class as a convenience for clients.
They combine allocation and initialization in one step and return the created object.
However, the client receiving this object does not own the object and
thus (per the object-ownership policy) is not responsible for releasing it."
Is this referring (maybe) to garbage collection?!
So, asking:
"How are factory methods compared to Singletons", you could say:
"A singleton usually offers a single method to return the single instance of the class.
It isn't strictly a factory method as it doesn't create a different instance each time it's called,
but it is (essentially) the same kind of thing"...huh?
Also considering the question of:
"When I should implement the factory method pattern vs an init overload?"
I found an this stackexchange thread:
Which said (succinctly)::
"Every class should have a designated initializer, i.e. an instance method whose name starts with "init" that leaves a newly allocated object in a valid state.
You can add other initializers that provide additional functionality, but these should call the designated initializer. You can also create factory methods when that's helpful."
Thus:
ā¢ always provide a designated initializer
ā¢ provide additional initializers and factory methods if you need or want them