I'm fairly new to programming and i'm having trouble to understand how to apply effectively the principle showed in the following link (the ATM one):
http://www.objectmentor.com/resources/articles/isp.pdf
Basically it starts with a design that does not complain the ISP (Interface Segregation Principle), and moves forward to refactor the behavior into different interfaces.
My question is: Don't we use interfaces to express common behaviour among not so (or not) related abstractions?
What's the point of encapsulating methods in an interface, if not even one is going to be shared with the classes that are going to implement them? In which scenario this could be consider useful?
If we continue the line of the example, the following code is given:
public interface ITransaction
{
void Execute();
}
public interface IDepositUi
{
void RequestDepositAmount();
}
public class DepositTransaction : ITransaction
{
private IDepositUi depositUI;
public DepositTransaction(IDepositUi ui)
{
depositUI = ui;
}
public virtual void Execute()
{
/*code*/
depositUI.RequestDepositAmount();
/*code*/
}
}
public interface WithdrawalUI
{
void RequestWithdrawalAmount();
}
public class WithdrawalTransaction : ITransaction
{
private WithdrawalUI withdrawalUI;
public WithdrawalTransaction(WithdrawalUI ui)
{
withdrawalUI = ui;
}
public virtual void Execute()
{
/*code*/
withdrawalUI.RequestWithdrawalAmount(); /*code*/
}
}
public interface TransferUI
{
void RequestTransferAmount();
}
public class TransferTransaction : ITransaction
{
private TransferUI transferUI;
public TransferTransaction(TransferUI ui)
{
transferUI = ui;
}
public virtual void Execute()
{
/*code*/
transferUI.RequestTransferAmount();
/*code*/
}
}
public interface UI : IDepositUi, WithdrawalUI, TransferUI
{
}
As far as i understand this, in order to use the previous design we should have something like:
UI impui = new IMPLEMENTATIONUI(); // Some UI implementation
DepositTransaction dt = new DepositTransaction(Gui);
dt.Execute();
Now, wouldn't we need that the IMPLEMENTATIONUI implements every single method? And if so, wouldn't it break the SRP?.