Bird
Raised Fist0
LLDsystem_design~25 mins

Interface Segregation Principle in LLD - System Design Exercise

Choose your learning style10 modes available
Design: Interface Segregation Principle Application
Focus on designing interfaces and their segregation in a modular system. Implementation details of business logic are out of scope.
Functional Requirements
FR1: Design interfaces that are client-specific and do not force clients to depend on methods they do not use.
FR2: Ensure that the system supports multiple types of clients with different needs without unnecessary dependencies.
FR3: Allow easy extension and maintenance by minimizing the impact of changes on unrelated clients.
Non-Functional Requirements
NFR1: The system should handle up to 1000 concurrent clients with different interface needs.
NFR2: API response latency should be under 100ms for interface method calls.
NFR3: Maintain high code maintainability and low coupling between components.
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
Key Components
Client-specific interfaces
Implementation classes that implement multiple small interfaces
Interface adapters or facades if needed
Dependency injection to bind clients to specific interfaces
Design Patterns
Interface Segregation Principle (ISP)
Adapter Pattern
Facade Pattern
Dependency Injection
Reference Architecture
  +----------------+       +----------------+       +----------------+
  | Client Type A  |       | Client Type B  |       | Client Type C  |
  +-------+--------+       +-------+--------+       +-------+--------+
          |                        |                        |
          |                        |                        |
  +-------v--------+       +-------v--------+       +-------v--------+
  | Interface A    |       | Interface B    |       | Interface C    |
  +-------+--------+       +-------+--------+       +-------+--------+
          |                        |                        |
          +-----------+------------+------------+-----------+
                      |                         |
              +-------v--------+        +-------v--------+
              | Implementation |        | Implementation |
              | Class 1        |        | Class 2        |
              +----------------+        +----------------+
Components
Client Type A Interface
Interface in chosen language
Defines only the methods needed by Client Type A
Client Type B Interface
Interface in chosen language
Defines only the methods needed by Client Type B
Client Type C Interface
Interface in chosen language
Defines only the methods needed by Client Type C
Implementation Classes
Classes implementing multiple small interfaces
Provide concrete behavior for segregated interfaces
Dependency Injection Container
DI framework or manual injection
Bind clients to the correct interface implementations
Request Flow
1. Client Type A requests service via Interface A.
2. Request is routed to Implementation Class that implements Interface A.
3. Implementation Class executes only methods defined in Interface A.
4. Client Type B uses Interface B similarly, without seeing Interface A methods.
5. This segregation prevents clients from depending on unused methods.
6. New client types can be added by defining new interfaces without changing existing ones.
Database Schema
Not applicable as this design focuses on interface segregation and modular code structure rather than data storage.
Scaling Discussion
Bottlenecks
Large monolithic interfaces causing clients to depend on unused methods.
Implementation classes becoming too large by implementing many interfaces.
Difficulty in maintaining and extending interfaces as client types grow.
Solutions
Split large interfaces into smaller, client-specific interfaces following ISP.
Use composition to delegate responsibilities to smaller classes.
Apply dependency injection to manage interface implementations cleanly.
Regularly review and refactor interfaces as new client requirements emerge.
Interview Tips
Time: Spend 10 minutes understanding client needs and interface requirements, 15 minutes designing segregated interfaces and implementation classes, 10 minutes discussing scaling and maintenance, and 10 minutes answering questions.
Explain why large interfaces cause problems for clients.
Describe how segregating interfaces improves modularity and maintainability.
Show how different clients get only the methods they need.
Discuss how this design supports future extension without breaking existing clients.
Mention patterns like Adapter and Dependency Injection that complement ISP.

Practice

(1/5)
1. What is the main idea behind the Interface Segregation Principle in system design?
easy
A. Interfaces should be as large as possible to cover all features.
B. All classes must implement every method of an interface.
C. Clients should only depend on methods they actually use.
D. Interfaces should be avoided to reduce complexity.

Solution

  1. Step 1: Understand the principle's goal

    The Interface Segregation Principle aims to keep interfaces small and focused so clients are not forced to depend on unused methods.
  2. Step 2: Match the correct description

    Clients should only depend on methods they actually use. correctly states that clients should only depend on methods they use, which aligns with the principle.
  3. Final Answer:

    Clients should only depend on methods they actually use. -> Option C
  4. Quick Check:

    Interface Segregation Principle = Clients depend only on used methods [OK]
Hint: Focus on clients using only needed methods [OK]
Common Mistakes:
  • Thinking interfaces must be large and cover all features
  • Believing all classes must implement every interface method
  • Assuming interfaces should be avoided
