📘 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
Factory Design Pattern: Without Using Lambda Expressions
package net.javaguides.corejava.factorypattern;
public interface Shape {
void draw();
}
package net.javaguides.corejava.factorypattern;
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
package net.javaguides.corejava.factorypattern;
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
package net.javaguides.corejava.factorypattern;
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
package net.javaguides.corejava.factorypattern;
public enum ShapeType {
CIRCLE, RECTANGLE, SQUARE;
}
package net.javaguides.corejava.factorypattern;
public class ShapeFactory {
// use getShape method to get object of type shape
public Shape getShape(ShapeType shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType.equals(ShapeType.CIRCLE)) {
return new Circle();
} else if (shapeType.equals(ShapeType.RECTANGLE)) {
return new Rectangle();
} else if (shapeType.equals(ShapeType.SQUARE)) {
return new Square();
}
return null;
}
}
package net.javaguides.corejava.factorypattern;
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
// get an object of Circle and call its draw method.
Shape shape1 = shapeFactory.getShape(ShapeType.CIRCLE);
// call draw method of Circle
shape1.draw();
// get an object of Rectangle and call its draw method.
Shape shape2 = shapeFactory.getShape(ShapeType.RECTANGLE);
// call draw method of Rectangle
shape2.draw();
// get an object of Square and call its draw method.
Shape shape3 = shapeFactory.getShape(ShapeType.SQUARE);
// call draw method of square
shape3.draw();
}
}
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Factory Design Pattern: Refactor With Lambda Expressions
Supplier<Shape> circleSupplier = Circle::new;
Circle circle = circleSupplier.get();
final static Map<ShapeType, Supplier<Shape>> map = new HashMap<>(); static { map.put(ShapeType.CIRCLE, Circle::new); map.put(ShapeType.RECTANGLE, Rectangle::new); map.put(ShapeType.SQUARE, Square::new); }
package net.javaguides.corejava.factorypattern;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
public class ShapeFactory {
final static Map<ShapeType, Supplier<Shape>> map = new HashMap<>();
static {
map.put(ShapeType.CIRCLE, Circle::new);
map.put(ShapeType.RECTANGLE, Rectangle::new);
map.put(ShapeType.SQUARE, Square::new);
}
public Shape getShape(ShapeType shapeType) {
Supplier<Shape> shape = map.get(shapeType);
if (shape != null) {
return shape.get();
}
throw new IllegalArgumentException("No such shape " + shapeType.name());
}
}
package net.javaguides.corejava.factorypattern;
import java.util.function.Supplier;
public class FactoryPatternDemo {
public static void main(String[] args) {
Supplier<ShapeFactory> shapeFactory = ShapeFactory::new;
// get an object of Circle and call its draw method.
Shape shape1 = shapeFactory.get().getShape(ShapeType.CIRCLE);
// call draw method of Circle
shape1.draw();
// get an object of Rectangle and call its draw method.
Shape shape2 = shapeFactory.get().getShape(ShapeType.RECTANGLE);
// call draw method of Rectangle
shape2.draw();
// get an object of Square and call its draw method.
Shape shape3 = shapeFactory.get().getShape(ShapeType.SQUARE);
// call draw method of square
shape3.draw();
}
}
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Learn complete Java 8 on Java 8 Tutorial
The source code of this article is available on my GitHub repository https://github.com/RameshMF/factory-pattern-java8
Comments
Post a Comment
Leave Comment