Behavioral Design Patterns in Java Design Patterns

Behavioral Design Patterns :

Chain of Responsibility :

  1. Purpose: Gives multiple objects a chance to process the request, thereby avoiding coupling between the sender and receiver of the request. Chain these objects together and pass the request down the chain until one object handles it.

  2. Scenes:

    1. There are multiple objects that can handle a request, and which object handles the request is automatically determined at runtime.
    2. You want to submit a request to one of multiple objects without explicitly specifying the recipient.
    3. The collection of objects that can handle a request is dynamically specified.
  3. Structure diagram

    1

  4. known application

    1. Java Servlet: 1 FilterChain = n Filter + 1 Servlet filter chain, filter the request
    2. Spring MVC: 1 HandlerExecutionChain = nHandlerInterceptor + 1 handler request interceptor to intercept requests
    3. Spring MVC: 1 RequestResponseBodyAdviceChain = mRequestBodyAdvice + n ResponseBodyAdvice such as: unified Restful-style response status
  5. Related Patterns

    1. Chain of Responsibility is often used with Composite. In this case, a widget's parent widget can act as its successor.

Iterator

  1. Purpose: Provides a way to sequentially access elements in an aggregate object without exposing the object's internal representation.

  2. Scenes:

    1. Access the contents of an aggregate object without exposing its internal representation.
    2. Multiple traversals of aggregate objects are supported.
    3. Provides a unified interface for traversing different aggregate structures.
  3. Structure diagram

    1

  4. known application

    1. ArrayList
    2. HashSet
  5. Related Patterns

    1. Composite: Iterators are often applied to recursive structures such as composites.
    2. Factory Method: Polymorphic iterators rely on Factory Method to instantiate the appropriate iterator subclass.

Strategy mode (Strategy)

  1. Purpose: Define a series of algorithms, encapsulate them one by one, and make them interchangeable. This pattern allows the algorithm to vary independently of the client using it.

  2. Scenes:

    1. Many related classes just behave differently. "Strategies" provide a way to configure a class with one of several behaviors.
    2. A class defines various behaviors, and these behaviors appear in the form of multiple conditional statements in the operation of the class. Move the relevant conditional branches into their respective policy classes in place of these conditional statements.
  3. Structure diagram

    2

  4. known application

    1. Arrays.sort
    2. Collections.sort
  5. Template Method

    1. Purpose: Defines the skeleton of an algorithm in an operation, while deferring some steps to subclasses. Template Method allows subclasses to redefine certain steps of an algorithm without changing its structure

    2. Scenes:

      1. Implement the invariant parts of the algorithm and leave the mutable behavior to subclasses.
      2. Common behavior in each subclass should be extracted and concentrated into a common parent class to avoid code duplication.
    3. Structure diagram

      3

    4. known application

      1. AbstractQueueSynchronizer

        public final void acquire(int arg) {
                  
                  
            if (!tryAcquire(arg) &&
                acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
                selfInterrupt();
        }
        
    5. Related Patterns

      1. Strategy: Template method uses inheritance to change part of the algorithm, Strategy uses delegate to change the whole algorithm.
      2. Factory Method is often called by template method.

Guess you like

Origin blog.csdn.net/a1774381324/article/details/120796871