in these slides i have explained the Chain of Responsibility design pattern. slides includes definition, explanation and then implementation by code examples.
3. Definition
1. Avoid coupling the sender of a request to its receiver by giving
multiple objects a chance to handle the request.
2. We can say that normally each receiver contains reference of another
receiver. If one object cannot handle the request then it passes the
same to the next receiver and so on.
3. Chain the receiving objects and pass the request along the chain until
an object handles it.
• It comes under the “Behavioral Design Patterns”.
Mudasir Qazi - mudasirqazi00@gmail.com 316-Dec-14
4. Advantages and Usage
• Advantages:
1. It reduces the coupling.
2. It adds flexibility while assigning the responsibilities to objects.
3. It allows a set of classes to act as one, events produced in one class can be sent
to other handler classes with the help of composition.
• Usage:
1. When more than one object can handle a request and the handler is unknown.
2. When the group of objects that can handle the request must be specified in
dynamic way.
Mudasir Qazi - mudasirqazi00@gmail.com 416-Dec-14
5. UML Diagram
Client’s Request comes to
the (abstract) handler and
it decides that which of the
(concrete) sub handlers will
actually full fil the request.
If request is not completed
by first assigned sub
handler then it returns it to
the base handler and it
decides which of the sub
handlers is now feasible for
task.
Mudasir Qazi - mudasirqazi00@gmail.com 516-Dec-14
7. Examples
1. Leave Application Example:
Lets say we have an organization where Team members when apply for leave, the request goes to
the Team Leader. Team leader can approve all the leave request of less than 10 days. If the leave
request is of more than 10 days then the request will be passed on to the Project Leader. Project
leader is able to approve leaves of up to 20 days. If the leave is applied for more than 20 days then
this requests will be passed to the HR. HR can approve up to 30 days of leave. If the leave is of
more than 30 days then the leave application cannot be approved by the system and it needs a
manual process for approval.
2. Loan Approval Example:
In a bank where the approval route for mortgage applications are from the bank manager to the
director then to the vice president, where the approval limits are:
Manager – 0 to 100k
Director – 100k to 250k
Vice President – anything above 250k
We will pass the request to the manager until the application is processed.
Mudasir Qazi - mudasirqazi00@gmail.com 716-Dec-14
8. Implementation – Loan Approval Example
• We are going to implement loan approval example like discussed above. Following is its UML Diagram.
Approving Limits:
Manager – 0 to 100k
Director – 100k to 250k
Vice President – anything above 250k
Mudasir Qazi - mudasirqazi00@gmail.com 816-Dec-14
9. Step 1 : Loan Class
Our Loan class contains an integer to hold loan
amount, a getter property and a constructor
working as a setter method.
Mudasir Qazi - mudasirqazi00@gmail.com 916-Dec-14
10. Step 2 : Main Handler (abstract)
This class is going to act as main request handler, every request
form client will be accepted by this and then this will pass request
to its subclasses.
Mudasir Qazi - mudasirqazi00@gmail.com 1016-Dec-14
11. Step 3 : Concrete Classes
Note: All these classes are inherited form our
LoanApprover abstract class. And implements
the ApproveLoan method with respect to
their expectations.
Mudasir Qazi - mudasirqazi00@gmail.com 1116-Dec-14
12. Step 4 : Testing
We created three object to test with different
amounts. Then we have to tell the hierarchy
by SetNextApprover method.
Mudasir Qazi - mudasirqazi00@gmail.com 1216-Dec-14
13. Step 5 : Results
• Here are results as we expected.
Mudasir Qazi - mudasirqazi00@gmail.com 1316-Dec-14