Pattern Name
Synopsis:
Factory pattern is one of most used design pattern in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Varying: subclass of object that is instantiated.
I would like to think about design patterns in terms of classes being as ‘people,’ and patterns are ways that people talk to each other.
So, to me the factory pattern is like a hiring agency. You’ve got someone that will need a variable number of workers. This person may know some info they need in the people they hire, but that’s it.
So, when they need a new employee, they call the hiring agency and tell them what they need. Now, to actually hire someone, you need to know a lot of stuff - benefits, eligibility verification, etc. But the person hiring doesn’t need to know any of this - the hiring agency handles all of that.
In the same way, using a Factory allows the consumer to create new objects without having to know the details of how they’re created, or what their dependencies are - they only have to give the information they actually want.
Intent:
In what situations can the pattern be used, we can consider it as pattern`s context.
- creates objects without exposing the instantiation logic to the
client - refers to the newly created object through a common interface
Structure
Diagram using the Object Modeling Technique (OMT)
Implementation
: Implementation details to consider
Applicability
Participants
Classes and objects in design
Related patterns
: A list of related patterns
Motivation
: A scenario that illustrates the problem