Contenido del Curso
Software Architecture Fundamentals
Software Architecture Fundamentals
Data Structures Design
In software design, data structures are methods of organizing, storing, and managing data so that it can be used efficiently.
Data structures are essential for storing and accessing data in an organized and efficient manner. They are the building blocks that allow a program to handle data effectively, providing specific ways to organize, process, and retrieve information according to the application's needs.
Some of the most widely used data structures include:
- Arrays: store items in a contiguous memory space, allowing easy indexing and retrieval of elements by position;
- Linked lists: connect each item to the next in a sequence, which allows for efficient insertions or deletions;
- Stacks and queues: enable ordering of elements, often with restrictions (e.g., "last-in, first-out" for stacks);
- Hash tables (or dictionaries): use a key-value pair format to allow fast lookups and are especially useful when unique identifiers are needed;
- Trees and graphs: provide hierarchical or interconnected data structures, perfect for representing relationships or dependencies.
These structures are foundational tools that allow developers to optimize how data flows and is manipulated in a program.
Example
To enhance the understanding of data structures in the context of an e-commerce platform, let's explore each key area with illustrative tables, code samples, and examples.
Product Catalog
The Product Catalog functions as the main inventory system. It stores all the products available on the platform, with details like product ID, name, category, price, and stock quantity.
A hash map allows quick retrieval of product information using a unique key, such as a product ID. This structure supports fast lookups, making it ideal for retrieving specific product details without searching through a large list.
# Product Catalog implemented as a hash map (dictionary) product_catalog = { "P123": {"name": "Wireless Headphones", "category": "Electronics", "price": 99.99, "stock_quantity": 50}, "P124": {"name": "Running Shoes", "category": "Sportswear", "price": 49.99, "stock_quantity": 100} } # Fast retrieval by product ID product = product_catalog.get("P123") # Retrieves details for Wireless Headphones print(product)
Shopping Cart
The Shopping Cart stores items that a customer intends to buy temporarily. Each item typically includes details like product ID, quantity, and item price.
A linked list is ideal for dynamic lists like shopping carts because it allows items to be added or removed easily without reorganizing the entire list. The cart can expand or shrink as users add or remove items, and linked lists handle these changes more efficiently than arrays for this purpose.
# Shopping cart as a linked list (using a list of dictionaries in Python for simplicity) class CartItem: def __init__(self, product_id, quantity, price): self.product_id = product_id self.quantity = quantity self.price = price self.next_item = None # Reference to the next item in the cart # Initializing cart items cart_head = CartItem("P123", 1, 99.99) second_item = CartItem("P124", 2, 49.99) cart_head.next_item = second_item # Linking the items # Traversing the linked list to view cart items current_item = cart_head while current_item: print(f"Product ID: {current_item.product_id}, Quantity: {current_item.quantity}") current_item = current_item.next_item
Order History
The Order History tracks all previous orders placed by a customer, allowing them to view past purchases and check order statuses.
An array is suitable for storing order history, as it allows easy sequential access to past orders. Arrays are efficient for read-heavy operations, making it ideal for order history, which is frequently read but rarely modified.
# Order History as a list (dynamic array) order_history = [ {"order_id": "O001", "date": "2024-09-01", "total": 149.98, "status": "Shipped"}, {"order_id": "O002", "date": "2024-09-10", "total": 9.99, "status": "Delivered"} ] # Accessing orders by index print(order_history[0]) # Outputs the details of the first order
Customer Profiles
Customer Profiles store essential customer information like name, email, address, and payment methods. Each customer should have a unique identifier that links to their profile details.
A binary search tree efficiently searches and manages sorted data. In customer management, a BST allows fast searches, inserts, and deletions by customer ID while maintaining sorted order.
# Basic structure for customer profile management using a Binary Search Tree class CustomerNode: def __init__(self, customer_id, name, email): self.customer_id = customer_id self.name = name self.email = email self.left = None # Left child self.right = None # Right child # Adding customer profiles (simplified binary search tree logic) root_customer = CustomerNode("C001", "Alice Johnson", "alice@example.com") root_customer.left = CustomerNode("C000", "Bob Smith", "bob@example.com") root_customer.right = CustomerNode("C002", "Charlie Brown", "charlie@example.com") # Function to find a customer in the tree def find_customer(root, customer_id): if root is None or root.customer_id == customer_id: return root elif customer_id < root.customer_id: return find_customer(root.left, customer_id) else: return find_customer(root.right, customer_id) # Retrieve customer with ID "C001" customer = find_customer(root_customer, "C001") print(customer.name if customer else "Customer not found")
Summary
In an e-commerce platform, selecting appropriate data structures ensures efficient data management and seamless user experiences:
- Product Catalog uses a Hash Map for fast lookups;
- Shopping Cart relies on a Linked List for dynamic item management;
- Order History leverages an Array for efficient sequential access;
- Customer Profiles utilize a Binary Search Tree for organized customer data storage and quick retrieval.
Choosing suitable data structures based on the specific requirements of each component optimizes performance, scalability, and user satisfaction.
¡Gracias por tus comentarios!