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()