Skip to content

Latest commit

 

History

History

builder

Builder Design Pattern

Videos

Section Video Links
Builder Overview Builder Overview Builder Overview Builder Overview
Builder Use Case Builder Use Case Builder Use Case Builder Use Case
Python List Python List Python List Python List

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.

Builder UML Diagram

Builder Pattern Overview

Source Code

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

Output

python ./builder/builder_concept.py
['a', 'b', 'c']

Example Use Case

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

Example UML Diagram

Builder Pattern in Context

Output

python ./builder/client.py
This is a Ice Igloo with 1 door(s) and 0 window(s).
This is a Sandstone Castle with 100 door(s) and 200 window(s).
This is a Wood House Boat with 6 door(s) and 8 window(s).

New Coding Concepts

Python List

In the file /builder/builder_concept.py

    def __init__(self):
        self.parts = []

The [] is indicating a Python List.

The list can store multiple items, they can be changed, they can have items added and removed, can be re-ordered, can be pre-filled with items when instantiated and is also very flexible.

PS> python
>>> items = []
>>> items.append("shouldn't've")
>>> items.append("y'aint")
>>> items.extend(["whomst", "superfluity"])
>>> items
["shouldn't've", "y'aint", 'whomst', 'superfluity']
>>> items.reverse()
>>> items
['superfluity', 'whomst', "y'aint", "shouldn't've"]
>>> items.remove("y'aint")
>>> items
['superfluity', 'whomst', "shouldn't've"]
>>> items.insert(1, "phoque")
>>> items
['superfluity', 'phoque', 'whomst', "shouldn't've"]
>>> items.append("whomst")
>>> items.count("whomst")
2
>>> len(items)
5
>>> items[2] = "bagnose"
>>> items
['superfluity', 'phoque', 'bagnose', "shouldn't've", 'whomst']
>>> items[-2]
"shouldn't've"

Lists are used in almost every code example in this book. You will see all the many ways they can be used.

In fact, a list was used in the /abstract_factory/furniture_factory.py example,

if furniture in ['SmallChair', 'MediumChair', 'BigChair']:
    ...

This line, creates a list at runtime including the strings 'SmallChair', 'MediumChair' and 'BigChair'. If the value in furniture equals the same string as one of those items in the list, then the condition is true and the code within the if statement block will execute.

Summary

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