Sobes.tech
Junior — Middle

How would you implement an object-oriented architecture for a file sharing system?

sobes.tech AI

Answer from AI

To implement object-oriented architecture for a file sharing system, the main classes and their responsibilities can be outlined as follows:

  • File — a class representing a file with attributes such as name, size, type, creation date, etc.
  • User — a system user who can upload and download files.
  • Storage — an abstraction of storage, which can have implementations for local file system, cloud, etc.
  • FileManager — a class managing file operations: upload, download, delete.
  • TransferService — responsible for transferring files between users or systems.

Example in Python:

class File:
    def __init__(self, name, size, file_type):
        self.name = name
        self.size = size
        self.file_type = file_type

class User:
    def __init__(self, username):
        self.username = username

class Storage:
    def save(self, file: File):
        pass  # implementation of saving

    def retrieve(self, file_name: str) -> File:
        pass  # implementation of retrieving a file

class FileManager:
    def __init__(self, storage: Storage):
        self.storage = storage

    def upload(self, file: File):
        self.storage.save(file)

    def download(self, file_name: str) -> File:
        return self.storage.retrieve(file_name)

This approach allows for easy system extension, adding new storage types or file transfer methods.