Sitemap

Member-only story

Design Patterns in Go: Chain of Responsibility

4 min readOct 29, 2024
Chain of Responsibility Pattern

Behavioral Patterns target issues related to communication and interaction between objects. They focus on defining protocols for collaboration among objects that are often too complex to design statically, enabling dynamic delegation of responsibilities at runtime.

OK, Let’s go through the description of the 1st behavioral pattern — Chain of Responsibility.

Context:

In some scenarios, multiple objects may be able to handle a request:

  • request may be processed by all objects one by one
  • request may be processed by all objects in some order which matters
  • request may be processed by several objects rather than all
  • the objects maybe changed at runtime, and cannot be known in advance

Here, multiple objects actually means some ‘logic’, like an article, the system will do some checking before accept this article:

  • checking if the user valid
  • checking if the user authorized
  • checking if the article data is empty
  • checking if the article data contains dirty words
  • checking if the article data is copied from others (checking similarity)

--

--

ZhangJie (Kn)
ZhangJie (Kn)

No responses yet