summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-06-07 14:57:55 -0700
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-06-07 14:57:55 -0700
commit9345562cc88f31792306d92ee7992444b9b1462d (patch)
tree9c16129bb0e1759b1f4d9731c38e02885df53977 /docs
parent714521a4c7a6814c365c061bcf967c4c45692e3d (diff)
docs: Add note about composition to the design guide.
Diffstat (limited to 'docs')
-rw-r--r--docs/design_guide.rst20
1 files changed, 20 insertions, 0 deletions
diff --git a/docs/design_guide.rst b/docs/design_guide.rst
index 8ce47041f..6fe54e70e 100644
--- a/docs/design_guide.rst
+++ b/docs/design_guide.rst
@@ -289,6 +289,26 @@ SPI Example
i2c.readinto(self.buf)
return self.buf[0]
+Use composition
+--------------------------------------------------------------------------------
+
+When writing a driver, take in objects that provide the functionality you need
+rather than taking their arguments and constructing them yourself or subclassing
+a parent class with functionality. This technique is known as composition and
+leads to code that is more flexible and testable than traditional inheritance.
+
+.. seealso:: `Wikipedia <https://en.wikipedia.org/wiki/Dependency_inversion_principle>`_
+ has more information on "dependency inversion".
+
+For example, if you are writing a driver for an I2C device, then take in an I2C
+object instead of the pins themselves. This allows the calling code to provide
+any object with the appropriate methods such as an I2C expansion board.
+
+Another example is to expect a `DigitalInOut` for a pin to toggle instead of a
+`microcontroller.Pin` from `board`. Taking in the `~microcontroller.Pin` object
+alone would limit the driver to pins on the actual microcontroller instead of pins
+provided by another driver such as an IO expander.
+
Lots of small modules
--------------------------------------------------------------------------------