lunar
lunar

Reputation: 1232

Is this a factory method?

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?

Upvotes: 1

Views: 140

Answers (2)

nullpotent
nullpotent

Reputation: 9260

It doesn't mean that. Interface in the context of interaction, control (creation) and not the Java's interface

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564681

The Factory method pattern does not necessarily have anything to do with interfaces (in terms of a language's interface keyword 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 Room is created, with subclasses of the main type creating different concrete types of Room instances. This is still using the Factory method pattern, even though there is no "interface" involved.

My question is if I "substitute" the interface with a parent class would it still be a factory method?

Yes - it would still be a factory method.

Upvotes: 5

Related Questions