Static Factory Method – Implementing in Base Class

design-patternsfactory-methodobject-orientedopen-closesolid

An increasingly popular definition of factory method is: a static
method of a class that returns an object of that class' type. But
unlike a constructor, the actual object it returns might be an
instance of a subclass.
enter image description here

From : https://sourcemaking.com/design_patterns/factory_method

Doesn't it violate OCP?

I have also seen this in production code.

What would be the correct implementation?

I can think of a separate Creator class with static method, which returns subclass objects.
My proposed solution is clearly not abstract factory as it doesn't create families of products, also it doesn't fit any of the GoF creational pattern. So I am skeptical about my solution.

Best Answer

According to the Open/Close Principe (OCP):

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

Let's examine your static factory architecture under this perspective:

  • Imagine that we want to extend our design horizontally with a subclass ProductThree. How could the makeProduct() return an object that could be one of the subclass without knowing the constructor of the subclasses ? I can think of two approaches:
    • You might have to change the method if it is implemented as a huge switch or chained if. This would infringe OCP.
    • You could think of a self registration mechanism that decouples Product from its subclasses (e.g. using a map / associative container mapping a class name / identifier with a class maker method- Java example). This would meet extensibility requirement and remain compliant with OCP.
  • Imagine that we want to extend our design vertically, by specializing ProductOne into ProductOneA and ProductOneB. To achieve this, you could not rely on sub class constructors in makeProduct(). You would have to use a makeProductOne(). This requires that the self registration is designed so to enable cascading through the hierachy. Although this could be challenging, it doesn't seem impossible.
  • Suppose that we could find a way to implement the self-registration requirement in the design. In this case you could completely seal the factory method, in compliance with OCP.

In conclusion, the concept of the static factory is compliant with OCP. Of course, specific implementation might infringe OCP, but only if they wouldn't take into consideration the additional requirements I mentioned above.

Note that using a factory class is a cleaner approach : it enforces separation of concerns (i.e. avoid that the factory knows about internals of the Product). But with regard to OCP, using a static method as explained above is an acceptable alternative.

I can't tell for other languages, but Andrei Alexandrescu demonstrated the implementation of a factory method in "Modern C++ design - Generic Programming and design patterns applied". It's full OCP using a map and registering/unregistering functions. The only difficulty he mentions it the type identifier that has to be provided to the factory. Of course, an enum would be an OCP issue, but he explored several alternatives using RTTI or a unique ID generator to circumvent this issue.

Related Topic