Chain of Responsibility

Contents

Design Patterns
1
Design Patterns
PREVIEW2m 26s
2
Singleton Pattern
PREVIEW3m 13s
9
Decorator
7m 48s
10
Flyweight
9m 20s
11
Proxy
7m 36s
12
Facade
6m 5s
14
Command
11m 44s
15
Iterator
8m 24s
16
Visitor
9m 4s
17
Mediator
11m 38s
18
Memento
9m 48s
19
Observer
10m 40s
20
Strategy
8m 27s
21
State
10m 27s
22
Template
7m 56s
23
MVC
10m 27s
26
DAO
10m 27s
27

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
4h 56m
Students
55
Ratings
5/5
starstarstarstarstar
Description

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

Hello dear friends. In this video, we will examine the chain of responsibility pattern. Let's begin. The chain of responsibility design pattern is one of the gang of four design patterns which creates a chain of receiver objects for a request. The chain of responsibility pattern is a behavioral pattern which is used to avoid coupling the sender of the request to the receiver, and gives more than one receiver object the opportunity to handle the request. The chain of responsibility pattern allows a number of classes to attempt to handle a request independently. The receiver objects of the requests are free from the order and can be used in any order for processing. This pattern decouples sender and receiver objects based on type of request. This pattern defines a chain of receiver objects having the responsibility depending on runtime conditions to either handle a request or forward it to the next receiver on the chain.

This pattern helps us avoid coupling of sender and receiver objects of a request and allows us to have more than one receiver as well for the request. In chain of responsibility, sender sends a request to a chain of objects. The request can be handled by any object in the chain. A chain of responsibility pattern says that just avoiding coupling the sender of a request to its receiver by giving multiple objects a chance to handle the request. In other words, 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. Let's look at the UML of chain of responsibility pattern. In this UML, handler is an interface which will primarily receive the request and dispatch the request to a chain of handlers. It has reference only to the first handler in the chain and does not know anything about the rest of the handlers. Concrete handlers are actual handlers of the request chained in some sequential order.

Client is the originator of the request, and this will access the handler to handle it. So, let's look at the advantages of this pattern. This pattern reduces coupling and it adds flexibility when assigning responsibilities to objects. Chain of responsibility allows a set of classes to act as one. Let's ask another question, in what scenarios would you use this pattern? To use this pattern, some conditions must be met. If more than one object can handle the request and the handler is unknown, or a request is to be issued to one of several objects and the receiver is not specified explicitly. Also, the set of objects able to handle the request is to be specified dynamically. After these conditions have been met, you can select the chain of responsibility pattern. Now, there are two most commonly used examples for this pattern, a log system and an ATM dispenser. In my opinion, the ATM example is better than the log system example. So, let's use the ATM example to create a project using the chain of responsibility pattern. First, create a new project and name it chain of responsibility. We can create a class currency that will store the amount. First, we need a property as amount. Now in constructor we will get this as parameter.

Let's add a getter for amount. Now we need a base interface name, will be DispenseChain. We will need two methods. First, set next. Second will be Dispense. We need to create different processor classes that will implement as the dispense chain interface and provide implementation of dispense methods. We will use £10, £20, and £50. First one is £10. Implements DispenseChain. Define DispenseChain. Now we will override methods. Done. Let's go on with £20. Last one is £50. Done. Now we need an ATM dispense chain class to use altogether. We have to start with the largest amount. So, we will start with £50. Now define 20, define 10. Now we need next chain method, £50 next is must be 20, and £20 next will be 10. Done. Now we can use on client page. First, define an ATM dispense chain. Now, we will get value from user. Now we need a loop. I think the best loop is while for this situation.

Amount=0. Now, we need a condition to make sure that money amount can be divided 10. Now, we will start with 50. Now, we have finished because 50 will send us to 20 and 20 will send us to 10, and the program will be finished. Let's try. First, let's try 310. Now try 170. As you can see, our project is a success. This is how the chain of responsibility pattern works. In the following video, we will examine the command pattern. See you in the next video.

 

About the Author
Students
1911
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics