Factory Method Pattern – Design Patterns (ep 4)
Why Stack Overflow recommends it
Stack Overflow users link it when explaining or illustrating the Factory Method pattern and how it differs from simple factory and abstract factory.
Recommended by 2 people across 2 different months, 2017 to 2021.
What it is
A 27-minute episode in Christopher Okhravi's Design Patterns series for object-oriented languages, covering the Factory Method pattern. It walks through the pattern's structure, motivation and relation to creator and product classes, drawing on the Gang of Four book and Head First Design Patterns.
What people said
Recommends user7941334 · 2017-06 · Stack Overflow · on “Polymorphism (C# .NET)”
Since you don't know which animal will be created at run-time, then you should use an animal factory, e.g use the "simple factory" or the "factory method" pattern. It let's you pass the animal type ('Cat', or 'Dog', or etc) and creates the corresponding animal object for you. You can then call the `letsHear()` function, passing that new created instance to it. See: THIS [LINK] and THIS [this video] . Good luck!
Recommends Daniel · 2020-04 · Stack Overflow · on “Scenarios where we can use factory pattern in real projects”
Factory Method Pattern is used when • a class can't anticipate the class of objects it must create. • a class wants its subclasses to specify the objects it creates. • classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate. The Factory Method Pattern is useful if you’ve only got one concrete creator because you are decoupling the implementation of the product from its use. If you add additional products or change a product’s implementation, it will not affect your Creator (because the Creator is not tightly coupled to any ConcreteProduct). I think this video [this video] explains it really good.
Cites user7941334 · 2017-06 · Stack Overflow · on “Creating subclass instances from base class constructor”
Your question is: Inside a class, can I create instances of other classes? Well, theoretical and technical, you can easily receive the answer "yes". But the real answer is... "no". Because, if you do it: you are tightly coupling the class in which the instances are created ( Car ) to the classes from which the instances are created ( Engine , Wheels , FuelTank ). This way, a car will always must have a fuel tank, even though there could be implemented an electrical power only solution. the car will not only have the responsibility to go on a road, but to create its car parts by itself, too. And this is somehow... "Transformers"-like. But, if you want to correctly learn and apply OOP, then th
Cites bcperth · 2018-07 · Stack Overflow · on “Factory method pattern uses inheritance while the abstract factory pattern uses composition how?”
This is a revised reply after some thought. Factory Method : Usually, createObject() is a method of the Creator object. Abstract Factory : Usually the Factory Object is an attribute of the Creator object. Assume now that createObject() and Factory Object belong to their respective Creator. Factory Method : createObject() may change in sub-classes of the Creator. This happens by changing the implementation of creatObject() via inheritance . Abstract Factory : Factory Object may also change in sub-classes of the Creator. This change however happens by replacing one Factory Object with a different Factory Object ie the Creator object is changed by composition . In your demo creatObject() and Fa
Cites Melvin Winthagen · 2019-04 · Stack Overflow · on “Access problems using the Factory Pattern on C#”
In your code you are calling the abstract class directly. Perhaps doing Cube cube = new Cube(); instead of GeometricFigure cube = new Cube(); would make a difference. The following links also provide a lot of useful information on the factory pattern: [LINK] [this video] Hope any of this helps, and if not, would love to try to help you further!
Cites Enlico · 2021-02 · Stack Overflow · on “Why is the Factory pattern not needed in Haskell? And how are the needs that pattern addresses in OOP addressed in Haskell?”
I've read this question [LINK] about the Abstract factory pattern , but the only answer to it tries to emulate in Haskell what one would in OOP languages (although the foreword is along the lines you don't need it in Haskell ). On the other hand, my intention is not really to force an OOP-specific pattern on a Functional language as Haskell. Quite the opposite, I'd like to understand how Haskell addresses the needs that in OOP are addressed via the Factory pattern . I have the feeling that even these needs might not make sense in Haskell in the first place, but I can't formulate the question any better. My understanding of the structure of the factory pattern (based on this video [this video] whi