I have a factory-type class that creates its products based on the traits of another object. This means that I need a reference to the input object somewhere. I am planning to either:
A) Define the input object as a property and set it in a custom init method. So the factory’s owner calls “initWithObject:”, then calls “createProduct”.
B) Define the factory’s creation methods so that they take in the input object as an argument. So the factory’s owner inits normally and then calls “createProductWithObject:”.
All else equal, is one of these methods preferable to the other from an overall design standpoint? Method A makes things simpler for me since I don’t have to make every method accept an input, but I’d like to be sure that I’m not overlooking anything.
Thanks!
I think it completely depends on how you intend to use your factory class. If this were a pizza factory, you might want to use option A in a scenario where the style of all pizzas (the products) depends on the initial value.
On the other hand, the individual pizzas can depend on something that other pizzas from the same factory don’t.
In this case, both options are being used. You’ll just need to decide what makes the most sense for your needs.