Skip to content

Latest commit

 

History

History
 
 

chain_of_responsibility

Chain of Responsibility Design Pattern

Videos

Section Video Links
Chain of Responsibility Chain of Responsibility Overview Chain of Responsibility Overview Chain of Responsibility Overview
Use Case Chain of Responsibility Use Case Chain of Responsibility Use Case Chain of Responsibility Use Case
Python Floor Division Python Floor Division Python Floor Division Python Floor Division
Accepting User Input Accepting User Input Accepting User Input Accepting User Input

Book

Cover Links
Design Patterns In Python (ASIN : B08XLJ8Z2J)    https://www.amazon.com/dp/B08XLJ8Z2J
   https://www.amazon.co.uk/dp/B08XLJ8Z2J
   https://www.amazon.in/dp/B08Z282SBC
   https://www.amazon.de/dp/B08XLJ8Z2J
   https://www.amazon.fr/dp/B08XLJ8Z2J
   https://www.amazon.es/dp/B08XLJ8Z2J
   https://www.amazon.it/dp/B08XLJ8Z2J
   https://www.amazon.co.jp/dp/B08XLJ8Z2J
   https://www.amazon.ca/dp/B08XLJ8Z2J
   https://www.amazon.com.au/dp/B08Z282SBC

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

Chain of Responsibility Design Pattern

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

Chain of Responsibility Design Pattern

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 yourself

New 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.,

PS> python
>>> 9 / 3
3.0

Python Version 3 also has an option to return an integer version (floor) of the number by using the double // characters instead.

PS> python
>>> 9 // 3
3

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.