Contents
Where do you put factory methods?
Factory Method lets a class defer instantiation to subclasses….It is good idea to use factory methods inside object when:
- Object’s class doesn’t know what exact sub-classes it have to create.
- Object’s class is designed so that objects it creates were specified by sub-classes.
Does factory pattern need interface?
If your factory only serves to keep your callers away from direct invocations of the target constructor, you do not need the factory interface. You can always apply a design pattern in the way you like. There is nothing wrong with deleting an interface.
What is factory interface?
The factory design pattern says that define an interface ( A java interface or an abstract class) and let the subclasses decide which object to instantiate. The factory method in the interface lets a class defer the instantiation to one or more concrete subclasses.
When to use interface or abstract factory base?
The interface (or abstract factory base class, which is essentially the same as an interface in effect) is useful whenever the caller of the factory does not know the type of the factory. You provided the base for your own practical example, so I’ll add my explanation here why that’s not only useful when having a list of factories:
Do you need to decouple interface from factory?
If you don’t want your code to depend on the specific factory, you need an extra abstraction layer, provided by the interface, leading to “decoupling” the two parts. If you don’t mind your code binding to the specific factory implementation, you could always remove the interface. Hope I helped!
How are interfaces used in the design of classes?
Interfaces let you document that there are a set of classes that honor a specific set of polymorphic methods without having to create an inheritance hierarchy to link all of the classes. Without Interfaces the designer would have to create an artificial base class that they all inherited from.
Why is the factory method important in Java?
Factory Method lets a class defer instantiation to subclasses. So an Interface seems to be an important part of the Factory Design Pattern, but the only reason I found where its practical is when you make a collection in the main method.