00:00

Behavioral Design Pattern

Behavioral Design Patterns are a category of design patterns that focus on how objects communicate and interact with each other. Instead of concentrating on object creation or structure, these patterns define clear ways for objects to share responsibilities and behavior.

The main goal of behavioral design patterns is to make the system flexible, loosely coupled, and easy to maintain. They help reduce complex conditional logic and make code easier to extend without changing existing classes.

Key Characteristics

  • Defines how objects collaborate and communicate
  • Improves flexibility by separating responsibilities
  • Encourages loose coupling between classes
  • Makes code easier to test and maintain

Common Behavioral Design Patterns

  • Observer Pattern
  • Strategy Pattern
  • Command Pattern
  • State Pattern
  • Iterator Pattern

Example: Observer Design Pattern

The Observer Pattern is one of the most commonly used behavioral design patterns. It defines a one-to-many relationship between objects. When one object changes its state, all its dependent objects are notified automatically.

Real-life example: Think of a YouTube channel. When a new video is uploaded, all subscribed users get a notification.

Observer Pattern – Java Example

Step 1: Create the Observer Interface


    public interface Observer {
        void update(String message);
    }
    

Step 2: Create the Subject Interface


    import java.util.List;
    
    public interface Subject {
        void subscribe(Observer observer);
        void unsubscribe(Observer observer);
        void notifyObservers();
    }
    

Step 3: Implement the Subject


    import java.util.ArrayList;
    import java.util.List;
    
    public class YouTubeChannel implements Subject {
    
        private List<Observer> observers = new ArrayList<>();
        private String latestVideo;
    
        @Override
        public void subscribe(Observer observer) {
            observers.add(observer);
        }
    
        @Override
        public void unsubscribe(Observer observer) {
            observers.remove(observer);
        }
    
        public void uploadVideo(String videoTitle) {
            this.latestVideo = videoTitle;
            notifyObservers();
        }
    
        @Override
        public void notifyObservers() {
            for (Observer observer : observers) {
                observer.update(latestVideo);
            }
        }
    }
    

Step 4: Implement the Observer


    public class Subscriber implements Observer {
    
        private String name;
    
        public Subscriber(String name) {
            this.name = name;
        }
    
        @Override
        public void update(String message) {
            System.out.println(name + " received notification: New video uploaded - " + message);
        }
    }
    

Step 5: Test the Observer Pattern


    public class ObserverPatternDemo {
    
        public static void main(String[] args) {
    
            YouTubeChannel channel = new YouTubeChannel();
    
            Observer user1 = new Subscriber("Sandeep");
            Observer user2 = new Subscriber("Ajay");
    
            channel.subscribe(user1);
            channel.subscribe(user2);
    
            channel.uploadVideo("Behavioral Design Patterns Explained");
        }
    }
    

Output


    Sandeep received notification: New video uploaded - Behavioral Design Patterns Explained
    Ajay received notification: New video uploaded - Behavioral Design Patterns Explained
    

Why Use Behavioral Design Patterns?

  • Reduces tight coupling between objects
  • Makes behavior easy to change at runtime
  • Improves code readability and structure
  • Follows SOLID design principles

Conclusion

Behavioral Design Patterns play a crucial role in building scalable and maintainable applications. They help define clear communication rules between objects and keep business logic clean. The Observer Pattern is a great starting point to understand how behavioral patterns work in real-world applications.