design pattern - chain of responsibility

13
Chain of Responsibility From Definition to Implementation Mudasir Qazi - [email protected] 1 16-Dec-14

Upload: mudasir-qazi

Post on 09-Aug-2015

88 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 1

Chain of ResponsibilityFrom Definition to Implementation

16-Dec-14

Page 2: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 3: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 4: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 5: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 6: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 6

UML – Sequence Diagram

16-Dec-14

Page 7: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 8: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 9: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 10: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 11: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 12: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 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

Page 13: Design Pattern - Chain of Responsibility

Mudasir Qazi - [email protected] 13

Step 5 : Results

• Here are results as we expected.

16-Dec-14