Skip to content

Instantly share code, notes, and snippets.

@renanzulian
Created May 10, 2021 11:43
Show Gist options
  • Save renanzulian/cd9b386cc58a964425483c94b846b49e to your computer and use it in GitHub Desktop.
Save renanzulian/cd9b386cc58a964425483c94b846b49e to your computer and use it in GitHub Desktop.
Strategy design patter - Python
class Budget:
def __init__(self, value):
self._value = value
@property
def value(self):
return self._value
class TaxCalculator:
def calculate(self, budget, tax_strategy):
return tax_strategy.calculate(budget)
class ISS:
@staticmethod
def calculate(budget):
return budget.value * 0.2
class ICMS:
@staticmethod
def calculate(budget):
return budget.value * 0.1
if __name__ == "__main__":
budget = Budget(5000)
calculator = TaxCalculator()
iss_tax = calculator.calculate(budget, ISS)
icms_tax = calculator.calculate(budget, ICMS)
print(f"Your budget is {budget.value}")
print(f"ISS tax: ${iss_tax}")
print(f"ICMS tax: ${icms_tax}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment