课程: Python: Design Patterns (2021)

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Chain of responsibility example

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…

内容