What is Interface segregation ?
Interface segregation is a principle in object-oriented programming that suggests that clients should not be forced to depend on interfaces they do not use. In other words, it promotes the idea of creating specialised interfaces that cater to specific client requirements rather than having a single interface that encompasses all possible functionalities.
This principle defines that interface should be thin in nature, in other word it should contain ONLY required API’s that are needed. It defines that interface should not be FAT in nature, in other word it should not contain many API’s that are not needed.
This design principle is used when we create Base class and its API or only interface class.
When we will get to know that We did not follow this Design principle ?
If we are creating a derived class and we are forced to define the empty virtual function then we are NOT following “interface segregation” and our base interface is FAT and it contains many not required API. This should be corrected.
Example:
#include <iostream> // Interface for a printer class IPrinter { public: virtual void print() = 0; }; // Interface for a scanner class IScanner { public: virtual void scan() = 0; }; // Printer class implementing IPrinter interface class Printer : public IPrinter { public: void print() override { std::cout << "Printing..." << std::endl; } }; // Scanner class implementing IScanner interface class Scanner : public IScanner { public: void scan() override { std::cout << "Scanning..." << std::endl; } }; // Photocopier class implementing both IPrinter and IScanner interfaces class Photocopier : public IPrinter, public IScanner { public: void print() override { std::cout << "Printing..." << std::endl; } void scan() override { std::cout << "Scanning..." << std::endl; } }; int main() { Printer printer; Scanner scanner; Photocopier photocopier; // Using printer IPrinter* pPrinter = &printer; pPrinter->print(); // Using scanner IScanner* pScanner = &scanner; pScanner->scan(); // Using photocopier IPrinter* pPhotocopierPrinter = &photocopier; pPhotocopierPrinter->print(); IScanner* pPhotocopierScanner = &photocopier; pPhotocopierScanner->scan(); return 0; }
Explanation:
Interface segregation is a principle in object-oriented
In the above example, we have two interfaces: IPrinter
, IScanner
, and two classes implementing those interfaces: Printer
and Scanner
. The Photocopier
class implements both the IPrinter
and IScanner
interfaces.
By segregating the interfaces, we can provide different functionalities to clients depending on their needs. The Printer
class only exposes the print()
method, and the Scanner
class only exposes the scan()
method. The Photocopier
class provides both print()
and scan()
methods, suitable for clients that require both functionalities.
In the main()
function, we demonstrate how different clients can use the appropriate interfaces. We create instances of Printer
, Scanner
, and Photocopier
and utilise the respective interfaces based on the functionality required.
By adhering to the interface segregation principle, we ensure that clients are not forced to depend on interfaces they do not need, leading to more cohesive and maintainable code.