Skip to content

Instantly share code, notes, and snippets.

@esilvajr
Created April 26, 2025 18:38
Show Gist options
  • Save esilvajr/f2174e466cffae88f76b7cb7c30b65ce to your computer and use it in GitHub Desktop.
Save esilvajr/f2174e466cffae88f76b7cb7c30b65ce to your computer and use it in GitHub Desktop.
Exemplo de interface em Python.
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)
@esilvajr
Copy link
Author

Para executar com o Python instalado:

python3 interface_example.py

ou

python interface_example.py

A depender da versão do Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment