Catalog of Python Patterns? [LONG!]

Paul Rubin phr-n2002a at nightsong.com
Wed Apr 3 21:04:39 EST 2002


jimd at vega.starshine.org (Jim Dennis) writes:
>  GoF Design Patterns in Python
> 
>  [Warning LONG POST AHEAD!]
> 
>  I'd like to see a canonical catalog of OODP in Python.
> 
>  At a minimum it should list each of the 23 GoF patterns, and
>  at least one minimal Python implementation of that pattern.

Yucch, I've never been a fan of that book, it seems too cultish to me.

>  Creational::
>    Abstract Factory:
>     I've never understood the difference between an "abstract factory"
>     and a "factory method."  Since Python doesn't really have 
>     "abstract" versus "concrete" classes, perhaps the distinction is
>     meaningless.
> 
>     Every Python class acts like a factory.  anObj = Foo() returns
>     a reference to a new "Foo" object.  Additionally Python allows
>     us to define normal functions which return object references.

I think the idea here is an abstract factory returns an instance that's
some descendant of a class, rather than necessarily a direct member.
For example, you might have a general "polygon" class, and subclasses
"pentagon" and "hexagon" for 5- and 6-sided polygons.  Then

   p = PolygonFactory(5)

could return a pentagon object, while

   p = PolygonFactory(9)

would return a general polygon with 9 sides.

The only way I know of to do that in Python with a class constructor
rather than a function involves using metaclasses in a fairly hairy way.



More information about the Python-list mailing list