Created
May 10, 2021 11:43
-
-
Save renanzulian/cd9b386cc58a964425483c94b846b49e to your computer and use it in GitHub Desktop.
Strategy design patter - Python
This file contains hidden or 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
| 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