loop
loop

Reputation: 9242

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.

Upvotes: 0

Views: 145

Answers (3)

Deniz
Deniz

Reputation: 888

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.

Upvotes: 4

sarin
sarin

Reputation: 5307

Remember:

  • Single responsibility: each interface looks after a single responsibility
  • Open for extension closed for modification:
  • Liskov Substitution: Interfaces are passed round so you can substitute different classes that all use the interface
  • Interface segregation: because of the separation one change wont affect another!
  • Dependency inversion: By using interfaces and implementing your SOLID rules you are ready for Inversion of Control and Dependency Injection!

So when you create an instance of IModemConnection and call Dial(123456) it passes back an instance of an interface IModemDataExchange. This basically says once you have connected you are then interested in exchanging data from any class that use the interface IModemDataExchange. Anything else on the class is in addition!

Upvotes: 0

yar_sh
yar_sh

Reputation: 511

I didn't go through the whole article, but the example is simple: when you have just public void Dial(String number); in IModemConnection the objects that are used via this interface can just dial and... well, if they just dial, because of void return "type" there is nothing else to do after calling this method. A bit useless, right?
So in order to do something after calling the Dial method the interface now returns IModemDataExchange object that can be used to Send and Receive.
It could be pretty ok to return void if the IModem interface wasn't split in 2 separate interfaces (you can see .NET SqlConnection Open method as example of the same method concept that returns void). But to force and show the SRP principle the author decided to give this example and by this design Dial(String number) should return the IModemDataExchange result that can be used to do other operations after dial.

Upvotes: 1

Related Questions