Section | Video Links |
---|---|
Factory Overview | |
Factory Use Case | |
ABCMeta Module |
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
python ./factory/factory_concept.py
ConcreteProductB
... Refer to Book or Design Patterns In Python website to read textual content.
python ./factory/client.py
{'width': 40, 'depth': 40, 'height': 40}
ABCMeta classes are a development tool that help you to write classes that conform to a specified interface that you've designed.
ABCMeta refers to Abstract Base Classes.
The benefits of using ABCMeta classes to create abstract classes is that your IDE and Pylint will indicate to you at development time whether your inheriting classes conform to the class definition that you've asked them to.
Abstract classes are not instantiated directly in your scripts, but instead inherited by subclasses that will provide the implementation code for the abstract methods. E.g., you don't create IChair
, but you create SmallChair
that implemented the methods described in the IChair
interface.
An abstract method is a method that is declared, but contains no implementation. The implementation happens at the class that inherits the abstract class.
You don't need to use ABCMeta classes and interfaces that you have created in your final python code. You code will still work without them.
You can try it by removing the interfaces from all of the chair classes above, and you will see that your python program will still run.
eg, change
class BigChair(IChair):
to
class BigChair():
and it will still work.
While it is possible to ensure your classes are correct without using abstract classes, it is often easier to use abstract classes as a backup method of checking correctness, especially if your projects become very large and involve many developers.
Note that in all my code examples, the abstract classes are prefixed with a capital I, to indicate that they are abstract interfaces. They have no code in their methods. They do not require a self
or cls
argument due to the use of @staticmethod
. The inheriting class will implement the code in each of the methods that the abstract class is describing. If subclasses are inheriting an abstract base class, and they do not implement the methods as described, there will be Pylint error or warning message (E0110).
See PEP 3119 : https://www.python.org/dev/peps/pep-3119/
... Refer to Book or Design Patterns In Python website to read textual content.