Designing a module or feature with classes

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-07-22

Problem Statement


🎯 Objective


Design a modular, object-oriented system for a given feature, where functionality is decomposed into logical classes with clear responsibilities, relationships, and interactions.


The goal is to demonstrate understanding of OOP principles (Encapsulation, Abstraction, Inheritance, Polymorphism) and good class design practices such as separation of concerns,

extensibility, and maintainability.


πŸ”Ή Context


In any large software project, features are implemented as modules β€” self-contained components that perform a specific function.


Your task is to design a feature/module (for example: authentication, payments, cart management, logging, or notifications) using appropriate classes and interfaces that ensure reusability, testability, and clarity.


βœ… Functional Requirements (Generic Template)


1.The system should define core entities (classes) and their relationships.
2.Each class should have a clear responsibility β€” handling data or logic for one concept.
3.The system should encapsulate internal state and expose clean public interfaces (APIs).
4.The design should support future extensions (e.g., new payment methods, new data sources).
5.The module should be loosely coupled but highly cohesive.


βš™οΈ Non-Functional Requirements


RequirementDescription


Extensibility

Should allow easy feature addition without modifying existing classes.


Reusability

Designed components can be reused across different modules.


Maintainability

Code should be modular and easy to refactor.


Testability

Classes should be unit-testable with clear interfaces.


Performance

Should minimize unnecessary dependencies or computations.


🧩 Example: Design a Logging Module


To illustrate how to β€œdesign a module or feature with classes,” let’s take a Logging System as an example.


🧠 Problem Context


You need to design a Logging Module that allows different parts of an application to log messages (INFO, ERROR, DEBUG, etc.) to various destinations β€” e.g., console, file, or remote server.


βœ… Functional Requirements


1.Allow logging messages with levels (INFO, WARN, ERROR, DEBUG).
2.Support multiple output channels (console, file, cloud).
3.Allow configuration of logging format and level.
4.Should be easily extendable for new log destinations.


🧱 Core Classes


Class and Responsibility


Logger

Central class to log messages; delegates to handlers.


LogHandler (interface)

Abstract handler for sending logs to a destination.


ConsoleHandler

Writes logs to console.


FileHandler

Writes logs to a file.


RemoteHandler

Sends logs to a remote API endpoint.


LogFormatter

Formats log messages (e.g., timestamps, thread info).


LogLevel (enum)

Defines levels (INFO, WARN, ERROR, DEBUG).


🧾 Example Class Diagram (Text Representation)

1 2β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 3β”‚ Logger β”‚ 4│──────────────────────────│ 5β”‚ - handlers: List<LogHandler> β”‚ 6│──────────────────────────│ 7β”‚ + log(level, message) β”‚ 8β”‚ + addHandler(handler) β”‚ 9β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 10 β”‚ 11 β–Ό 12β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 13β”‚ LogHandler (IF) β”‚ 14│──────────────────────────│ 15β”‚ + handle(level, message) β”‚ 16β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 17 β”Œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 18 β–Ό β–Ό β–Ό 19ConsoleHandler FileHandler RemoteHandler 20 21β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 22β”‚ LogFormatter β”‚ 23│──────────────────────────│ 24β”‚ + format(level, message) β”‚ 25β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


πŸ’‘ Example Flow


1.App creates a Logger instance.
2.Attaches ConsoleHandler and FileHandler.
3.Calls logger.log(INFO, "Server started").
4.Logger delegates to both handlers β†’ message is printed and stored in log file.


🧠 Design Patterns Used


Pattern and Use


Strategy Pattern

Different log handlers implement same interface.


Factory Pattern

For creating handlers based on configuration.


Singleton

Single global Logger instance (optional).


Decorator

Add extra functionality (e.g., timestamp, context).


πŸ’¬ Example Interview Prompt Summary

Design a module or feature using classes.
For example, design a Logging module that can log messages at different levels to multiple destinations.
Show your class structure, relationships, and how you’d ensure extensibility and clean separation of concerns.


πŸš€ Other Example Features You Could Design


Feature and Description


Authentication Module

Classes for User, AuthService, TokenManager, PasswordEncoder.


Payment Gateway

Classes for Payment, PaymentProcessor, CreditCard, PayPal, UPI.


Email Notification System

Classes for Email, SMTPClient, TemplateEngine, NotificationService.


Cache System

Classes for Cache, LRUCache, CacheManager.


Shopping Cart Module

Classes for Cart, CartItem, Product, CartService.


🧩 Template for Any β€œFeature with Classes” Problem


Design a [Feature/Module Name].


β€’Define the key entities (classes) involved.
β€’Specify their responsibilities and relationships.
β€’Show how classes interact to fulfill the feature’s functionality.
β€’Ensure extensibility, encapsulation, and loose coupling.
β€’Optionally, describe patterns or interfaces that make the design flexible.


Solution

Comments