Skip to content

Instantly share code, notes, and snippets.

@kpsychas
Last active November 22, 2019 07:01
Show Gist options
  • Save kpsychas/1e719b93528be0c952b9a31a8e2a0d66 to your computer and use it in GitHub Desktop.
Save kpsychas/1e719b93528be0c952b9a31a8e2a0d66 to your computer and use it in GitHub Desktop.
The bridge design pattern retrieved from stackoverflow archived documentation
class Spellbook:
def cast(self, enemy):
raise NotImplementedError
class SpellbookFire(Spellbook):
def cast(self, enemy):
print("I've lit the {} on fire.".format(enemy))
class SpellbookFreeze(Spellbook):
def cast(self, enemy):
print("I've frozen the {}".format(enemy))
class Wizard:
spellbook = None
def cast_spell(self, enemy):
raise NotImplementedError
class BattleWizard(Wizard):
def __init__(self, spellbook):
self.spellbook = spellbook
def cast_spell(self, enemy):
print ("Yarrrrr!")
self.spellbook.cast(enemy)
class TimidWizard(Wizard):
def __init__(self, spellbook):
self.spellbook = spellbook
def cast_spell(self, enemy):
print ("Oh dear!")
self.spellbook.cast(enemy)
battle_wizard = BattleWizard(SpellbookFire())
timid_wizard = TimidWizard(SpellbookFreeze())
battle_wizard.cast_spell('goblin') # output: "Yarrrr!"
# "I've lit the goblin on fire."
timid_wizard.cast_spell('goblin') # output: "Oh dear!"
# "I've frozen the goblin."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment