From the course: Python: Design Patterns (2021)
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Chain of responsibility example - Python Tutorial
From the course: Python: Design Patterns (2021)
Chain of responsibility example
- [Instructor] Let's start, by first defining our handler class, which is an abstract handler. Since the point of a handler is to find its successor. If the current handler cannot handle a request, we need an attribute to store a successor handler. Type self._successor. The successor handler is coming in as an argument, in the in it method, the next method is the handle method in which will first try to handle the request. To handle the request we call _handle method. If this method is able to handle the request, it will return the true value. The next part of our code is checking the value, in the handled variable as you can see here. If the _handle request method, couldn't handle the request, then we have to do something else, which is using a successor. That's why we type self._successor.handle request, this _handle method provides an interface, to be implemented by the following concrete handler classes. Let's go…
Contents
-
-
-
-
-
-
(Locked)
Observer1m 5s
-
(Locked)
Observer example6m 22s
-
(Locked)
Visitor56s
-
(Locked)
Visitor example6m 42s
-
(Locked)
Iterator1m 15s
-
(Locked)
Iterator example4m 32s
-
(Locked)
Strategy40s
-
(Locked)
Strategy example5m 44s
-
(Locked)
Chain of responsibility1m 4s
-
(Locked)
Chain of responsibility example5m 57s
-
(Locked)
-
-