Good post and all, but could you show us how to create a factory which doesn’t violate the open/closed principle? (I.e. a factory which when adding a third let’s say “PrintDialogue”, or whatever, a factory which doesn’t require modifying the factory’s code just to add another implementation) Maybe something that uses metadata from the children to determine what to do for instance? Thnx.
There's a more advanced form of the Factory Pattern which makes it easier to stay aligned with the Open Closed Principle, called the Abstract Factory Pattern: en.wikipedia.org/wiki/Abstract_fac...
However, in the implementation above, while the factory itself violates the open/closed principle, the Dialog itself doesn't and the factory aids you in adhering to it.
You implement the Dialog, thus extending it, rather than modifying it and you use the abstract to interface with the concrete implementation returned from the Factory.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Good post and all, but could you show us how to create a factory which doesn’t violate the open/closed principle? (I.e. a factory which when adding a third let’s say “PrintDialogue”, or whatever, a factory which doesn’t require modifying the factory’s code just to add another implementation) Maybe something that uses metadata from the children to determine what to do for instance? Thnx.
There's a more advanced form of the Factory Pattern which makes it easier to stay aligned with the Open Closed Principle, called the Abstract Factory Pattern: en.wikipedia.org/wiki/Abstract_fac...
However, in the implementation above, while the factory itself violates the open/closed principle, the
Dialog
itself doesn't and the factory aids you in adhering to it.You implement the Dialog, thus
extending
it, rather thanmodifying
it and you use the abstract to interface with the concrete implementation returned from the Factory.