This is about the Factory Pattern. I am a little confused.
I saw implementations where the createInstance()
method is static and some implementations that are non-static.
Some say it's depending on "style" or "taste" and some say it does not. Wikipedia says that it should be non-static, and http://www.dofactory.com/Patterns/PatternFactory.aspx also says that it should be non-static, according to the Gang of Four.
My question is: does it depend on style & taste or does it violate the Factory Pattern if it's implemented the static way? What's right?
Specifically with the Factory pattern, no, there is no requirement that the factory methods be static. The essence of the pattern is that you have one object which is responsible for creating instances of another class.
A static factory method is a public static method on the object that returns a new instance of the object. These type of methods share the same benefits as the traditional factory method design pattern. This is especially useful for value objects that don't have a separate interface and implementation class.
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.
It depends, for instance you can have a factory that will only produce a certain amount of objects in which case there will be an associated state. For the most part factory methods can be static as long as they don't rely on any non-static variables (such as non-static globals and such) for their creation.
I'm very hesitant to categorize "instance versus static" as a matter of taste. This sort of implies that it's aesthetic like a favorite color or, more appropos, camelCase versus PascalCase.
Instance versus static is more a question of tradeoffs. With instance members of any kind, you get all of the benefits of polymorphism, since you can implement interfaces and inherit from other classes when you have instances and instance members. With statics, you do not get these benefits. Generally, static versus instance is a tradeoff for up-front simplicity versus downstream simplicity. Statics are easy because they are globally accessible and you don't have to consider things like "when should this be instantiated and by whom?" You don't have to pass them around with accessors/mutators or constructors, and your API looks cleaner. This makes up front reasoning easier. But, it makes maintenance and future implementations harder.
If you have a static method -- say a factory method in your case -- and you later want it to behave differently in certain situations, you're kind of hosed. You have to make a second method and copy and paste the functionality minus whatever you want to change, and then have clients figure it out. Or, worse, you expose a global variable and have clients set this before and after using your method, with the global telling the method how to behave.
If you had gone the instance route up front, this would be easy. You'd just inherit and override your initial factory method and provide the derived classes where you need the new functionality. You're not placing additional burden on client code and you're making almost no modifications to existing classes (open/closed principle).
My advice would be to do future you and/or other maintainers a favor and use the instance implementation. It's not a matter of what the Gang of Four or anyone else want or prefer - it's a matter of your own sanity in the face of code rot.
Static method doesn't violate the pattern but it goes against many other object oriented practices (inversion of control + dependency injection as one example) so using instances is better.
Edit:
I just got some badge for this answer but when I read it I could not believe my eyes. It is wrong when we strictly speak about GoF Factory method pattern and it deserves some correcting.
You can have static CreateInstance
method for creating instance of a type - there is nothing wrong about that - people often call it factory method but that is not what is called Factory Method pattern. Once you start putting logic into this method to create instances of different types depending on some condition you may be actually in need of Factory Method pattern described by GoF.
The point of GoF Factory method pattern is to replace conditional logic inside CreateInstance
with inheritance and polymorphism and thus it cannot be static. Factory method is an instance method - moreover it is virtual. Your base type has usually abstract CreateInstance
and conditional logic is replaced by inheritance tree where each subtype overrides CreateInstance
and creates just specific product for that subtype.
If its an abstract factory
then instance level is normal. And instance level functionality tends to be easier to mock and unit test than static
level
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With