6

I'm new to Objective-C programming, and creating various classes for an iOS application I'm working on.

When creating objects, it seems like many classes in the built-in frameworks use the "static factory method" pattern, like this:

MyObject* m = [MyObject objectWithName:@"foo" id:@7 description:@"bar"];

however, many classes also simply have overrides on init, like this:

MyObject* m = [[MyObject alloc] initWithName:@"foo" id:@7 description:@"bar"];

I can see that if I want to cover all my bases, I'd implement both, and have the objectWithName... method call initWithName..., however this seems quite tedious.

I was wondering - is there any style or guidance from around when I should implement the factory method pattern vs an init overload? I've googled for this, but have been unable to find anything (most likely because the terms are quite generic and google doesn't search well for them)

Any advice or opinions would be much appreciated

  • The first isn't really a factory method per se; it's just a convenience method for combining an init with an autorelease. – mipadi May 9 '14 at 22:03
2

From https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/ClassFactoryMethods/ClassFactoryMethods.html:

Factory methods can be more than a simple convenience. They can not only combine allocation and initialization, but the allocation can inform the initialization. As an example, let’s say you must initialize a collection object from a property-list file that encodes any number of elements for the collection (NSString objects, NSData objects, NSNumber objects, and so on). Before the factory method can know how much memory to allocate for the collection, it must read the file and parse the property list to determine how many elements there are and what object type these elements are.

Another purpose for a class factory method is to ensure that a certain class (NSWorkspace, for example) vends a singleton instance. Although an init... method could verify that only one instance exists at any one time in a program, it would require the prior allocation of a “raw” instance and then, in memory-managed code, would have to release that instance. A factory method, on the other hand, gives you a way to avoid blindly allocating memory for an object that you might not use.

  • After reading that I'm thinking... Add factory methods when you need to for efficiency (e.g. allocating custom amounts of memory like in the example), and then maybe for convenience if you feel like it? – Orion Edwards Apr 28 '14 at 1:15
  • I don't know if you would call it "efficiency," but yes, that's the basic idea. – Robert Harvey Apr 28 '14 at 2:27
2

I was wondering - is there any style or guidance from around when I should implement the factory method pattern vs an init overload?

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.

So that's the guideline:

  • always provide a designated initializer
  • provide additional initializers and factory methods if you need or want them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.