mirror of
https://github.com/donnemartin/system-design-primer.git
synced 2025-12-14 17:08:56 +03:00
git'n a system-desgin-primer
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
Video Conferencing System:
|
||||
|
||||
1. User: Represents a user in the system.
|
||||
|
||||
2. Meeting: Handles participant management and audio/video streaming.
|
||||
60
B4/Solutions/system_design/Video_conferencing/video.py
Normal file
60
B4/Solutions/system_design/Video_conferencing/video.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import random
|
||||
import threading
|
||||
import time
|
||||
|
||||
class User:
|
||||
def __init__(self, user_id, name):
|
||||
self.user_id = user_id
|
||||
self.name = name
|
||||
|
||||
class Meeting:
|
||||
def __init__(self, meeting_id):
|
||||
self.meeting_id = meeting_id
|
||||
self.participants = []
|
||||
self.is_active = True
|
||||
|
||||
def add_participant(self, user):
|
||||
self.participants.append(user)
|
||||
print(f"{user.name} joined the meeting {self.meeting_id}.")
|
||||
|
||||
def remove_participant(self, user):
|
||||
self.participants.remove(user)
|
||||
print(f"{user.name} left the meeting {self.meeting_id}.")
|
||||
|
||||
def stream_audio_video(self):
|
||||
# Simulated audio/video streaming
|
||||
while self.is_active:
|
||||
print(f"Streaming audio/video in meeting {self.meeting_id}...")
|
||||
time.sleep(2)
|
||||
|
||||
def end_meeting(self):
|
||||
self.is_active = False
|
||||
print(f"Meeting {self.meeting_id} has ended.")
|
||||
|
||||
class VideoConferencingSystem:
|
||||
def __init__(self):
|
||||
self.meetings = {}
|
||||
|
||||
def create_meeting(self):
|
||||
meeting_id = random.randint(1000, 9999)
|
||||
meeting = Meeting(meeting_id)
|
||||
self.meetings[meeting_id] = meeting
|
||||
print(f"Meeting {meeting_id} created.")
|
||||
return meeting
|
||||
|
||||
# Example Usage
|
||||
vcs = VideoConferencingSystem()
|
||||
meeting = vcs.create_meeting()
|
||||
|
||||
user1 = User(1, "Alice")
|
||||
user2 = User(2, "Bob")
|
||||
|
||||
meeting.add_participant(user1)
|
||||
meeting.add_participant(user2)
|
||||
|
||||
stream_thread = threading.Thread(target=meeting.stream_audio_video)
|
||||
stream_thread.start()
|
||||
|
||||
time.sleep(5)
|
||||
meeting.end_meeting()
|
||||
stream_thread.join()
|
||||
@@ -0,0 +1,7 @@
|
||||
Payment Processing System:
|
||||
|
||||
1. User: Represents a user with a balance.
|
||||
|
||||
2. Transaction: Represents a payment transaction between users.
|
||||
|
||||
3. PaymentProcessingSystem: Processes transactions and handles fund transfers.
|
||||
34
B4/Solutions/system_design/payment_processing/payment.py
Normal file
34
B4/Solutions/system_design/payment_processing/payment.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import random
|
||||
|
||||
class User:
|
||||
def __init__(self, user_id, name, balance):
|
||||
self.user_id = user_id
|
||||
self.name = name
|
||||
self.balance = balance
|
||||
|
||||
class Transaction:
|
||||
def __init__(self, sender, receiver, amount):
|
||||
self.transaction_id = random.randint(1000, 9999)
|
||||
self.sender = sender
|
||||
self.receiver = receiver
|
||||
self.amount = amount
|
||||
|
||||
class PaymentProcessingSystem:
|
||||
def process_payment(self, transaction):
|
||||
if transaction.sender.balance >= transaction.amount:
|
||||
transaction.sender.balance -= transaction.amount
|
||||
transaction.receiver.balance += transaction.amount
|
||||
print(f"Transaction {transaction.transaction_id} successful: {transaction.sender.name} paid {transaction.receiver.name} ${transaction.amount}.")
|
||||
else:
|
||||
print(f"Transaction {transaction.transaction_id} failed: Insufficient funds.")
|
||||
|
||||
# Example Usage
|
||||
user1 = User(1, "Alice", 100)
|
||||
user2 = User(2, "Bob", 50)
|
||||
|
||||
payment_system = PaymentProcessingSystem()
|
||||
transaction = Transaction(user1, user2, 30)
|
||||
payment_system.process_payment(transaction)
|
||||
|
||||
print(f"{user1.name}'s balance: ${user1.balance}")
|
||||
print(f"{user2.name}'s balance: ${user2.balance}")
|
||||
@@ -0,0 +1,36 @@
|
||||
import threading
|
||||
|
||||
class Document:
|
||||
def __init__(self, title):
|
||||
self.title = title
|
||||
self.content = ""
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def edit(self, new_content):
|
||||
with self.lock:
|
||||
self.content = new_content
|
||||
print(f"Document '{self.title}' updated: {self.content}")
|
||||
|
||||
class User:
|
||||
def __init__(self, user_id, name):
|
||||
self.user_id = user_id
|
||||
self.name = name
|
||||
|
||||
def edit_document(self, document, new_content):
|
||||
print(f"{self.name} is editing the document '{document.title}'...")
|
||||
document.edit(new_content)
|
||||
|
||||
# Example Usage
|
||||
document = Document("Project Plan")
|
||||
user1 = User(1, "Alice")
|
||||
user2 = User(2, "Bob")
|
||||
|
||||
# Simulating concurrent edits
|
||||
thread1 = threading.Thread(target=user1.edit_document, args=(document, "Draft by Alice."))
|
||||
thread2 = threading.Thread(target=user2.edit_document, args=(document, "Draft by Bob."))
|
||||
|
||||
thread1.start()
|
||||
thread2.start()
|
||||
|
||||
thread1.join()
|
||||
thread2.join()
|
||||
@@ -0,0 +1,7 @@
|
||||
1. Document: Represents a document being edited, with content locked for
|
||||
safe concurrent editing.
|
||||
|
||||
2. User: Represents a user editing the document.
|
||||
|
||||
3. Each user can edit the document concurrently, with proper locking to prevent
|
||||
data corruption.
|
||||
5
B4/Solutions/system_design/smart_home/features.p[y
Normal file
5
B4/Solutions/system_design/smart_home/features.p[y
Normal file
@@ -0,0 +1,5 @@
|
||||
Smart Home System:
|
||||
|
||||
1. SmartDevice: Represents a smart device with on/off control.
|
||||
|
||||
2. SmartHomeSystem: Manages devices and controls their states.
|
||||
44
B4/Solutions/system_design/smart_home/smart_home.py
Normal file
44
B4/Solutions/system_design/smart_home/smart_home.py
Normal file
@@ -0,0 +1,44 @@
|
||||
class SmartDevice:
|
||||
def __init__(self, device_id, device_type):
|
||||
self.device_id = device_id
|
||||
self.device_type = device_type
|
||||
self.is_on = False
|
||||
|
||||
def turn_on(self):
|
||||
self.is_on = True
|
||||
print(f"{self.device_type} {self.device_id} turned on.")
|
||||
|
||||
def turn_off(self):
|
||||
self.is_on = False
|
||||
print(f"{self.device_type} {self.device_id} turned off.")
|
||||
|
||||
class SmartHomeSystem:
|
||||
def __init__(self):
|
||||
self.devices = {}
|
||||
|
||||
def add_device(self, device):
|
||||
self.devices[device.device_id] = device
|
||||
print(f"Added {device.device_type} {device.device_id}.")
|
||||
|
||||
def control_device(self, device_id, action):
|
||||
device = self.devices.get(device_id)
|
||||
if device:
|
||||
if action == "on":
|
||||
device.turn_on()
|
||||
elif action == "off":
|
||||
device.turn_off()
|
||||
else:
|
||||
print("Invalid action.")
|
||||
else:
|
||||
print("Device not found.")
|
||||
|
||||
# Example Usage
|
||||
smart_home = SmartHomeSystem()
|
||||
light = SmartDevice(1, "Light")
|
||||
thermostat = SmartDevice(2, "Thermostat")
|
||||
|
||||
smart_home.add_device(light)
|
||||
smart_home.add_device(thermostat)
|
||||
|
||||
smart_home.control_device(1, "on") # Turn on the light
|
||||
smart_home.control_device(2, "off") # Turn off the thermostat
|
||||
Reference in New Issue
Block a user