Skip to content

Latest commit

 

History

History

composite

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Composite Design Pattern

Videos

Section Video Links
Composite Overview Composite Overview Composite Overview Composite Overview
Composite Use Case Composite Use Case Composite Use Case Composite Use Case
Conditional Expressions Conditional Expressions Conditional Expressions Conditional Expressions

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.

Terminology

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

Composite UML Diagram

Composite Pattern UML Diagram

Source Code

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

Output

python ./composite/composite_concept.py

LEAF_A          id:2050574298848
LEAF_B          id:2050574298656
COMPOSITE_1     id:2050574298272
COMPOSITE_2     id:2050574298128

<Leaf>          id:2050574298656        Parent: None
<Composite>     id:2050574298128        Parent: None    Components:2
<Leaf>          id:2050574298848        Parent: 2050574298128
<Composite>     id:2050574298272        Parent: 2050574298128   Components:0

Composite Example Use Case

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

Composite Example UML Diagram

Composite Pattern Use Case UML Diagram

Output

python ./composite/client.py
<DIR>  root             id:2028913323984        Components: 4
..<FILE> abc.txt        id:2028913323888        Parent: 2028913323984
..<FILE> 123.txt        id:2028913323792        Parent: 2028913323984
..<DIR>  folder_a       id:2028913432848        Components: 1
....<FILE> xyz.txt      id:2028913433088        Parent: 2028913432848
..<DIR>  folder_b       id:2028913433184        Components: 1
....<FILE> 456.txt      id:2028913434432        Parent: 2028913433184

<DIR>  root             id:2028913323984        Components: 3
..<FILE> abc.txt        id:2028913323888        Parent: 2028913323984
..<FILE> 123.txt        id:2028913323792        Parent: 2028913323984
..<DIR>  folder_b       id:2028913433184        Components: 2
....<FILE> 456.txt      id:2028913434432        Parent: 2028913433184
....<DIR>  folder_a     id:2028913432848        Components: 1
......<FILE> xyz.txt    id:2028913433088        Parent: 2028913432848

New Coding Concepts

Conditional Expressions (Ternary Operators).

In /composite/composite_concept.py, there are two conditional expressions.

Conditional expressions an alternate form of if/else statement.

id(self.reference_to_parent) if self.reference_to_parent is not None else None

If the self.reference_to_parent is not None, it will return the memory address (id) of self.reference_to_parent, otherwise it returns None.

This conditional expression follows the format

value_if_true if condition else value_if_false

eg,

SUN = "bright"
SUN_IS_BRIGHT = True if SUN == "bright" else False
print(SUN_IS_BRIGHT)

or

ICE_IS_COLD = True
ICE_TEMPERATURE = "cold" if ICE_IS_COLD == True else "hot"
print(ICE_TEMPERATURE)

or

CURRENT_VALUE = 99
DANGER = 100
ALERTING = True if CURRENT_VALUE >= DANGER else False
print(ALERTING)

Visit https://docs.python.org/3/reference/expressions.html#conditional-expressions for more examples of conditional expressions.

Summary

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