DEV Community

loading...
Cover image for Introduction of the Factory Method Pattern

Introduction of the Factory Method Pattern

onurdesk
Onurdesk is a technical platform based primarily on Spring, Java and Nodejs ecosystem app creation. We write tutorials and manuals concentrating extensively on realistic and real-life usage cases.
・1 min read

A Factory Method Pattern produces goods, and a software factory produces objects. Usually, object creation in Java takes place like so:

SomeClass someClassObject = new SomeClass();
Enter fullscreen mode Exit fullscreen mode

The problem with the above approach is that the code using the SomeClass ‘s object, suddenly now becomes dependent on the concrete implementation of SomeClass. There’s nothing wrong with using new to create objects but it comes with the baggage of tightly coupling our code to the concrete implementation class, which is a violation of code to an interface and not to an implementation.

Formally, the factory method is defined as providing an interface for object creation but delegating the actual instantiation of objects to subclasses.

Click here for the full tutorial

Discussion (0)