The Goal:

Manage how objects communicate when something happens.

Example

Imagine an order is placed in your Spring Boot application. You need to send a confirmation email and update inventory. Instead of hardcoding those services together, you use an Observer (via Spring’s Event system) to broadcast a message.

  1. The Event (the message being passed)
public record OrderPlacedEvent(String orderId) {}
  1. The Publisher
@Service
public class OrderService {
    private final ApplicationEventPublisher publisher;

    public OrderService(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    public void placeOrder(String orderId) {
        System.out.println("Order saved to database.");
        
        // The BEHAVIORAL action: Broadcasting a message
        publisher.publishEvent(new OrderPlacedEvent(orderId));
    }
}
  1. The Listener (The Observer reacting)
@Component
public class EmailNotificationService {
    
    @EventListener
    public void sendEmail(OrderPlacedEvent event) {
        // Reacting to the workflow
        System.out.println("Sending confirmation email for: " + event.orderId());
    }
}

Why this is Behavioral:

Notice how this isn’t about making pieces “fit.” It’s about establishing a communication channel. The OrderService doesn’t care how the EmailNotificationService is built; it only cares about passing the message at the right time in the workflow.