from abc import ABC, abstractmethod
class ShippingStrategy(ABC):
@abstractmethod
def calculate(self, order):
pass
class StandardShipping(ShippingStrategy):
def calculate(self, order):
return 5.0 # фиксированная стоимость для стандартной доставки
class ExpressShipping(ShippingStrategy):
def calculate(self, order):
return 15.0 # фиксированная стоимость для экспресс-доставки
class DiscountedShipping(ShippingStrategy):
def calculate(self, order):
return 2.0 # скидка на доставку
class Order:
def __init__(self, items):
self.items = items # список товаров
self.shipping_strategy = None # стратегия по умолчанию
def set_shipping_strategy(self, strategy: ShippingStrategy):
self.shipping_strategy = strategy
def calculate_shipping_cost(self):
if not self.shipping_strategy:
raise ValueError("Shipping strategy is not set")
return self.shipping_strategy.calculate(self)
# Создание заказа
order = Order(items=["book", "laptop", "pen"])
# Установка стратегии доставки и расчет стоимости
order.set_shipping_strategy(StandardShipping())
print("Standard Shipping Cost:", order.calculate_shipping_cost()) # Standard Shipping Cost: 5.0
order.set_shipping_strategy(ExpressShipping())
print("Express Shipping Cost:", order.calculate_shipping_cost()) # Express Shipping Cost: 15.0
order.set_shipping_strategy(DiscountedShipping())
print("Discounted Shipping Cost:", order.calculate_shipping_cost()) # Discounted Shipping Cost: 2.0