import random coupling_examples = [ ( "Tight Coupling", "In this example, the UserService class is tightly coupled to the Database class.", """ class Database: def get_user(self, user_id): # database logic here pass class UserService: def __init__(self): self.db = Database() def get_user_info(self, user_id): return self.db.get_user(user_id) """ ), ( "Loose Coupling", "This example shows loose coupling through dependency injection.", """ class Database: def get_user(self, user_id): # database logic here pass class UserService: def __init__(self, database): self.db = database def get_user_info(self, user_id): return self.db.get_user(user_id) """ ), ( "Decoupling with Interfaces", "This example uses an interface to further decouple the classes.", """ from abc import ABC, abstractmethod class DatabaseInterface(ABC): @abstractmethod def get_user(self, user_id): pass class Database(DatabaseInterface): def get_user(self, user_id): # database logic here pass class UserService: def __init__(self, database: DatabaseInterface): self.db = database def get_user_info(self, user_id): return self.db.get_user(user_id) """ ) ] cohesion_examples = [ ( "Functional Cohesion", "All elements contribute to a single, well-defined task.", """ class UserAuthentication: def __init__(self, username, password): self.username = username self.password = password def hash_password(self): # Hash the password pass def check_password_strength(self): # Check if the password meets security requirements pass def authenticate(self): # Authenticate the user pass """ ), ( "Sequential Cohesion", "Elements are grouped because the output from one element serves as input to another element.", """ class OrderProcessor: def process_order(self, order): validated_order = self.validate_order(order) calculated_order = self.calculate_total(validated_order) return self.submit_order(calculated_order) def validate_order(self, order): # Validate the order pass def calculate_total(self, order): # Calculate the total price pass def submit_order(self, order): # Submit the order to the system pass """ ), ( "Communicational Cohesion", "Elements operate on the same data.", """ class UserManager: def __init__(self, user_data): self.user_data = user_data def validate_user(self): # Validate user data pass def save_user(self): # Save user to database pass def send_welcome_email(self): # Send welcome email to user pass """ ) ] monolithic_example = """ class EcommerceApp: def __init__(self): self.users = {} self.products = {} self.orders = {} self.payments = {} self.user_preferences = {} def register_user(self, username, password): # User registration logic self.users[username] = {"password": password} self.user_preferences[username] = [] def add_product(self, product_id, name, price, category): # Product addition logic self.products[product_id] = {"name": name, "price": price, "category": category} def create_order(self, user, product_id, quantity): # Order creation logic if user in self.users and product_id in self.products: order_id = len(self.orders) + 1 self.orders[order_id] = {"user": user, "product": product_id, "quantity": quantity} self.user_preferences[user].append(self.products[product_id]["category"]) return order_id return None def process_payment(self, order_id, payment_method): # Payment processing logic if order_id in self.orders: self.payments[order_id] = {"method": payment_method, "status": "completed"} return True return False def recommend_products(self, user, num_recommendations=3): # Simple product recommendation system if user not in self.user_preferences or not self.user_preferences[user]: return random.sample(list(self.products.values()), min(num_recommendations, len(self.products))) user_categories = self.user_preferences[user] most_common_category = max(set(user_categories), key=user_categories.count) recommended_products = [ product for product in self.products.values() if product["category"] == most_common_category ] if len(recommended_products) < num_recommendations: other_products = [ product for product in self.products.values() if product["category"] != most_common_category ] recommended_products.extend(random.sample(other_products, num_recommendations - len(recommended_products))) return recommended_products[:num_recommendations] # Usage app = EcommerceApp() app.register_user("alice", "password123") app.add_product(1, "Laptop", 999.99, "Electronics") app.add_product(2, "Smartphone", 499.99, "Electronics") app.add_product(3, "Headphones", 99.99, "Electronics") app.add_product(4, "T-shirt", 19.99, "Clothing") order_id = app.create_order("alice", 1, 1) app.process_payment(order_id, "credit_card") # Get product recommendations for Alice recommendations = app.recommend_products("alice", 2) print("Recommended products for Alice:") for product in recommendations: print(f"- {product['name']} (${product['price']})") """