Given the following interface:
public interface IFoo
{
bool Foo(Person a, Person b);
}
and the following two implementations of the above:
public class KungFoo : IFoo
{
public bool Foo(Person a, Person b)
{
if (a.IsAmateur || b.IsAmateur) // common logic
return true;
return false;
}
}
public class KongFoo : IFoo
{
public bool Foo(Person a, Person b)
{
if (a.IsAmateur || b.IsAmateur) // common logic
return false;
return true;
}
}
where should I place the “common logic” (as commented in the code) so it is in just one place (e.g. as a Func) and does not need to be repeated (as is the case above) for multiple implementations?
Note that the example above is very trivial but the real-life “common logic” is more complicated and the Foo() method does something useful!
I hope the question is clear (and not already been answered elsewhere – I did do a search) but feel free to probe me for more details if required.
In a common abstract class: