The Factory pattern is not an OCP violator.
Depending on how you perform Complex behavior further.
If Complex is required to support the creation of new types of Complex objects, and you decide to modify Complex by adding new fromX methods to support them, this means that Complex becomes an OCP violator, because Complex must be opened again for change:
class Complex { public static Complex fromCartesian(double real, double imag) { return new Complex(real, imag); } public static Complex fromPolar(double modulus, double angle) { return new Complex(modulus * cos(angle), modulus * sin(angle)); }
You can transfer this problem to a text file or a properties file of some kind to free yourself from the need to change the java class, but this does not stop you from writing additional logic in this area, a solution to support the new Complex types.
Depending on the use of Complex in your design (how do the different types differ? How do you use them?), There are several alternative options that may apply well.
One such OCP friendly alternative is to subclass Complex to provide additional Factory methods. A subclass is the simplest illustration of how Complex expands but does not change.
Another OCP friendly alternative to changing Complex in this case is the Drawing Decorator . Continuously decorating Complex ability to create new Complex options for OCP, because Complex does not change, but expands, wrapping it with new features.
A third option might be to modify the Complex structure so that it is computed by the composition. This will give you the opportunity to use a strategy template to distinguish between different types of Complex behavior.
The thing about the Factory pattern is that it helps the context encode OCP . You can use one of the above methods to stay on the right side of OCP with their Factory class, but your colleagues, probably to look at the graph of the object, ask the wisdom of having an object graph over one Factory and simplify it back to one Factory, which will return you to the first example.
In such cases, instead of trying to bend your implementation of the Factory pattern to comply with SOLID principles, consider why you're using it at all.