From the course: Java Design Patterns: Behavioral Part 1
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Solution: The Chain of Responsibility pattern - Java Tutorial
From the course: Java Design Patterns: Behavioral Part 1
Solution: The Chain of Responsibility pattern
(upbeat music) - [Instructor] Now let's have a look at my solution to this exercise. So, first of all, in my AuthenticationHandler class I've created a constructor. This takes another AuthenticationHandler as an argument and inside the constructor I'm assigning that to a field called next. In the handleRequest method I've got a condition that checks if next is no and if it's not, then I call the handleRequest method on the next variable. This is how requests will get passed along the chain. Next let's take out a concrete implementation of this class. So if I go to basic AuthenticationHandler, I can see that I've got a constructor and this takes an AuthenticationHandler as an argument too and then it calls super and passes in this Handler. In the handleRequest method, I have a condition to check if the requestType that's I'm passing in is equal to basic and if it is then I go ahead and print out Handling basic…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.