Skip to content

Latest commit

 

History

History
 
 

observer

Observer Pattern

Videos

Section Video Links
Observer Overview Observer Overview Observer Overview Observer Overview
Observer Use Case Observer Use Case Observer Use Case Observer Use Case
Python Set Python Set Python Set Python Set

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.

Observer UML Diagram

Observer Pattern Overview

Source Code

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.

Output

python ./observer/observer_concept.py
Observer id:2084220160272 received ('First Notification', [1, 2, 3])
Observer id:2084220160224 received ('First Notification', [1, 2, 3])
Observer id:2084220160272 received ('Second Notification', {'A': 1, 'B': 2, 'C': 3})

Example Use Case

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.

Example UML Diagram

Observer Pattern in Context

Output

python ./observer/client.py
PieGraph, id:1
Drawing a Pie graph using data:[1, 2, 3]
BarGraph, id:2
Drawing a Bar graph using data:[1, 2, 3]
TableView, id:3
Drawing a Table view using data:[1, 2, 3]
PieGraph, id:1
Drawing a Pie graph using data:[4, 5, 6]
TableView, id:3
Drawing a Table view using data:[4, 5, 6]

New Coding Concepts

Python Set

A Python Set is similar to a List. Except that the items in the Set are guaranteed to be unique, even if you try to add a duplicate. A set is a good choice for keeping a collection of observables, since the problem of duplicate observables is automatically handled.

A Set can be instantiated using the curly braces {} or set(), verses [] for a List and () for a Tuple. It is not the same as a Dictionary, which also uses {}, since the dictionary items are created as key:value pairs. ie {"a": 1, "b": 2, "c": 3}

PS> python
>>> items = {"yankee", "doodle", "dandy", "doodle"}
>>> items
{'yankee', 'doodle', 'dandy'}
>>> items.add("grandy")
>>> items
{'grandy', 'yankee', 'doodle', 'dandy'}
>>> items.remove("doodle")
>>> items
{'grandy', 'yankee', 'dandy'}

Note, if instantiating an empty Set then use my_object = Set() rather than my_object = {} to reduce ambiguity with creating an empty Dictionary.

Summary

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.