principlepythonMinor
Strategy Design Pattern in Python
Viewed 0 times
pythondesignpatternstrategy
Problem
I'm reading the awesome Head First Design Patterns book. As an exercise I converted first example (or rather a subset of it) to Python. The code I got is rather too simple, i.e. there is no abstract nor interface declarations, it's all just 'classes'. Is that the right way to do it in Python? My code is below, original Java code and problem statement can be found at http://books.google.com/books?id=LjJcCnNf92kC&pg=PA18
class QuackBehavior():
def __init__(self):
pass
def quack(self):
pass
class Quack(QuackBehavior):
def quack(self):
print "Quack!"
class QuackNot(QuackBehavior):
def quack(self):
print "..."
class Squeack(QuackBehavior):
def quack(self):
print "Squeack!"
class FlyBehavior():
def fly(self):
pass
class Fly():
def fly(self):
print "I'm flying!"
class FlyNot():
def fly(self):
print "Can't fly..."
class Duck():
def display(self):
print this.name
def performQuack(self):
self.quackBehavior.quack()
def performFly(self):
self.flyBehavior.fly()
class MallardDuck(Duck):
def __init__(self):
self.quackBehavior = Quack()
self.flyBehavior = Fly()
if __name__ == "__main__":
mallard = MallardDuck()
mallard.performQuack()
mallard.performFly()
mallard.flyBehavior = FlyNot()
mallard.performFly()Solution
class QuackBehavior():Don't put
() after classes, either skip it, or put object in theredef __init__(self):
passThere's no reason to define a constructor if you aren't going to do anything, so just skip it
def quack(self):
passYou should probably at least
raise NotImplementedError(), so that if anyone tries to call this it'll complain. That'll also make it clear what this class is doing.You don't really need this class at all. The only reason to provide classes like this in python is documentation purposes. Whether or not you think that's useful enough is up to you.
class Quack(QuackBehavior):
def quack(self):
print "Quack!"
class QuackNot(QuackBehavior):
def quack(self):
print "..."
class Squeack(QuackBehavior):
def quack(self):
print "Squeack!"
class FlyBehavior():
def fly(self):
pass
class Fly():If you are going to defined FlyBehavior, you should really inherit from it. It just makes it a litle more clear what you are doing.
def fly(self):
print "I'm flying!"
class FlyNot():
def fly(self):
print "Can't fly..."
class Duck():I'd define a constructor taking the quack and fly behavior here.
def display(self):
print this.name
def performQuack(self):
self.quackBehavior.quack()
def performFly(self):
self.flyBehavior.fly()
class MallardDuck(Duck):
def __init__(self):
self.quackBehavior = Quack()
self.flyBehavior = Fly()There's not really a reason for this to be a class. I'd make a function that sets up the Duck and returns it.
if __name__ == "__main__":
mallard = MallardDuck()
mallard.performQuack()
mallard.performFly()
mallard.flyBehavior = FlyNot()
mallard.performFly()Code Snippets
class QuackBehavior():def __init__(self):
passdef quack(self):
passclass Quack(QuackBehavior):
def quack(self):
print "Quack!"
class QuackNot(QuackBehavior):
def quack(self):
print "..."
class Squeack(QuackBehavior):
def quack(self):
print "Squeack!"
class FlyBehavior():
def fly(self):
pass
class Fly():def fly(self):
print "I'm flying!"
class FlyNot():
def fly(self):
print "Can't fly..."
class Duck():Context
StackExchange Code Review Q#23033, answer score: 7
Revisions (0)
No revisions yet.