Design Pattern - Chain of Responsibility

Design Pattern - Chain of Responsibility
Dalam pemrograman berbasis objek, chain of responsibility pattern merupakan sebuah design pattern yang terdiri dari sekumpulan command objects dan sekumpulan processing objects.  Setiap objek berisi logika pengolahan yang mendefinisikan jenis objek perintah yang dapat menangani; sisanya diteruskan ke objek pengolahan berikutnya dalam rantai. Juga Mekanisme ada untuk menambahkan objek pengolahan baru untuk akhir rantai ini.


There is a potentially variable number of "handler" or "processing element" or "node" objects, and a stream of requests that must be handled. Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings.
Chain of responsibility example


Encapsulate the processing elements inside a "pipeline" abstraction; and have clients "launch and leave" their requests at the entrance to the pipeline.
Chain of responsibility example
The pattern chains the receiving objects together, and then passes any request messages from object to object until it reaches an object capable of handling the message. The number and type of handler objects isn't known a priori, they can be configured dynamically. The chaining mechanism uses recursive composition to allow an unlimited number of handlers to be linked.
Chain of Responsibility simplifies object interconnections. Instead of senders and receivers maintaining references to all candidate receivers, each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain.
Make sure there exists a "safety net" to "catch" any requests which go unhandled.
Do not use Chain of Responsibility when each request is only handled by one handler, or, when the client object knows which service object should handle the request.


The derived classes know how to satisfy Client requests. If the "current" object is not available or sufficient, then it delegates to the base class, which delegates to the "next" object, and the circle of life continues.
Chain of responsibility scheme
Multiple handlers could contribute to the handling of each request. The request can be passed down the entire length of the chain, with the last link being careful not to delegate to a "null next".

Berikut adalah contoh bagian program yang menggunakan chain of responsibility pattern

Sumber :

No comments:

Post a Comment