Design Pattern - Chain of Responsibility

mudasirqazi00 2,485 views 13 slides Apr 30, 2015
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

in these slides i have explained the Chain of Responsibility design pattern. slides includes definition, explanation and then implementation by code examples.


Slide Content

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

Contents / Agenda Definition Advantages and Usage UML Diagram Sequence Diagram Daily life Examples Implementation – Scenario UML Implementation – Steps 1 to 4 Results Mudasir Qazi - [email protected] 2 16-Dec-14

Definition Avoid coupling the sender of a request to its receiver by giving multiple objects a chance to handle the request. 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. 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 - [email protected] 3 16-Dec-14

Advantages and Usage Advantages: It reduces the coupling. It adds flexibility while assigning the responsibilities to objects. 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: When more than one object can handle a request and the handler is unknown. When the group of objects that can handle the request must be specified in dynamic way. Mudasir Qazi - [email protected] 4 16-Dec-14

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 - [email protected] 5 16-Dec-14

UML – Sequence Diagram Mudasir Qazi - [email protected] 6 16-Dec-14

Examples 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. 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 - [email protected] 7 16-Dec-14

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 - [email protected] 8 16-Dec-14

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 - [email protected] 9 16-Dec-14

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 - [email protected] 10 16-Dec-14

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 - [email protected] 11 16-Dec-14

Step 4 : Testing We created three object to test with different amounts. Then we have to tell the hierarchy by SetNextApprover method. Mudasir Qazi - [email protected] 12 16-Dec-14

Step 5 : Results Here are results as we expected. Mudasir Qazi - [email protected] 13 16-Dec-14