Skip to content

Files

Latest commit

b79e663 · Oct 30, 2023

History

History

singleton

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Oct 30, 2023
Feb 27, 2021
Feb 27, 2021
Feb 27, 2021
Feb 27, 2021
Feb 27, 2021
Mar 9, 2021
Oct 12, 2022

README.md

Singleton Design Pattern

Videos

Section Video Links
Singleton Overview Singleton Overview   Singleton Overview   Singleton Overview
Singleton Use Case Singleton Use Case   Singleton Use Case   Singleton Use Case
Python Dictionary Python Dictionary   Python Dictionary   Python Dictionary

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 or Design Patterns In Python website to read textual content.

Singleton UML Diagram

Singleton UML Diagram

Output

python ./singleton/singleton_concept.py
id(Singleton)   = 2164775087968
id(OBJECT1)     = 2164775087968
id(OBJECT2)     = 2164775087968
id(OBJECT3)     = 2164775087968

... Refer to Book or Design Patterns In Python website to read textual content.

Example Use Case

... Refer to Book or Design Patterns In Python website to read textual content.

Example UML Diagram

Singleton Use Case Diagram

Output

python ./singleton/client.py
-----------Leaderboard-----------
|       1       |       Emmy    |
|       2       |       Cosmo   |
|       3       |       Sean    |

-----------Leaderboard-----------
|       1       |       Emmy    |
|       2       |       Cosmo   |
|       3       |       Sean    |

-----------Leaderboard-----------
|       1       |       Emmy    |
|       2       |       Cosmo   |
|       3       |       Sean    |

New Coding Concepts

Python Dictionary

In the file /singleton/leaderboard.py,

    "The Leaderboard as a Singleton"
    _table = {}

The {} is indicating a Python Dictionary.

A Dictionary can be instantiated using the curly braces {} or dict()

The Dictionary is similar to a List, except that the items are key:value pairs.

The Dictionary can store multiple key:value pairs, they can be changed, can be added and removed, can be re-ordered, can be pre-filled with key:value pairs when instantiated and is very flexible.

Since Python 3.7, dictionaries are ordered in the same way that they are created.

The keys of the dictionary are unique.

You can refer to the dictionary items by key, which will return the value.

PS> python
>>> items = {"abc": 123, "def": 456, "ghi": 789}
>>> items["abc"] 
123

You can change the value at a key,

PS> python
>>> items = {"abc": 123, "def": 456, "ghi": 789}
>>> items["def"] = 101112 
>>> items["def"]
101112

You can add new key:value pairs, and remove them by using the key.

PS> python
>>> items = {"abc": 123, "def": 456, "ghi": 789}
>>> items["jkl"] = 101112
>>> items["jkl"]
101112
>>> items.pop('def')
456
>>> items
{'abc': 123, 'ghi': 789, 'jkl': 101112}

You can order a dictionary alphabetically by key

PS> python
>>> items = {"abc": 123, "ghi": 789, "def": 456}
>>> items
{'abc': 123, 'ghi': 789, 'def': 456}
>>> dict(sorted(items.items()))
{'abc': 123, 'def': 456, 'ghi': 789}

Summary

... Refer to Book or Design Patterns In Python website to read textual content.