2

I'm new to Design Patterns and am trying to learn how they typically look like. Right now I'm trying to understand the Factory Pattern, and I were wondering if my example is a typical Factory Pattern structure:

enter image description here

The ShapeFactory class uses the Shape-classes as dependencies(and are not instantiating them). Must the ShapeFactory instantiate the Shape-classes to be called a factory? Is this an accurate Factory Pattern diagram, or should the relation between the Shape-classes be associations instead?

Jesper
  • 2,044
  • 3
  • 21
  • 50
  • 1
    The arrow direction from Circle, Rectangle & Square to ShapeFactory should be other way round. – Santosh Nov 02 '17 at 07:41
  • 1
    Note that there is no single pattern named _Factory_. There are at least four patterns that include the word _Factory_, but none with a one-word name. – jaco0646 Nov 02 '17 at 22:34
  • See https://web.archive.org/web/20161027171211/http://corey.quickshiftconsulting.com/blog/first-post – Fuhrmanator Nov 03 '17 at 03:09
  • @Fuhrmanator Great guide for showing the factory patterns, which clearly shows that this isn't a Factroy method pattern. Would you say my example applies more to the Strategy Pattern? – Jesper Nov 03 '17 at 05:03
  • I have answered similar question in past. https://stackoverflow.com/questions/46384358/what-are-the-real-benefits-of-using-the-abstract-factory-in-the-following-exampl/46407888#46407888 – Kedar Tokekar Nov 07 '17 at 00:28
  • Probably this may also help - https://stackoverflow.com/questions/46630943/abstract-factory-pattern-unused-code/46637558#46637558 – Kedar Tokekar Nov 07 '17 at 00:31

2 Answers2

2

Your diagram represents the "Factory Method Pattern" but, slightly it is missing some important class or object. Looks like shape class is Concrete Creator class. It doesn't have Creator class.

Basically, factory method design pattern has four classes and objects are involved:

1) Product : It defines the interface of objects the factory method creates.

2) ConcreteProduct: Implements the product interface.

3) Creator: It declares the factory method, which return an object of type product.

4) ConcreteCreator: It overrides the factory method to return and instance of a ConcreteProduct

Below diagram with slightly modification into your given diagram that represents the complete Factory Method Pattern:

enter image description here

Gul Ershad
  • 1,743
  • 2
  • 25
  • 32
  • Hmm yes, this seems more accurate as a Factory Pattern. You wouldn't by any chance know if my diagram is more similar to some other Design Pattern? I was thinking Strategy, but the ShapeFactories dependencies kinda ruins it – Jesper Nov 02 '17 at 19:11
  • Sorry, but a factory method is polymorphic. I don't see how that pattern applies here (you're adding a lot to the OP's design). At best it's a simple factory. https://stackoverflow.com/a/20859513/1168342 – Fuhrmanator Nov 03 '17 at 03:13
  • 1
    Factory Methods are usually implemented within a class hierarchy, though they may be implemented by classes that simply share a common interface. It is not directly polymorphic. It provides a way to make object creation polymorphic. Factory Methods are often designed into framework classes to make it easy for programmers to extend a framework’s functionality. Such extensions are commonly implemented by subclassing a framework class and overriding a Factory Method to return a specific object. – Gul Ershad Nov 03 '17 at 03:32
0

Okay, I think I got the answer. This could be a factory pattern with methods(e.g. CreateCircle(), CreateRectangle etc.) with hidden dependencies in those methods that instantiate the classes.

I thought that a dependency was only for dependency injections, but I guess there could be a dependency when instantiating a class inside a method.

Jesper
  • 2,044
  • 3
  • 21
  • 50
  • a good discusssion but outside this tag, is "is the factory pattern stil usefull ?" I mean with injection and all features to instantiate a class by its name do we still need a pattern to instantiate object ? – granier Nov 01 '17 at 21:22