Design Pattern Description
History Key
- New content
Removed content
Recent Versions
Choose two versions to compare, or click the link to view it.
Design Patterns in Robot Rock
ExpandingList.py
click here to view source
The class ExpandingList uses the following design patterns:
-
Inheritance or subclassing: the class inherits from
listobject (a python builtin). It overrides the accessor method such that it is not possible to return a null reference from a positive index access call. -
Factory method/Builder: the class uses a factory method (
__default_object()) to build list elements when the list superclass requires expansion. Since the elements are built using only a dictionary and/or arbitrary parameter lists, the factory function is generic for any type of class. In this way, we use the Builder pattern, since the same construction process can create completely different class representations. - Lazy initialization: this pattern is at least partially applied. No list element is instantiated until a reference to its (out-of-bounds) index is made. Once this occurs, the entire list is expanded, and all items in the list are instantiated up until the latest reference.
Python idioms:
- EBFAP: Easier to beg forgiveness than ask permission. Used to catch and handle out of bound list indices in a try/catch block.