Creational Design Patterns in Java with spring #softwaredesignpatterns #javapatterns #javaspring #innovation #design thinking
- Rajkishore R
- Feb 23
- 2 min read

Creational Design Patterns in Java with Spring
Creational design patterns are concerned with how objects are created. These patterns provide mechanisms to create objects in a manner suitable to the situation. In the context of Java and Spring, several creational design patterns can be effectively utilized. Below are some of the most common creational design patterns along with their implementations in Java using the Spring Framework.
1. Singleton Pattern
The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. You can quickly implement this pattern in the spring container.
import org.springframework.stereotype.Component;
@Component
public class SingletonService {
// Private constructor to prevent instantiation
private SingletonService() {}
public void serve() {
System.out.println("Serving from SingletonService");
}
}
2. Factory Method Pattern
The Factory Method pattern defines an interface for creating an object but lets subclasses alter the type of objects that will be created. In Spring, you can use the FactoryBean interface.
import org.springframework.beans.factory.FactoryBean;
public class MyFactoryBean implements FactoryBean {
@Override
public MyObject getObject() {
return new MyObject();
}
@Override
public Class getObjectType() {
return MyObject.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
3. Abstract Factory Pattern
The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. This can be achieved in Spring using configuration classes.
public interface ShapeFactory {
Shape createShape();
}
public class CircleFactory implements ShapeFactory {
public Shape createShape() {
return new Circle();
}
}
public class SquareFactory implements ShapeFactory {
public Shape createShape() {
return new Square();
}
}
4. Builder Pattern
The Builder pattern separates the construction of a complex object from its representation. You can use the builder pattern in Spring by creating a builder class.
public class User {
private String name;
private String email;
private User(UserBuilder builder) {
this.name = builder.name;
this.email = builder.email;
}
public static class UserBuilder {
private String name;
private String email;
public UserBuilder setName(String name) {
this.name = name;
return this;
}
public UserBuilder setEmail(String email) {
this.email = email;
return this;
}
public User build() {
return new User(this);
}
}
}
5. Prototype Pattern
The Prototype pattern creates duplicate objects while keeping performance in mind. In Spring, you can define a prototype bean.
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class PrototypeBean {
public void display() {
System.out.println("Prototype Bean Instance");
}
}
Conclusion
Creational design patterns provide various strategies for object creation, enhancing flexibility and reusability in your code. You can create robust and maintainable applications by leveraging these patterns in conjunction with the Spring Framework.
Yorumlar