I have 2 methods with the exact same logic:
Dog RunDog()
{
// a LOT of businees logic
return DogMethod(dogParams);
}
Employee RunEmployee()
{
// the exact same logic from above
return EmployeeMethod(employeeParams (can be easily converted to/from dogParams));
}
Is there a common design pattern to help me avoid the code duplication?
Perhaps something like:
T RunT()
{
// Logic...
// Invoke DogMethod/EmployeeMethod depending on T and construct the params accodringly
}
I chose Dog/Employee to emphasize that there's no easy way to convert between the two.
If the two methods return different types then they do different things although they use the same business logic internally. So I would extract the commonly used business logic like
class Running
{
public Dog RunDog()
{
var dogParams = GetParams();
return DogMethod(dogParams);
}
public Employee RunEmployee()
{
var dogParams = GetParams();
var employeeParams = ConvertParams(dogParams);
return EmployeeMethod(employeeParams);
}
private DogParams GetParams()
{
// a LOT of business logic
}
}