Techniques to Identify ISP Violations
The Interface Segregation Principle (ISP) focuses on ensuring that classes only implement interfaces they actually need. When a class is forced to implement methods it doesn’t use, it violates ISP. Let’s look at different techniques to identify ISP violations with examples.
1. Low Cohesion
Low cohesion happens when a class tries to handle multiple unrelated tasks, making the class less focused and harder to maintain. Low cohesion often leads to ISP violations because the class implements more methods than necessary.
Example
Here, MediaDevice tries to handle both playing and recording, which can create low cohesion for a device that only plays media but doesn’t record.
public interface MediaDevice {
void playAudio();
void playVideo();
void recordVideo();
}
2. Fat Interface
A fat interface is one that contains too many methods, usually covering multiple unrelated functionalities. Fat interfaces force classes to depend on methods they don’t need, leading to bloated code and ISP violations.
Example: Payment Interface
This interface is too large and covers different types of payment methods. It forces all classes implementing it to handle multiple payment options, even if they don’t need them.
public interface PaymentProcessor {
void processCreditCard();
void processPayPal();
void processBankTransfer();
}
3. Empty or Unsupported Methods
When classes implement an interface and include empty or unsupported methods, it’s a clear sign of an ISP violation. Classes should not be forced to implement methods they won’t use.
Example: Vehicle Interface
Here, the Car class has to implement fly() and sail(), even though these methods don’t make sense for a car. These methods either remain empty or throw exceptions, violating ISP.
public class Car implements Vehicle {
@Override
public void drive() {
System.out.println("Car is driving...");
}
@Override
public void fly() {
// Cars can't fly
throw new UnsupportedOperationException("Car can't fly");
}
@Override
public void sail() {
// Cars can't sail
throw new UnsupportedOperationException("Car can't sail");
}
}
In this case, a Vehicle interface tries to cover driving, flying, and sailing, but not all vehicles can perform all these actions.
4. Classes with Too Many Responsibilities
When a class has too many responsibilities, it often implements multiple methods from a large interface. This indicates that the interface may need to be broken into smaller ones.
Example: Employee Interface
Here, CustomerSupport should only handle customer queries, but the large interface forces it to implement salary and team management methods, violating ISP.
public class CustomerSupport implements Employee {
@Override
public void calculateSalary() {
throw new UnsupportedOperationException("Customer support doesn't calculate salaries");
}
@Override
public void handleCustomerQueries() {
System.out.println("Handling customer queries...");
}
@Override
public void manageTeam() {
throw new UnsupportedOperationException("Customer support doesn't manage teams");
}
}
The Interface Segregation Principle ensures that classes remain focused by only implementing the methods they need. By looking for signs such as low cohesion, fat interfaces, empty methods, and difficult testing scenarios, you can identify ISP violations early and refactor your code to maintain modular, flexible design.
🤖 Don't fully get this? Learn it with Claude
Stuck on Techniques to Identify ISP Violations? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Techniques to Identify ISP Violations** (OO & Low-Level Design) and want to truly understand it. Explain Techniques to Identify ISP Violations from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Techniques to Identify ISP Violations** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Techniques to Identify ISP Violations** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Techniques to Identify ISP Violations** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.