Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 10
    "Should I want to create a rectangle some new way, like from edges or given a width and an area, etc... I would need to continue expanding this factory class, which violates SOLID" - no, it does not. That's what "open for extension" is in the open closed principle. You change only a single class, adding new functionality, without having to change anything else in the software. Commented Feb 27 at 6:59
  • 1
    @zwoolli - Where will you write the logic to select the correct implementation of the factory interface? Commented Feb 27 at 7:04
  • 15
    Is there a better/cleaner way to go about creating instances dynamically using a factory? - yes, it is called KISS: don't introduce factories when you don't have a context from which you could derive requirements for the factory. As long as you don't have any context, the most simple and clean solution is to stay with constructors and no factory at all. Commented Feb 27 at 7:30
  • 8
    At least in the context of a Rectangle example, you're severely overthinking things. Your solution with multiple factories is in no way better because the caller must statically know which factory to select and which arguments to provide. There's nothing dynamic here (except the type checking, because the FactoryInput interface defeats static type checks). All of these factory functions could have been part of the original RectangleFactory, or even just static methods on the Rectangle class itself. Some systems do need more flexible designs, but you haven't explained that need here. Commented Feb 27 at 7:32
  • 5
    @zwoolli You cannot extend behaviour without modifying source code. The question is, whether a modification of a given piece of source has sideeffects where you do not expect them. Open for extension means, that you can extend behaviour (e.g. by adding a new factory method.) Closed for modification means, that such a change does not trigger further changes in other classes. Commented Feb 28 at 5:42