Chain of Responsibility

Intent

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Class diagram of the original GoF pattern

structure-GoF:chain

Improvement in MixJuice

Solved Problem(s)

Introduction problem
It is impossible to introduce Chain of Responsibility pattern into the existing classes without modifying the source code. (This problem is not mentioned in the GoF book.)
Extensibility problem
It is impossible to add new operations to Handler without modifying the source code. (This problem is mentioned at p.227,line.20-23 in the GoF book.)

Used programming technique(s)

Method addition to Handler and ConcreteHandler classes.

Consequences

Structure and pseudo code

structure:chain_sample1

module original {
  define abstract class Handler {
    Handler successor;
    define void handleRequestA(){ successor.handleRequestA(); }
  }

  define class ConcreteHandler1 extends Handler {
    void handleRequestA() {...}
  }

  define class ConcreteHandler2 extends Handler {
    void handleRequestA() {...}
  }
}

module extension extends original {
  class Handler {
    define void handleRequestB(){ successor.handleRequestB(); }
  }

  class ConcreteHandler1 {
    void handleRequestB() {...}
  }

  class ConcreteHandler2 {
    void handleRequestB() {...}
  }
}

Akira TANAKA <akr@m17n.org>, Yuuji ICHISUGI <y-ichisugi@aist.go.jp>

Top