python design patterns video lecturesIn my previous post about learning Python programming through video lectures I stopped at three lectures on Design Patterns. This time I continue from there.

If you don't know what a Design Pattern is, think of it as a simple solution to a specific problem that occurs very frequently in software design.

For example, suppose you use a bunch of unrelated pieces of code. It is a nice idea to bring the unrelated pieces of code together in a unified interface. This design pattern is called Facade. There are a bunch of patterns like this one.

The three lectures are given by Alex Martelli who works as "Über Tech Lead" for Google.

Python Design Patterns, Part I

Alex briefly covers the history and main principles of Design Patterns and quickly moves to discussing Structural and Behavioral DPs in Python.

Interesting ideas from the lecture:

  • [03:24] The name "Design Patterns" was first used by Christopher Alexander, an architect, who abstracted the idea of building buildings as building them using well known patterns which can be applied to the same problem over and over again without ever doing it the same way twice.
  • [05:30] Design Patterns are mostly applied to Object Oriented programming because it's the most widely spread programming paradigm nowadays.
  • [08:36] Design Patterns are not invented, they are discovered.
  • [10:00] Alex says that the original book Design Patterns by the Gang of Four should be read only when you are a master of DPs.
  • [13:10] Three classical categories of DPs are - Creational (deal with object instantiaton), Structural (deal with composition of objects) and Behavioral (deal with interaction of objects).
  • [14:05] "Program to an interface, not to an implementation."
  • [17:00] Use inheritance only when absolutely necessary, otherwise use "hold or wrap" principle.
  • [18:30] Never have more than one dot - Law of Demeter.
  • [18:50] Inheritance cannot restrict, use wrapping to restrict.
  • [21:41] In most of the cases when you need a single instance of something in Python, use a module instead of a class.
  • [22:23] Otherwise, just make 1 instance (without enforcing one).
  • [22:59] Singleton is also called "Highlander".
  • [24:50] There is basically no way to support subclassing well in Singleton.
  • [25:45] Monostate is also called "Borg".
  • [27:00] Python's data overriding helps in Monostate Design Pattern.
  • [29:00] Each Python's type/class is essentially a factory.
  • [32:06] Python does a "two-phase object construction".
  • [35:30] Adapter Design Pattern (it tweaks the interface to your needs).
  • [41:22] Facade Design Pattern (it provides a simple subset of a complex functionality).
  • [47:25] Bridge Design Pattern (it abstracts interface from the implementation).
  • [49:30] Decorator Design Pattern (it transparently modifies some functionality.).
  • [50:24] Proxy Design Pattern (sounds the same as decorator just for access control).
  • [51:21] Q and A!

Python Design Patterns, Part II

In this lecture Alex discusses behavioral patterns. Unlike the first part, he goes in depth of some of the patterns and explains how they can be implemented in Python.

Interesting ideas from the lecture:

  • [02:25] Template Method is a great pattern with a lousy name, a better name is "self-delegation".
  • [03:43] Example of Template Method Design Pattern (text pagination).
  • [08:50] Template Method Rationale.
  • [09:45] The "Hollywood Principle" - "don't call us, we'll call you"
  • [12:05] In Python you can also override data.
  • [13:10] Example of Template Method in Queue.Queue.
  • [14:05] If you are a good Python programmer, use Queue in threaded applications.
  • [17:45] Customizing Queue.
  • [19:30] Example of Template Method in cmd.Cmd.cmdloop.
  • [21:22] Example of Template Method in asyncore.dispatcher.
  • [22:30] Variant of Template Method - Mixin (not presented in Gang of Four book). It's a class to be multiply-inherited from and supplies organizing methods only.
  • [25:50] Template Method in DictMixin class.
  • [26:45] Example of DictMixin usage.
  • [29:00] Hooks can be factored out in another class. Two examples of this from Python's stdlib are HTML's formatter vs. writer, SAX's parser vs. handler
  • [32:40] Hook method introspection example of cmd.Cmd.docmd.
  • [33:30] There are three kinds of Template Methods - plain, factored into separate classes, and introspective.
  • [34:35] Example of all three kinds of Template Methods used in unittest.TestCase.
  • [36:17] State and Strategy Design Patterns. Very similar classes in what they do. They both factor out object's behavior.
  • [40:40] Ring buffer example done via State Design Pattern.
  • [43:35] Q and A!

Python Design Patterns, A Recap

This video lecture was presented at Google Developers day. It is a short version of the previous two video lectures. It starts with an example of Facade Design Pattern, moves on to history and all the types of design patterns.

I did not write out the interesting moments from this lecture as it was a subset of previous two lectures.

If you liked these lectures, check out this geek song about another commonly used design pattern - Model-View-Controller Song.

Even though these were Python design patterns, to understand some of them I used Perl Design Patterns website.

See ya!