Single Responsibility Principle the correct approach

SRP really bug me. I know how to find responsibility what I dont know is how to assemble them correctly
e.g

class Modem{
  public void dial(){//code here}
  public void hangup(){//code here}
  public void send(){//code here}
  public void recive(){//code here}
}

In this common example we have connection and communication responsibility. So we divide that into 2 new interfaces

interface Connection{ 
  public void dial();
  public void hangup();
}

interface Communication{ 
  public void send();
  public void recive();
}

implement them:

class SimpleModemConnection implements Connection{ 
  public void dial(){//code here}
  public void hangup(){//code here}
}

class SimpleModemCommunication implements Communication{ 
  public void send(){//code here}
  public void recive(){//code here}
}

and at this point I dont really know how client code should look like

Do I aggregate those intefaces into Modem class ?

class Modem {
 Connection connection = new SimpleModemConnection();
 Communication communication = new SimpleModemCommunication(); 
}

main(){
 Modem modem = new Modem();
 modem.connection.dial();
 modem.communication.send();
 modem.communication.recive();
 modem.connection.hangup();
}

Do I use them directly

main(){
 Connection connection = new SimpleModemConnection();
 Communication communication = new SimpleModemCommunication(); 
 connection.dial();
 communication.send();
 communication.recive();
 connection.hangup();
}

Or is there some other way?


Source: oop

Leave a Reply