Determine the different ways the system will be used. This includes main functions the system needs to perform and who will use it.
Primary Functions
Actors
Based on the requirements and use cases, identify the main objects of the system...
Determine how these objects will interact with each other to fulfill the use cases...
Design inheritance trees where applicable to promote code reuse and polymorphism. This step involves identifying common attributes and behaviors that can be abstracted into parent classes...
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
Attributes: For each class, define the attributes (data) it will hold...
Methods: Define the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
from abc import ABC, abstractmethod
from typing import Dict, List
---------- Slot ----------
class Slot:
def init(self, slot_id: str, cost: int):
self.slot_id = slot_id
self.cost = cost
---------- Payment Strategy Interface ----------
class PaymentType(ABC):
@abstractmethod
def process(self, slot: Slot, money: int):
pass
class CardPayment(PaymentType):
def process(self, slot: Slot, money: int):
pass # card-specific logic
class CashPayment(PaymentType):
def process(self, slot: Slot, money: int):
pass # cash-specific logic
class CoinPayment(PaymentType):
def process(self, slot: Slot, money: int):
pass # coin-specific logic
---------- Alert System ----------
class AlertSystem:
def send_alert(self, slot_id: str):
print(f"Alert: Restock needed for slot {slot_id}")
---------- Inventory Manager ----------
class InventoryManager:
def init(self, alert_system: AlertSystem):
self.inventory: Dict[str, int] = {}
self.alert_system = alert_system
def dispense_item(self, slot_id: str) -> bool:
pass
def update_count(self, slot_id: str, new_count: int):
pass
def restock_alert(self, slot_id: str):
self.alert_system.send_alert(slot_id)
---------- Payment Processor ----------
class PaymentProcessor:
def process_payment(self, payment_type: PaymentType, slot: Slot, money: int):
return payment_type.process(slot, money)
---------- Vending Machine ----------
class VendingMachine:
def init(self, slots: List[Slot], payment_processor: PaymentProcessor, inventory_manager: InventoryManager):
self.slots = {slot.slot_id: slot for slot in slots}
self.payment_processor = payment_processor
self.inventory_manager = inventory_manager
def process_payment(self, payment_type: PaymentType, slot: Slot, money: int):
return self.payment_processor.process_payment(payment_type, slot, money)
def dispense_item(self, slot_id: str):
pass
def update_count(self, slot_id: str, new_count: int):
self.inventory_manager.update_count(slot_id, new_count)
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
Critically examine your design for any flaws or areas for future improvement...