| Section | Video Links |
|---|---|
| Abstract Factory Overview | ![]() |
| Abstract Factory Use Case | ![]() |
| Exception Handling | ![]() |
... Refer to Design Patterns In Python website to read textual content.
... Refer to Design Patterns In Python website to read textual content.
python ./abstract_factory/abstract_factory_concept.py
<class 'factory_a.ConcreteProductB'>
<class 'factory_b.ConcreteProductC'>... Refer to Design Patterns In Python website to read textual content.
See this UML diagram of an Abstract Furniture Factory implementation that returns chairs and tables.
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}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 NoneIf 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
... Refer to Design Patterns In Python website to read textual content.



