Strategy Design Pattern

Daniel Santa Cruz dstcruz at gmail.com
Fri Apr 21 01:32:09 EDT 2006


Hello all,

I've been trying to go over my OO Patterns book, and I decided to try
to implement them in Python this time around.  I figured this would
help me learn the language better.

Well, I've gotten stuck with my first go at OO patterns with Python.  I
guess it goes without say that some of the stuff that are taken for
granted in most of the books (ie. Interfaces, Abstract classes) don't
really apply to Python per say, but the idea behind the patterns can be
extracted out still.  In the specific case of the Strategy pattern, I
think the goal is to abstract out of the class an algorithm that can
then be reused by some of the inherited classes.  This way we don't
repeat ourselves.  Maybe I got this all wrong...

I'm at a loss at how I can do this with Python, any pointers would be
more than welcomed!

To aid commenters... we can use the example used in "Head First Design
Patterns".  I've implemented this simple patter in Java and .NET... now
in python.  I can't draw UML here, so I'll try to pseudo talk what the
example has:

Abstract Base Class: Duck
+ FlyBehavior _fly
+ swim()
+ fly() -> calls _fly.fly()

Interface: FlyBehavior
+ fly()

Concrete Interface FlyHigh (implements FlyBehavior):
+ fly()

Concrete Class Duck1 (Inherits Duck):
+ Constructor: _fly = new FlyHigh()

Daniel.




More information about the Python-list mailing list