Sobes.tech
Middle

Provide examples of behavioral design patterns.

sobes.tech AI

Answer from AI

  • Chain of Responsibility: Passing a request along a chain of handlers. Each handler either processes the request or passes it to the next one. Example: event handling in GUI.

  • Command: Encapsulating a request as an object, allowing parameterization of clients with different requests, queuing or logging them, and supporting undo operations. Example: actions in a text editor menu.

  • Iterator: Providing a way to access all elements of a composite object sequentially without exposing its internal representation. Example: iterating over a collection.

    // Example of using Iterator
    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
    
  • Mediator: Defining an object that encapsulates the interaction between a set of objects. The mediator promotes loose coupling by preventing objects from referring to each other explicitly. Example: interaction of UI components in a dialog.

  • Memento: Saving and restoring an object's internal state externally without breaking encapsulation, so that the object can be returned to this state later. Example: undo feature in an application.

  • Observer: Defining a one-to-many dependency between objects so that when one object changes its state, all its dependents are notified and updated automatically. Example: event-driven systems.

    // Example implementation of Observer
    public interface Observer {
        void update();
    }
    
    public class Subject {
        private List<Observer> observers = new ArrayList<>();
    
        public void addObserver(Observer observer) {
            observers.add(observer);
        }
    
        public void notifyObservers() {
            for (Observer observer : observers) {
                observer.update();
            }
        }
    }
    
  • State: Allows an object to change its behavior when its internal state changes. It appears as if the class of the object has changed. Example: different states of a TCP connection.

  • Strategy: Defining a family of algorithms, encapsulating each one, and making them interchangeable. It allows selecting an algorithm at runtime. Example: different sorting algorithms.

    // Example of using Strategy
    interface SortingStrategy {
        void sort(int[] array);
    }
    
    class BubbleSortStrategy implements SortingStrategy {
        @Override
        public void sort(int[] array) {
            // Bubble sort implementation
        }
    }
    
    class Context {
        private SortingStrategy strategy;
    
        public void setStrategy(SortingStrategy strategy) {
            this.strategy = strategy;
        }
    
        public void executeSort(int[] array) {
            strategy.sort(array);
        }
    }
    
  • Template Method: Defining the skeleton of an algorithm in a superclass, allowing subclasses to override certain steps without changing the algorithm's structure. Example: steps for generating a report.

    // Example of Template Method
    abstract class AbstractClass {
        public final void templateMethod() {
            step1();
            step2();
            step3();
        }
    
        abstract void step1();
        void step2() { /* Default implementation */ }
        abstract void step3();
    }
    
    class ConcreteClass extends AbstractClass {
        @Override
        void step1() {
            // Specific implementation of step 1
        }
    
        @Override
        void step3() {
            // Specific implementation of step 3
        }
    }
    
  • Visitor: Represents an operation to be performed on elements of an object structure. Visitor allows defining a new operation without changing the classes of the elements it operates on. Example: operations on elements of a syntax tree.