Last active
August 4, 2021 16:26
-
-
Save omarjcero/deb37917494268c7a4886fe2d480879e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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