DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Interface Segregation Principle and How to Interpret It

Want to learn more about the Interface Segregation Principle and other SOLID principles? Check out this post to learn more about working with ISP.

Vadim Samokhin user avatar by
Vadim Samokhin
·
Aug. 20, 18 · Analysis
Like (2)
Save
Tweet
Share
8.95K Views

Join the DZone community and get the full member experience.

Join For Free

I wish all of my interfaces were segregated as good as this fish at Catania fish market


In this fourth post on SOLID principles, we will take a look at the interface segregation principle and how to interpret it. If you want to check out the previous post on the Liskov Substitution Principle, you can find it here.

Definition

According to Robert Martin,

"The interface-segregation principle ( ISP) states that no client should be forced to depend on methods it does not use."

Besides, Wikipedia provides a concise description of code compiled with ISP:

"ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them. Such shrunken interfaces are also called role interfaces."

I believe there is a deep foundation behind this principle, much like Kent Beck’s XP values that are a foundation for his XP principles.

Correct Abstraction

Correct abstraction is key to the Interface Segregation Principle. Wiki’s definition states nothing more than that your abstractions should be correct, thus the classes implementing them end up being small, cohesive, and solid.

Finding correct abstractions is more of an art. You should explore your domain, build some semantic nets, come up with a set of user stories, draw interaction diagrams —and, all of that doesn’t necessarily lead you to correct abstractions. Wrong abstractions are worse than no abstractions at all, so don’t forget about the Rule of Three.

When you think you’re done with some of your abstractions, represent them as interfaces. The techniques mentioned above reinforce them to be Role Interfaces. With this approach, concrete classes implement only as much as those abstractions require them to. So, we end up satisfying the Interface Segregation Principle without knowing that it exists.

Violation of the Interface Segregation Principle

When a client depends on methods that it doesn’t use, it means that your abstractions are wrong. Martin Fowler’s example of the Role Interface (which is a natural consequence of applying ISP) exposes a wrong initial object decomposition. And, it doesn’t take a standalone principle to claim that. The code is plainly not cohesive. So, don’t ponder on whether your code violates the Interface Segregation Principle; think about whether your abstractions are correct.

Code Example?

No code example — there's not much sense in it. Giving some random piece of code won’t tell much about domain abstractions and the contracts they follow. Anyways, there are plenty of examples on the Internet where a class has to implement a fat interface’s behavior that it’s not supposed to.

Final Thoughts

This principle comes naturally when you start decomposing your problem space by identifying major roles that take part in your domain. And, it’s never a mechanical action. No principle can automatically lead you to the correct object model.

So, ISP is a poor guidance when designing software, but an excellent indicator of whether it’s healthy or not. Don’t think about ISP when designing your software. Think about whether your abstractions are reusable and composable, and whether your objects are encapsulated and cohesive.

Interface segregation principle Interface (computing) Abstraction (computer science) IT

Published at DZone with permission of Vadim Samokhin. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • What Are the Different Types of API Testing?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: