Interface Segregation Principle explained with simple example

Interface Segregation Principle (ISP) is a software design principle which states that “many client specific interfaces are better than one generic interface“. This principle enforces to implement only usable methods which will reduce coupling between modules. This principle ensures that any client should be dependent only on those methods which they use.

Before going ahead we should know why do we need software design principle and what is software design principle.

Interface Segregation Principle (ISP) advantages:

  • Abstraction is enforced as every class needs/implements only what they need.
  • Implemented classes are small which are easy to maintain and test.
  • Dependencies between classes are relatively simpler.
  • Reusable, flexible and easily extensible code.

Let’s understand Interface Segregation Principle (ISP) with a simple example:

Consider a problem where we have to create a employee/manager interface design.

// Sample code. Syntax not verified
class employees_action
{
	public:
		virtual void work () = 0;
		virtual void lunchbreak () = 0;
};

class employee : public employees_action
{
	public:
		void work ()
		{
			// Do some work
		}

		void lunchbreak ()
		{
			// have lunch
		}
};

class manager: public employee
{
	public:
		void setEmployee (employee *e)
		{
			// Set this employee to manager's list
		}

		void supervise ()
		{
			// supervise all associated worker list
		}
};

Let’s analyse above classes from Interface Segregation principle perspective:

  • Let’s say if we have to add one more kind of employee “BusDriver” whose job is to pickup and drop employees then according to above design, “BusDriver” class has to inherit from “employees_action” abstract class and unnecessarily needs to define “lunchbreak” method too (Assuming bus driver job is couple of hours in morning/evening). This will create unnecessary codes in software.
  • Above point is a violation of ISP principle which states that “any class should implement only necessary interfaces” and these kinds of interface designs are called “polluted interfaces“.

Let’s redesign the class according to ISP principle

// Sample code. Syntax not verified
class employees_action_work
{
	public:
		virtual void work () = 0;
};

class employee_action_lunch
{
	public:
		virtual void lunchbreak () = 0;
}

class employee : public employees_action_work, employee_action_lunch
{
	public:
		void work ()
		{
			// Do some work
		}

		void lunchbreak ()
		{
			// have lunch
		}
};

class busDriver : public employees_action_work
{
	public:
		void work ()
		{
			// Do some work
		}
};

class manager: public employee
{
	public:
		void setEmployee (employees_action_work *e)
		{
			// Set this employee to manager's list
		}

		void supervise ()
		{
			// supervise all associated worker list
		}
};

Let’s analyse above classes from ISP perspective.

Each classes are defining only those methods which they require. Non of the classes has any unusable method implementation. This means that ISP principle is being followed.

Leave a Reply

Your email address will not be published. Required fields are marked *