I've recently read several articles about the advantages of the composition over inheritance. Their authors said that you can always replace inheritance with composition (to be precise, they say they don't know such situations where it can't be done). However, I can't think how to do this in the following case:
abstract class Reader
{
public Int32 ReadSum()
{
return this.ReadValue() + this.ReadValue();
}
public Int32 ReadSub()
{
return this.ReadValue() - this.ReadValue();
}
public Int32 ReadMul()
{
return this.ReadValue() * this.ReadValue();
}
public Int32 ReadDiv()
{
return this.ReadValue() / this.ReadValue();
}
public abstract Int32 ReadValue();
}
class RandomSource: Reader
{
public Int32 ReadValue()
{
return Math.RandomInt32();
}
}
class UserSource: Reader
{
public Int32 ReadValue()
{
return Console.ReadInt32();
}
}
Is this at all possible?