Before applying DDD, business logic and data handling are mixed, making the code harder to maintain. After applying DDD, the domain concepts like Money and OrderItem encapsulate behavior, making the model clearer and easier to extend.
### Before DDD: Business logic mixed with data access
class Order:
def __init__(self, items):
self.items = items
def total_price(self):
total = 0
for item in self.items:
total += item['price'] * item['quantity']
return total
# Data access and business logic are mixed
### After DDD: Clear domain model with business logic encapsulated
class Money:
def __init__(self, amount):
self.amount = amount
def add(self, other):
return Money(self.amount + other.amount)
class OrderItem:
def __init__(self, product, quantity, price):
self.product = product
self.quantity = quantity
self.price = price
def total_price(self):
return Money(self.price * self.quantity)
class Order:
def __init__(self, items):
self.items = items
def total_price(self):
total = Money(0)
for item in self.items:
total = total.add(item.total_price())
return total