I've recently picked up the Head First Design Patterns book in an effort to become a more efficient and better Python programmer. Unfortunately, the code examples in this book are in Java.
I'm not the first one that wishes there was a Python version of this book, but I thought that writing the Python code would be a great exercise.
I've tried to implement the Strategy design pattern from Chapter 1 of Head First Design Patterns below in Python. Now, I know that Python is not Java in a general sense, but not in the nitty-gritty sense. So, I'm sure there are more Pythonic things that I can do to this code.
Off the top of my head we could probably implement the Duck
class set_fly_behavior()
and set_quack_behavior()
using the Python property
built-in.
Any other suggestions?
#!/usr/bin/env python
################################################################################
# Abstract Duck class and concrete Duck type classes.
################################################################################
class Duck(object):
def __init__(self):
pass
def set_fly_behavior(self, fb):
self.fly_behavior = fb
def set_quack_behavior(self, qb):
self.quack_behavior = qb
def perform_fly(self):
self.fly_behavior.fly()
def perform_quack(self):
self.quack_behavior.quack()
def swim(self):
print "All ducks float, even decoys!"
class MallardDuck(Duck):
def __init__(self):
Duck.__init__(self)
fly_instance = Fly()
self.set_fly_behavior(fly_instance)
quack_instance = Quack()
self.set_quack_behavior(quack_instance)
print "I'm a real Mallard Duck"
class ModelDuck(Duck):
def __init__(self):
Duck.__init__(self)
fly_no_way_instance = FlyNoWay()
self.set_fly_behavior(fly_no_way_instance)
squeak_instance = SqueakQuack()
self.set_quack_behavior(squeak_instance)
print "I'm a Model Duck"
class DecoyDuck(Duck):
def __init__(self):
Duck.__init__(self)
fly_no_way_instance = FlyNoWay()
self.set_fly_behavior(fly_no_way_instance)
mute_instance = MuteQuack()
self.set_quack_behavior(mute_instance)
print "I'm a Decoy Duck"
################################################################################
# Fly behavior interface and behavior implementation classes.
################################################################################
class FlyBehavior():
def __init__(self):
pass
class Fly(FlyBehavior):
def fly(self):
print "I'm flying!!"
class FlyNoWay(FlyBehavior):
def fly(self):
print "I can't fly"
class FlyRocketPowered(FlyBehavior):
def fly(self):
print "I'm flying with a rocket!"
################################################################################
# Quack behavior interface and behavior implementation classes.
################################################################################
class QuackBehavior():
def __init__(self):
pass
class Quack(QuackBehavior):
def quack(self):
print "Quack"
class MuteQuack(QuackBehavior):
def quack(self):
print "<< Silence >>"
class SqueakQuack(QuackBehavior):
def quack(self):
print "Squeak"
################################################################################
# Test Code.
################################################################################
if __name__ == "__main__":
print '#'*80
print "Mallard Duck"
print '#'*80
mallard = MallardDuck()
mallard.perform_quack()
mallard.perform_fly()
print '#'*80
print "Model Duck"
print '#'*80
model = ModelDuck()
model.perform_fly()
fly_rocket_powered_instance = FlyRocketPowered()
model.set_fly_behavior(fly_rocket_powered_instance)
model.perform_fly()
print '#'*80
print "Decoy Duck"
print '#'*80
decoy = DecoyDuck()
decoy.perform_fly()
decoy.perform_quack()