Chain of Responsibility Design Pattern
Videos
| Section | Video Links |
|---|---|
| Chain of Responsibility | ![]() |
| Use Case | ![]() |
| Python Floor Division | ![]() |
| Accepting User Input | ![]() |
Book
Overview
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
Terminology
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
Chain of Responsibility UML Diagram
Source Code
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
Output
python ./chain_of_responsibility/chain_of_responsibility_concept.py Successor1 payload = 1 Successor2 payload = -1 Successor2 payload = -0.5 Successor2 payload = -0.25 Successor1 payload = -0.5 Successor1 payload = 0.5 Successor2 payload = -1.5 Finished result = -1.5
Example Use Case
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
Example UML Diagram
Output
python ./chain_of_responsibility/client.py
Enter amount to withdrawal : 180
Dispensing 3 £50 note(s)
Dispensing 1 £20 note(s)
Dispensing 1 £10 note
Now go spoil yourselfNew Coding Concepts
Floor Division
Normally division uses a single / character and will return a float even if the numbers are integers or exactly divisible with no remainder,
E.g.,
Python Version 3 also has an option to return an integer version (floor) of the number by using the double // characters instead.
See PEP-0238 : https://www.python.org/dev/peps/pep-0238/
Accepting User Input
In the file /chain_of_responsibility/client.py above, there is a command input .
The input command allows your script to accept user input from the command prompt.
In the ATM example, when you start it, it will ask the user to enter a number.
Then when the user presses the enter key, the input is converted to an integer and the value tested if valid.
AMOUNT = int(input("Enter amount to withdrawal : ")) if AMOUNT < 10 or AMOUNT % 10 != 0: ...continue
Note that in Python 2.x, use the raw_input() command instead of input() .
See PEP-3111 : https://www.python.org/dev/peps/pep-3111/
Summary
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.



