Skip to content

Latest commit

 

History

History

abstract_factory

Abstract Factory Design Pattern

Videos

Section Video Links
Abstract Factory Overview Abstract Factory Overview Abstract Factory Overview Abstract Factory Overview
Abstract Factory Use Case Abstract Factory Use Case Abstract Factory Use Case Abstract Factory Use Case
Exception Handling Exception Handling Exception Handling Exception Handling

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.

Abstract Factory UML Diagram

Abstract Factory Overview

Output

python ./abstract_factory/abstract_factory_concept.py
<class 'factory_a.ConcreteProductB'>
<class 'factory_b.ConcreteProductC'>

Abstract Factory Example Use Case

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

Abstract Factory Example UML Diagram

See this UML diagram of an Abstract Furniture Factory implementation that returns chairs and tables.

Abstract Furniture Factory

Output

python ./abstract_factory/client.py
<class 'small_chair.SmallChair'> : {'width': 40, 'depth': 40, 'height': 40}
<class 'medium_table.MediumTable'> : {'width': 110, 'depth': 70, 'height': 60}

New Coding Concepts

Exception Handling

Your Python code may produce errors. It happens to everybody. It is hard to foresee all possible errors, but you can try to handle them in case anyway.

Use the Try, Except and optional finally keywords to manage error handling.

In the example code, if no chair or table is returned, an Exception error is raised, and it includes a text string that can be read and written to the console.

Within your code you can use the raise keyword to trigger Python built in exceptions or even create your own.

def get_furniture(furniture):
    "Static get_factory method"
    try:
        if furniture in ['SmallChair', 'MediumChair', 'BigChair']:
            return ChairFactory.get_chair(furniture)
        if furniture in ['SmallTable', 'MediumTable', 'BigTable']:
            return TableFactory.get_table(furniture)
        raise Exception('No Factory Found')
    except Exception as _e:
        print(_e)
    return None

If WoodenTable is requested from the factory, it will print No Factory Found

You don't need to always raise an exception to make one happen. In that case you can handle the possibility of any type of error using just try and except, with the optional finally if you need it.

try:
  print(my_var)
except:
  print("An unknown error Occurred")
finally:
  print("This is optional and will get called even if there is no error")

The above code produces the message An Error Occurred because my_var is not defined.

The try/except allows the program to continue running, as can be verified by the line printed in the finally statement. So, this has given you the opportunity to manage any unforeseen errors any way you wish.

Alternatively, if your code didn't include the try/except and optional finally statements, the Python interpreter would return the error NameError: name 'my_var' is not defined and the program will crash at that line.

Also note how the default Python inbuilt error starts with NameError. You can handle this specific error explicitly using an extra except keyword.

try:
    print(my_var)
except NameError:
    print("There was a `NameError`")
except:
    print("An unknown error Occurred")
finally:
    print("This is optional and will get called even if there is no error")

You can add exception handling for as many types of errors as you wish.

Python Errors and Exceptions : https://docs.python.org/3/tutorial/errors.html

Summary

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