SimUDuck.py

__author__ = 'jasonjkeller'
 
class FlyBehavior(object):
def fly(self):
pass
class FlyNoWay(FlyBehavior):
def fly(self):
print "I can't fly"
class FlyRocketPowered(FlyBehavior):
def fly(self):
print "I'm flying with a rocket"
class FlyWithWings(FlyBehavior):
def fly(self):
print "I'm flying!!"
 
class QuackBehavior(object):
def quack(self):
pass
class Quack(QuackBehavior):
def quack(self):
print "Quack"
class FakeQuack(QuackBehavior):
def quack(self):
print "Qwak"
class MuteQuack(QuackBehavior):
def quack(self):
print "<< Silence >>"
class Squeak(QuackBehavior):
def quack(self):
print "Squeak"
 
class Duck(object):
flyBehavior = FlyBehavior()
quackBehavior = QuackBehavior()
def setFlyBehavior(self, fb):
self.setFlyBehavior = fb
def setQuackBehavior(self, qb):
self.setQuackBehavior = qb
def Display(self):
pass
def performFly(self):
self.flyBehavior.fly()
def performQuack(self):
self.quackBehavior.quack()
def swim(self):
print "All ducks float, even decoys!"

class MallardDuck(Duck):
def __init__(self):
self.quackBehavior = Quack()
self.flyBehavior = FlyWithWings()
def Display(self):
print "I'm a real Mallard duck"
class DecoyDuck(Duck):
def __init__(self):
self.quackBehavior = MuteQuack()
self.flyBehavior = FlyNoWay()
def Display(self):
print "I'm a duck Decoy"
class ModelDuck(Duck):
def __init__(self):
self.quackBehavior = Quack()
self.flyBehavior = FlyNoWay()
def Display(self):
print "I'm a model duck"
class RubberDuck(Duck):
def __init__(self):
self.quackBehavior = Squeak()
self.flyBehavior = FlyNoWay()
def Display(self):
print "I'm a rubber duckie"
class RedHeadDuck(Duck):
def __init__(self):
self.quackBehavior = Quack()
self.flyBehavior = FlyWithWings()
def Display(self):
print "I'm a real Red Headed duck"
 
def main():
mallard = MallardDuck()
rubberDuckie = RubberDuck()
decoy = DecoyDuck()
model = ModelDuck()
mallard.performQuack()
rubberDuckie.performQuack()
decoy.performQuack()
model.performFly()
model.setFlyBehavior(FlyRocketPowered())
model.performFly()
main()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s