Creational Design Pattern – Initial Concept
This springcavaj – Creational Design Pattern page briefly describes the Initial Concept of Creational Design Patterns, advantages, disadvantages, benefits, various types, common problems, and a few interview questions discussed.
This guide will provide a deep understanding of Creational Design Patterns for designing and architecting an application.
What is a Factory Design Pattern?
The Factory Pattern is a Creational Design Pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created. Instead of instantiating objects directly in the client code, the Factory Pattern lets a class defer the instantiation process to its subclasses.
What are the features of Factory Design Pattern?
- Encapsulation – Encapsulates object creation logic.
- Flexibility – Makes the system more flexible by decoupling the object creation process from the client code.
- Polymorphism – Supports polymorphism by allowing a class to instantiate objects of various types without knowing the specific type.
What are the advantages of Factory Design Patterns?
- Loose Coupling – The client code is decoupled from the concrete classes that it needs to instantiate.
- Single Responsibility Principle – The Factory Pattern adheres to the single responsibility principle by delegating the responsibility of instantiating objects to the factory.
- Reusability – Improves reusability by using the same factory to create different objects.
What are the disadvantages of Factory Design Patterns?
- Increased Complexity – Introduces an additional layer, which can complicate the code.
- Maintenance Overhead – Requires maintaining additional factory classes and interfaces.
What are the use cases of Factory Design Patterns?
- UI Components – Creating different UI components like buttons, text boxes, etc., without specifying their concrete classes.
- Database Connection – Creating different database connections depending on the database type (MySQL, PostgreSQL, NoSQL, etc.).
- Logging Framework – Instantiating different loggers based on the configuration.
Brief Description of Implementation
I have developed a small program to help you to understand the working principle of Abstract Factory Design Patterns. The code is uploaded to GitHub as springcavaj-designpattern repository. Please clone the application and import in any of the IDs like either STS or Eclipse.
After successfully importing the above code base, one will find a class named FactoryMasterclassApplication.java in the package as com.springcavaj.designpattern.factory. Please see the screenshot attached below.
Here you can see the class under the com.springcavaj.designpattern.factory package is FactoryMasterclassApplication.java. Just right-click on the class, select the Run As option, and then Java Application. It will run and generate the output as provided below.
INFO com.springcavaj.designpattern.factory.design.impl.Spring - Use XML Based Dependency Injections...
INFO com.springcavaj.designpattern.factory.design.impl.SpringBoot - use Annotation Based Dependency Injections...
Detailed Explanation
In this small program, I used the concept of Spring and SpringBoot, which are the 2 factory classes.
Framework.java is the interface that has one void method as frameworkFeatures(). Now both the Spring.java, SpringBoot.java classes implement the Framework.java interface. After implementing both overrides, the frameworkFeatures() method provides one LOG statement. Like, Spring.java provides the LOG statement as it implements XML-based Dependency Injection, whereas, SpringBoot.java provides the LOG statement as it implements Annotation-based Dependency Injection.
Now, there is another class named FrameworkFactory.java which will return the required factory object based on the type you are providing as a parameter in a function named getFramework(String frameworkType) and this method returns the reference of Framework.java interface. If no type matches, it returns null.
A client class named FactoryMasterclassApplication.java creates the object of FrameworkFactory and calls the getFramework() by passing the required factory type.
public class FactoryMasterclassApplication {
public static void main(String[] args) {
FrameworkFactory factory = new FrameworkFactory();
Framework framework = factory.getFramework("Spring");
framework.frameworkFeatures();
framework = factory.getFramework("springboot");
framework.frameworkFeatures();
}
}
Steps to run the application
The complete Run application steps are provided in this README.md file, but still, I am providing the steps to run points below.
- Clone the sprincavaj-designpattern application from the GitHub repository.
- Import the application as a Maven application, either in STS or Eclipse.
- Find the FactoryMasterclassApplication.java class.
- Right-click on the file and select Run As -> Java Application.
GitHub Code Link
Download the Source Code from GitHub
Common Problems Faced Implementing Factory Pattern
Factory Design Pattern Common Problems
Interview FAQs
Factory Design Pattern Interview FAQs
Other Useful Links
Implementations of various components of Spring Boot