2. Which of the following interface definitions follows the Interface Segregation Principle?
easy
A. interface Printer { void print(); void scan(); void fax(); }
B. interface Machine { void doEverything(); }
C. interface Device { void operate(); void print(); void scan(); void fax(); }
D. interface Printer { void print(); } interface Scanner { void scan(); }

Solution

  1. Step 1: Analyze interface sizes

    interface Printer { void print(); } interface Scanner { void scan(); } splits responsibilities into separate interfaces for printing and scanning, keeping them focused and small.
  2. Step 2: Compare with other options

    Options A, B, and C combine multiple unrelated methods into one interface, violating the principle.
  3. Final Answer:

    Separate interfaces for each responsibility -> Option D
  4. Quick Check:

    Small focused interfaces = interface Printer { void print(); } interface Scanner { void scan(); } [OK]
Hint: Look for interfaces with single focused responsibility [OK]
Common Mistakes:
  • Choosing interfaces with many unrelated methods
  • Ignoring separation of concerns
  • Confusing interface segregation with inheritance
3. Given these interfaces and classes, what will be the output?
interface Printer { void print(); }
interface Scanner { void scan(); }
class MultiFunctionDevice implements Printer, Scanner {
  public void print() { System.out.println("Printing document"); }
  public void scan() { System.out.println("Scanning document"); }
}

MultiFunctionDevice device = new MultiFunctionDevice();
device.print();
device.scan();
medium
A. Printing document\nScanning document
B. Printing document
C. Scanning document
D. Compilation error

Solution

  1. Step 1: Understand class implementation

    The class implements both Printer and Scanner interfaces and provides both methods.
  2. Step 2: Trace method calls

    Calling device.print() prints "Printing document" and device.scan() prints "Scanning document".
  3. Final Answer:

    Printing document\nScanning document -> Option A
  4. Quick Check:

    Both methods print messages = Printing document\nScanning document [OK]
Hint: Check which interfaces are implemented and methods called [OK]
Common Mistakes:
  • Assuming only one method runs
  • Expecting compilation error due to multiple interfaces
  • Confusing method outputs
4. Identify the problem in this interface design violating the Interface Segregation Principle:
interface Worker {
  void work();
  void eat();
}

class Robot implements Worker {
  public void work() { System.out.println("Robot working"); }
  public void eat() { throw new UnsupportedOperationException(); }
}
medium
A. Worker interface is too broad, forcing Robot to implement unused methods.
B. Robot should implement eat() properly.
C. Robot class should not implement Worker interface.
D. No problem, this design is correct.

Solution

  1. Step 1: Analyze interface methods

    The Worker interface combines work and eat methods, but Robot cannot eat.
  2. Step 2: Identify violation

    Robot is forced to implement eat() which it does not support, violating Interface Segregation Principle.
  3. Final Answer:

    Worker interface is too broad, forcing Robot to implement unused methods. -> Option A
  4. Quick Check:

    Broad interface forcing unused methods = Worker interface is too broad, forcing Robot to implement unused methods. [OK]
Hint: Check if all interface methods apply to all implementers [OK]
Common Mistakes:
  • Thinking Robot must implement eat() properly
  • Believing no problem exists
  • Suggesting Robot should avoid interface
5. You are designing a payment system with interfaces for different payment methods. Which design best follows the Interface Segregation Principle?
hard
A. interface Payment { void payWithCard(); void payWithCash(); void payWithCrypto(); }
B. interface CardPayment { void payWithCard(); } interface CashPayment { void payWithCash(); } interface CryptoPayment { void payWithCrypto(); }
C. interface Payment { void pay(); } class AllPayments implements Payment { void pay() { /* all methods */ } }
D. interface Payment { void payWithCard(); void payWithCash(); } class CryptoPayment implements Payment { void payWithCard() {} void payWithCash() {} }

Solution

  1. Step 1: Evaluate interface segregation

    interface CardPayment { void payWithCard(); } interface CashPayment { void payWithCash(); } interface CryptoPayment { void payWithCrypto(); } splits payment methods into separate interfaces, allowing clients to implement only what they need.
  2. Step 2: Compare other options

    Options A, B, and D force clients to implement methods they may not use, violating the principle.
  3. Final Answer:

    Separate interfaces for each payment method -> Option B
  4. Quick Check:

    Separate interfaces = interface CardPayment { void payWithCard(); } interface CashPayment { void payWithCash(); } interface CryptoPayment { void payWithCrypto(); } [OK]
Hint: Split interfaces by payment method for flexibility [OK]
Common Mistakes:
  • Combining all payment methods in one interface
  • Forcing classes to implement unused methods
  • Ignoring interface segregation benefits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard