I've recently worked on a reusable network service class for a service-aggregator iOS app. This class should retry a failed request if it was caused by expired user token. Plus, this class will be used between multiple contractors, who will create the aggregated services.
Request headers that will be used by this instance.
in `next` and failure in `error` event of returned `RACSignal` instance.
and refresh expired user token.
refresh current user token, and retry it once again.
Checks whether passed `error` caused by expired user token or not.
returned `RACSignal` if the process succeeds, and `error:` otherwise.
Returns network service for Pizza Delivery service.
Returns network service for Quick Laundry service.
Returns network service for Cab Finder service.
Yet, after revisiting the GoF book, I found that Factory pattern was meant to return subclasses instead of a the main class. Since I was returning the main class (AuthenticatedNetworkService), is the AuthenticatedNetworkServiceFactory could be considered as a Factory? Or was it just a Helper class?
There is no clear answer to your question, because the definition of a factory is not cast in stone.
Many people consider a factory to be anything that creates something for you so that you don't have to new it yourself. This is dumb in my opinion, but who am I to say that your factories are not true factories ?
The factory decides which particular subclass to instantiate (as Kilian Foth mentioned) so that you don't have to know the actual class of the object created.
The factory is an abstract class itself, so not only you don't have to know the actual class of the object created, but you don't even know the actual class of the factory that you are invoking.
So, according to these real benefits™ your factory is in fact a helper, but if you still go ahead and call it a factory I do not think anybody can blame you. By calling it a factory you are simply advertising the fact that "this thang instantiates stuff".
Not the answer you're looking for? Browse other questions tagged design-patterns ios swift-language factory or ask your own question.
Interface at the class or function level?
Could I put this package filtering mechanism in a factory?