# Good Example - Following the Law of Demeter
class Wallet:
def __init__(self):
self.balance = 0
def add_funds(self, amount):
if amount > 0:
self.balance += amount
class Person:
def __init__(self):
self.wallet = Wallet()
def deposit(self, amount):
if amount > 0:
self.wallet.add_funds(amount)
# Bad Example - Violating the Law of Demeter
class BadPerson:
def __init__(self):
self.wallet = Wallet()
def deposit(self, amount):
if amount > 0:
# Violating LoD: Accessing wallet's balance directly
self.wallet.balance += amount
# Usage of the classes
person = Person()
person.deposit(100) # Good Example
print(f"Good Example - Balance: {person.wallet.balance}")
bad_person = BadPerson()
bad_person.deposit(100) # Bad Example (LoD Violation)
print(f"Bad Example - Balance: {bad_person.wallet.balance}")