Skip to content

Instantly share code, notes, and snippets.

@omarjcero
Last active August 4, 2021 16:26
Show Gist options
  • Save omarjcero/deb37917494268c7a4886fe2d480879e to your computer and use it in GitHub Desktop.
Save omarjcero/deb37917494268c7a4886fe2d480879e to your computer and use it in GitHub Desktop.
# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
class Duck(object):
# Class attributes definition
def __init__(self):
# Instance attributes definition
self.fly_behavior = None
pass # lots of stuff - but missing something
def setFlyBehavior(self, new_fly_behavior):
self.fly_behavior = new_fly_behavior
def performFly(self):
self.fly_behavior.fly()
pass
# ------------------------------------------
class FlyBehavior(object):
def fly(self):
print("FlyBehavior.fly")
pass # one implementation
class FlyWithWings(FlyBehavior):
def fly(self):
print("FlyWithWings.fly")
pass
class FlyNoWay(FlyBehavior):
def fly(self):
print("FlyNoWay.Nofly")
pass
# ------------------------------------------
class SpecificDuck(Duck):
def __init__(self):
print("Specific duck")
self.fly_behavior = FlyWithWings()
pass
class AnotherSpecificDuck(Duck):
def __init__(self):
print("Another Spec. duck")
self.fly_behavior = FlyNoWay()
pass
# ------------------------------------------
duck = SpecificDuck()
duck.performFly()
print("--------")
duck2 = AnotherSpecificDuck()
duck2.performFly()
duck2.setFlyBehavior(FlyWithWings())
duck2.performFly()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment