Interface Segregation Principle (ISP)
The Interface Segregation Principle (ISP) is one of the five SOLID principles of object-oriented programming (OOP). It states that "Many client-specific interfaces are better than one general-purpose interface." In other words, we should avoid creating large, monolithic interfaces that expose a lot of functionality that not all clients need. Instead, we should create smaller, more specific interfaces that only expose the functionality that a particular client needs.
here is an example of how the Interface Segregation Principle (ISP) can be violated:
public interface Shape {
void draw();
void resize();
void rotate();
void translate();
}
public
class
Circle
implements
Shape
{
private
double radius;
public
Circle(double radius)
{
this.radius = radius;
}
@Override
public
void draw() {
// Draw a circle
}
@Override
public void resize() {
// Resize the circle
}
@Override
public void rotate() {
// Rotate the circle
}
@Override
public void translate() {
// Translate the circle
}
}
public
class
Triangle
implements
Shape
{
private
double sideA;
private
double sideB;
private
double sideC;
public
Triangle(double sideA, double sideB, double sideC)
{
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
@Override
public
void draw() {
// Draw a triangle
}
@Override
public void resize() {
// Resize the triangle
}
@Override
public void rotate() {
// Rotate the triangle
}
@Override
public void translate() {
// Translate the triangle
}
}
The Shape
interface defines five methods: draw
, resize
, rotate
, translate
, and getArea
. However, not all shapes support all of these methods. For example, a circle does not support the getArea
method, and a triangle does not support the rotate
method. This violates the ISP because it forces all shapes to implement methods that they do not support.
A better way to design this code would be to create separate interfaces for each type of shape:
public interface Drawable {
void draw();
}
public interface Resizable {
void resize();
}
public interface Rotatable {
void rotate();
}
public interface Translatable {
void translate();
}
public interface Shape extends Drawable, Resizable, Translatable {
double
getArea();
}
public
class
Circle
implements
Shape
{
private
double radius;
public
Circle(double radius)
{
this.radius = radius;
}
@Override
public void draw() {
// Draw a circle
}
@Override
public void resize() {
// Resize the circle
}
@Override
public void translate() {
// Translate the circle
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
public
class
Triangle
implements
Shape
{
private
double sideA;
private
double sideB;
private
double sideC;
public
Triangle(double sideA, double sideB, double sideC)
{
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
@Override
public
void draw() {
// Draw a triangle
}
@Override
public void resize() {
// Resize the triangle
}
@Override
public void translate() {
// Translate the triangle
}
@Override
public double getArea() {
// Calculate the area of the triangle
}
}
This code follows the ISP because the Shape
interface only defines methods that are common to all shapes, and the Drawable
, Resizable
, Rotatable
, and Translatable
interfaces define methods that are specific to certain types of shapes. This allows us to only implement the methods that are relevant to a particular shape, and it makes our code more modular and easier to maintain.
Top comments (0)