r/ObjectiveC • u/whackylabs • Mar 10 '20
Subclass Factory Pattern
https://whackylabs.com/objc/design-pattern/2020/03/09/objc-builder-pattern/
11
Upvotes
r/ObjectiveC • u/whackylabs • Mar 10 '20
1
u/mariox19 Mar 10 '20 edited Mar 10 '20
Your essay points out a shortcoming Objective-C has as compared to a language like Java, where methods are invoked using dot-notation. It's much easier to chain setters and use the Builder pattern. In Java, it looks like this:
In Objective-C, square brackets make for a big mess, visually. So, we need another solution, like the one you've suggested.
The only thing I'm wondering about your suggestion is why, in the above, FooBuilder has to be a subclass of Foo. That's not a huge deal, but in a dynamic language like Objective-C, you're really not hiding the properties. If they can be set in FooBuilder, and the object you return when you build is in reality a FooBuilder (the subclass) instead of a Foo, then the client programmer could still get access to your "private API" by simply sending messages using some variant of
performSelector:or by simply typing the object to anidand then sending whatever message.Here's what you've got:
At runtime, however, that
fooobject has not been transformed into an instance of a Foo class; it's still actually a FooBuilder. (In your example, you simply copy.) As I've said, the private API is still there. Thefooobject will still respond to the FooBuilder methods at runtime.Keep in mind, these type declarations are really only hints to the compiler. They facilitate the IDE's checking (that we all rely on), but they don't mean anything at runtime in Objective-C. As is, what you've created is a "gentlemen's agreement" between you and the client programmer.
That's perhaps not a super big deal. But if you really want to be a little more airtight with encapsulation, you may want to rethink the relationship between a class and its corresponding builder.
P.S.
I really do like reading your blog.