Search code examples
c#solid-principles

Single responsibility principle confusion


i am going through this article to understand SRP. There is a IModem interface initially

interface IModem : IDisposable
{
    void Dial(String number);
    void Send(char c);
    char Recv();
}

This interface has two responsibilities One is Connection and other is Data Exchange so it should be broken down in sub-interface and what is done :-

interface IModemConnection : IDisposable
{
    void Dial(String number);
} 

interface IModemDataExchange
{
    void Send(char c);
    char Recv();
}

Till this part i got understand but further the above interface are changed like below and i am not able to get what public IModemDataExchange Dial(String number); Part is doing.

interface IModemConnection : IDisposable
{
    IModemDataExchange Dial(String number);
}

interface IModemDataExchange
{
    void Send(char c);
    char Recv();
}

can anybody tell me why we have done that.


Solution

  • Tutorial has a sentence like

    The modem connection implementation becomes a factory for a data exchange implementation

    . As far as I understand, author aimed to return a dataexchange object whenever you dial another modem (when a connection is established). Of course there are other issues about signalling part, for ex. accepting the call/request and etc. However, author did not go into this detail, because this may be a little bit out off topic for this tutorial. In my opinion Dial returns an IModemDataExchange that you can call Send/Recv on that object. For example,

    using (IModemConnection modemConnection = new IsdnModem())
    {
        IModemDataExchange dataExchange = modemConnection.Dial("123456")
        dataExchange.Send("Hello");
    }
    

    As the author implies, Dial calls another modem and creates a data path in order to communicate through this data path. Its about factory pattern also.