Interface segregation principle

      Comments Off on Interface segregation principle

Home Forums Programming Software engineering Object-oriented programming Interface segregation principle

This topic contains 0 replies, has 1 voice, and was last updated by  Васильев Владимир Сергеевич 2 weeks, 4 days ago.

  • Author
    Posts
  • #4088

    The interface segregation principle claims: child classes should not be forced to implement interface that they do not use. In other words, if a child class does not need a part of the functionality provided by its base class, you chose the wrong base class or the base class violates the interface segregation principle.

    We are talking about the base types that force their child classes to know and do too much.

    Let’s assume we have a base interface for a certain device. It provides us the functionality for making calls and using the phone book, and also for playing games and surfing the Internet, reading books, and using an alarm clock.

    Thus, it is assumed that all the classes that implement this interface will have at least this functionality. But the question is whether those child classes really need all these options in the same device. This phenomenon has a specific name – “fat” interface and it manifests itself in the overfilling the functionality of the base type (class or interface).

    That’s an example of how it can be when you overfill the base type with extra functionality:

    interface IDevice
    {
       void MakeACall(string number);
       string FindContact(string name);
       void CreateNewContact(string number, string name);
       void CreateNewMessage(string text);
       void SendMessage(string to, string text);
       DateTime GetCurrentTime();
       void SetTime(DateTime time);
       void SetAlarmTime(DateTime time);
       void ChooseMelody(string name);
       void ConnectToInternet(string password);
       void UseBrowser(string name);
       void DownloadProgram(string name);
       void UseOnlineService(string name);
       void FindBook(string name);
       void OpenBook(string name, int page);
       void NextPage(string book, int currentPage);
       void PreviousPage(string book, int currentPage);
       void CreateBookmark(string book, int page);
    }

    Not every reading device must make calls and download additional programs and be an alarm clock, as well as not every phone should be a reading device and to be able to download programs from the Internet. And so on. Obviously, the approach to the development of the interface had to be more subtle, thoughtful.

    If you try to divided all the functionality of the interface into logical groups, you get a pretty nice result:

    interface ICallingDevice
    {
       void MakeACall(string number);
       string FindContact(string name);
       void CreateNewContact(string number, string name);
       void CreateNewMessage(string text);
       void SendMessage(string to, string text);
    }
     
    interface IAlarmClock
    {
       DateTime GetCurrentTime();
       void SetTime(DateTime time);
       void SetAlarmTime(DateTime time);
       void ChooseMelody(string name);
    }
     
    interface INetworkDevice
    {
       void ConnectToInternet(string password);
       void UseBrowser(string name);
       void DownloadProgram(string name);
       void UseOnlineService(string name);
    }
    interface IReaderDevice
    {
       void FindBook(string name);
       void OpenBook(string name, int page);
       void NextPage(string book, int currentPage);
       void PreviousPage(string book, int currentPage);
       void CreateBookmark(string book, int page);
    }

    ICallingDevice interface encapsulates the logic for making calls, the interface IAlarmClock – is responsible for logic which concerns the use of a clock and alarm clock, the interface INetworkDevice – is responsible for the behavior of the network device, the interface IReaderDevice – allows its child classes to implement the functionality of reading devices (e-book).

    All options are clearly delineated in different logical areas, and it will be easier to endow the child classes – only with the functionality that is needed, not including anything extra. Thus, the second approach demonstrates the clever use of the interface segregation principle.

You must be logged in to reply to this topic.