Does the Factory method violate the Open / Closed principle?

Does Factory create a method template (not to be confused with Factory or Abstract Factory templates) Open / Closed principle ?

Update: To clarify, I mean a scenario in which a particular class has static Factory methods. For example (this is from the Wikipedia page on FMP):

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)); } private Complex(double a, double b) { //... } } 

Does the private constructor close the class of the subclass, i.e. extended?

Will the class be modified to support new Factory methods? For example, if a class that originally had only from the Carthusian and later from the Polar was necessary, is it necessary to modify the class to support this?

Are both of them Open / Closed broken?

+7
design-patterns factory-method open-closed-principle
source share
3 answers

No, it does not violate the Open / Closed principle at all.

Open / Closed means that you can change the way the system works without changing existing code. You can extend the code and use it in different ways, but the old code is still in tact and does not need to be re-tested.

The Factory method template will create an object of a different type based on the specified parameters. The Factory method really works well with the Open / Closed principle, if everything is done correctly. However, if you create a new class and then want the Factory method to create a new object of this type, you would have to change the Factory method.

Although, if you had some kind of configuration file or something similar that is read by the Factory method, you will not have to change the Factory method ... only the configuration file, which then determines which object will be created by the Factory method.

+3
source share

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)); } //class opened for modification public static Complex fromOtherMeans(String x , String y) { return new Complex(x, y); } } 

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.

+5
source share

Nope. From your Wikipedia link:

software objects (classes, modules, functions, etc.) must be open for extension, but closed for modification

Overriding the factory method is an extension. You create a new class. You do not change the existing class. You must substitute (through the configuration of your IoC container) a subclass for the original.

+2
source share

All Articles