Created
April 26, 2025 18:38
-
-
Save esilvajr/f2174e466cffae88f76b7cb7c30b65ce to your computer and use it in GitHub Desktop.
Exemplo de interface em 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
from abc import ABC, abstractmethod | |
class InterfaceOrder(ABC): | |
@abstractmethod | |
def apply_discount(self, value) -> float: | |
raise NotImplementedError | |
class NormalOrder(InterfaceOrder): | |
def apply_discount(self, value: float) -> float: | |
return value - (value * 0.10) | |
class BlackFridayOrder(InterfaceOrder): | |
def apply_discount(self, value: float) -> float: | |
return value - (value * 0.30) | |
class ChristmasOrder(InterfaceOrder): | |
def apply_discount(self, value: float) -> float: | |
return value - (value * 0.50) | |
class OrderService(): | |
def order(self, order: InterfaceOrder, value: float)-> None: | |
print(order.apply_discount(value)) | |
if __name__ == '__main__': | |
product_value: float = 100.00 | |
order_service: OrderService = OrderService() | |
order_service.order(NormalOrder(), product_value) | |
order_service.order(BlackFridayOrder(), product_value) | |
order_service.order(ChristmasOrder(), product_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Para executar com o Python instalado:
python3 interface_example.py
ou
python interface_example.py
A depender da versão do Python.