📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Video
The factory design pattern overview
- Factory Pattern is one of the Creational Design Pattern.
- The Factory Design Pattern or Factory Method Pattern is one of the most used design patterns in Java.
- In the Factory pattern, we create an object without exposing the creation logic to the client and refer to newly created objects using a common interface.
- The factory design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern takes out the responsibility of the instantiation of a class from the client program to the factory class.
Factory Pattern Implementation
Polygon Interface
package com.java.tutorials.factory;
public interface Polygon {
public String getType();
}
Triangle Implementation
package com.java.tutorials.factory;
public class Triangle implements Polygon {
@Override
public String getType() {
return "Triangle";
}
}
Square Implementation
package com.java.tutorials.factory;
public class Square implements Polygon {
@Override
public String getType() {
return "Square";
}
}
Pentagon Implementation
package com.java.tutorials.factory;
public class Pentagon implements Polygon {
@Override
public String getType() {
return "Pentagon";
}
}
Octagon Implementation
package com.java.tutorials.factory;
public class Octagon implements Polygon {
@Override
public String getType() {
return "Octagon";
}
}
PolygonFactory Implementation
package com.java.tutorials.factory;
public class PolygonFactory {
public static Polygon getInstance(int sides) {
Polygon polygon = null;
switch (sides) {
case 3:
polygon = new Triangle();
break;
case 4:
polygon = new Square();
break;
case 5:
polygon = new Pentagon();
break;
case 8:
polygon = new Octagon();
break;
default:
// throw exception
break;
}
return polygon;
}
}
Client
package com.java.tutorials.factory;
public class Client {
public static void main(String[] args) {
// triangle
Polygon polygon = PolygonFactory.getInstance(3);
System.out.println(polygon.getType());
// square
Polygon square = PolygonFactory.getInstance(4);
System.out.println(square.getType());
// pentagon
Polygon pentagon = PolygonFactory.getInstance(5);
System.out.println(pentagon.getType());
// octagon
Polygon octagon = PolygonFactory.getInstance(8);
System.out.println(octagon.getType());
}
}
Output
Triangle
Square
Pentagon
Octagon
When to Use Factory Method Design Pattern
- When the implementation of an interface or an abstract class is expected to change frequently.
- The factory design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern takes out the responsibility of the instantiation of a class from the client program to the factory class.
- The lifetime management of the generated objects must be centralized to ensure consistent behavior within the application.
- Advantage: The factory design pattern provides an approach to code for interface rather than implementation.
- Advantage: The factory pattern removes the instantiation of actual implementation classes from the client code. The factory pattern makes our code more robust, less coupled and easy to extend.
Comments
Post a Comment
Leave Comment