Design Pattern - Chain of Responsibility

Preview:

Citation preview

Mudasir Qazi - mudasirqazi00@gmail.com 1

Chain of ResponsibilityFrom Definition to Implementation

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 2

Contents / Agenda

• Definition• Advantages and Usage• UML Diagram• Sequence Diagram• Daily life Examples• Implementation – Scenario UML• Implementation – Steps 1 to 4• Results

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 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”.

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 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.

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 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.

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 6

UML – Sequence Diagram

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 7

Examples1. 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 100kDirector – 100k to 250kVice President – anything above 250kWe will pass the request to the manager until the application is processed.

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 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 100kDirector – 100k to 250kVice President – anything above 250k

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 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.

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 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.

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 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.

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 12

Step 4 : Testing

We created three object to test with different amounts. Then we have to tell the hierarchy by SetNextApprover method.

16-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 13

Step 5 : Results

• Here are results as we expected.

16-Dec-14

Recommended