| Low-Level Design (LLD) of a Polling System 🗳️
|
| A Polling System allows users to create, vote on, and view polls. This design covers essential components, class structure, and workflow.
|
|
|
| Step 1: Identify the Core Functionalities
|
| 🔹 User Management → Users can create polls & vote.
|
| 🔹 Poll Management → Create, edit, and delete polls.
|
| 🔹 Voting Mechanism → Users can vote once per poll.
|
| 🔹 Result Computation → Count votes and display results.
|
|
|
| Step 2: Identify the Key Entities & Relationships
|
| User → Represents the users participating in polls.
|
| Poll → Stores details about a specific poll.
|
| Option → Represents the choices in a poll.
|
| Vote → Tracks the votes cast by users.
|
| Step 3: Define the Class Structure
|
| 1. User Class
|
| python
|
| Copy
|
| Edit
|
| class User:
|
| def __init__(self, user_id: int, name: str):
|
| self.user_id = user_id
|
| self.name = name
|
|
|
| def get_details(self):
|
| return f"User: {self.name} (ID: {self.user_id})"
|
| ✔ Stores basic user information.
|
|
|
| 2. Poll Class
|
| python
|
| Copy
|
| Edit
|
| class Poll:
|
| def __init__(self, poll_id: int, creator: User, question: str, options: list):
|
| self.poll_id = poll_id
|
| self.creator = creator
|
| self.question = question
|
| self.options = {option: 0 for option in options} # Stores votes for each option
|
| self.voters = set() # Track users who voted
|
| self.is_active = True # Poll status
|
|
|
| def vote(self, user: User, option: str):
|
| if not self.is_active:
|
| return "Poll is closed."
|
| if user.user_id in self.voters:
|
| return "User has already voted."
|
| if option not in self.options:
|
| return "Invalid option."
|
|
|
| self.options[option] += 1
|
| self.voters.add(user.user_id)
|
| return f"{user.name} voted for '{option}'"
|
|
|
| def close_poll(self):
|
| self.is_active = False
|
| return "Poll closed."
|
|
|
| def get_results(self):
|
| return self.options if not self.is_active else "Poll is still active."
|
| ✔ Handles poll creation, voting, and results computation.
|
|
|
| 3. PollManager Class
|
| python
|
| Copy
|
| Edit
|
| class PollManager:
|
| def __init__(self):
|
| self.polls = {} # {poll_id: Poll object}
|
| self.poll_counter = 1
|
|
|
| def create_poll(self, creator: User, question: str, options: list):
|
| if len(options) < 2:
|
| return "A poll must have at least two options."
|
|
|
| poll = Poll(self.poll_counter, creator, question, options)
|
| self.polls[self.poll_counter] = poll
|
| self.poll_counter += 1
|
|
|
| return f"Poll '{question}' created with ID {poll.poll_id}"
|
|
|
| def get_poll(self, poll_id: int):
|
| return self.polls.get(poll_id, "Poll not found.")
|
|
|
| def close_poll(self, poll_id: int):
|
| if poll_id not in self.polls:
|
| return "Poll not found."
|
|
|
| return self.polls[poll_id].close_poll()
|
| ✔ Manages poll creation, retrieval, and closing polls.
|
|
|
| Step 4: Testing the System
|
| python
|
| Copy
|
| Edit
|
| # Initialize Poll Manager
|
| poll_mgr = PollManager()
|
|
|
| # Create Users
|
| user1 = User(1, "Alice")
|
| user2 = User(2, "Bob")
|
| user3 = User(3, "Charlie")
|
|
|
| # Create a Poll
|
| print(poll_mgr.create_poll(user1, "Best Programming Language?", ["Python", "Java", "C++"]))
|
|
|
| # Fetch Poll
|
| poll = poll_mgr.get_poll(1)
|
| if isinstance(poll, Poll):
|
| print(poll.vote(user1, "Python")) # Alice votes for Python
|
| print(poll.vote(user2, "Java")) # Bob votes for Java
|
| print(poll.vote(user3, "Python")) # Charlie votes for Python
|
|
|
| print(poll.get_results()) # Display poll results
|
|
|
| print(poll_mgr.close_poll(1)) # Close poll
|
|
|
| print(poll.get_results()) # Display final results
|
| Step 5: Explanation
|
| User Class → Represents users who create & vote on polls.
|
| Poll Class → Manages poll data, voting logic, and results.
|
| PollManager → Handles poll creation, retrieval, and management.
|
| Step 6: Enhancements & Scalability
|
| 🔹 Multiple-choice Voting → Allow users to select multiple options.
|
| 🔹 Poll Expiry → Auto-close polls after a set duration.
|
| 🔹 Anonymous Polls → Hide voter identities.
|
| 🔹 Real-time Updates → Use WebSockets for live results.
|
| 🔹 Database Integration → Store polls & votes persistently.
|
|
|
| 🚀 Conclusion
|
| This LLD provides a modular, scalable, and extendable Polling System. Users can create, vote, and view poll results seamlessly! 🗳️
|