DEV Community

Cover image for Top 5 Design Patterns in Java Spring Boot: Best Practices and Examples
Jacky
Jacky

Posted on

Top 5 Design Patterns in Java Spring Boot: Best Practices and Examples

As a seasoned Java backend developer immersed in the world of Spring Boot and Spring Framework for a decade, I’ve come to realize the crucial role that design patterns play in building robust and scalable applications. In this article, we’ll delve into five essential design patterns and explore the best practices for applying them effectively in your Spring Boot projects. Each pattern will be accompanied by a practical example to demonstrate its implementation.

Singleton Pattern

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. This is particularly useful for managing resources such as database connections or caching objects. Here’s how you can implement it in Spring Boot:

public class DatabaseConnection {
    private static DatabaseConnection instance;

    private DatabaseConnection() {
        // Private constructor to prevent instantiation
    }
    public static synchronized DatabaseConnection getInstance() {
        if (instance == null) {
            instance = new DatabaseConnection();
        }
        return instance;
    }
}
Enter fullscreen mode Exit fullscreen mode

Factory Method Pattern

The Factory Method pattern provides an interface for creating objects in a superclass, allowing subclasses to alter the type of objects that will be created. This is useful for decoupling object creation logic from client code. Let’s see an example in Spring Boot:

public interface PaymentProcessor {
    void processPayment();
}

public class CreditCardProcessor implements PaymentProcessor {
    @Override
    public void processPayment() {
        // Process credit card payment logic
    }
}
public class PayPalProcessor implements PaymentProcessor {
    @Override
    public void processPayment() {
        // Process PayPal payment logic
    }
}
public interface PaymentProcessorFactory {
    PaymentProcessor createPaymentProcessor();
}
@Component
public class PaymentProcessorFactoryImpl implements PaymentProcessorFactory {
    @Override
    public PaymentProcessor createPaymentProcessor() {
        // Logic to determine which processor to create (based on configuration, etc.)
        return new CreditCardProcessor();
    }
}
Enter fullscreen mode Exit fullscreen mode

Observer Pattern

The Observer pattern defines a one-to-many dependency between objects, ensuring that when one object changes state, its dependents are notified and updated automatically. This is commonly used in event-driven systems. Let’s implement it in Spring Boot:

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class OrderListener implements ApplicationListener<OrderEvent> {
    @Override
    public void onApplicationEvent(OrderEvent event) {
        // Handle order event
    }
}

public class OrderEvent extends ApplicationEvent {
    public OrderEvent(Object source) {
        super(source);
    }
}

@Component
public class OrderService {
    private ApplicationEventPublisher eventPublisher;
    public OrderService(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }
    public void placeOrder() {
        // Logic to place order
        // Publish order event
        eventPublisher.publishEvent(new OrderEvent(this));
    }
}
Enter fullscreen mode Exit fullscreen mode

Decorator Pattern

The Decorator pattern allows behavior to be added to objects dynamically, without affecting the behavior of other objects from the same class. This is useful for adding features such as logging, caching, or encryption to existing classes. Let’s implement it in Spring Boot:

public interface DataService {
    void fetchData();
}

@Component
public class DataServiceImplementation implements DataService {
    @Override
    public void fetchData() {
        // Fetch data implementation
    }
}
@Component
public class LoggingDecorator implements DataService {
    private DataService delegate;
    public LoggingDecorator(DataService delegate) {
        this.delegate = delegate;
    }
    @Override
    public void fetchData() {
        // Logging logic before fetching data
        delegate.fetchData();
        // Logging logic after fetching data
    }
}
Enter fullscreen mode Exit fullscreen mode

Strategy Pattern

The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. This is useful when you have multiple algorithms that can be used interchangeably. Let’s implement it in Spring Boot:

public interface CompressionStrategy {
    void compress(String file);
}

@Component
public class ZipCompressionStrategy implements CompressionStrategy {
    @Override
    public void compress(String file) {
        // Zip compression logic
    }
}
@Component
public class RarCompressionStrategy implements CompressionStrategy {
    @Override
    public void compress(String file) {
        // RAR compression logic
    }
}
@Component
public class CompressionContext {
    private CompressionStrategy strategy;
    public CompressionContext(CompressionStrategy strategy) {
        this.strategy = strategy;
    }
    public void setStrategy(CompressionStrategy strategy) {
        this.strategy = strategy;
    }
    public void compressFile(String file) {
        strategy.compress(file);
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Design patterns are indispensable tools in the arsenal of any Java backend developer, particularly when working with frameworks like Spring Boot. By mastering these patterns and applying them judiciously in your projects, you can achieve code that is not only more maintainable and scalable but also easier to understand and extend.

Take a sip of coffee… ☕︎ ☕︎ ☕︎

Top comments (0)