Factory pattern consist of classes that implement particular interface. Does it always have to be an interface? Would it still be a factory method when I have subclasses that inherit from other class not an interface?
Factory pattern consist of classes that implement particular interface. Does it always have to
Share
The Factory method pattern does not necessarily have anything to do with interfaces (in terms of a language’s
interfacekeyword or construct). You can have factory methods that create class instances, and may construct a subclass instead of the base class just as easily as if you’re using interfaces.For example, see this Wikipedia example. Here, a
Roomis created, with subclasses of the main type creating different concrete types ofRoominstances. This is still using the Factory method pattern, even though there is no “interface” involved.Yes – it would still be a factory method.