Reputation: 14736
I have a solution with two projects each producing a separate dll that is used by another windows application. Each of those projects has a class called MyActions with only one method like so
Project1
public class MyActions
{
public SomeTypeA DoJob(string str1, string str2)
{
}
}
Project 2
public class MyActions
{
public SomeTypeB DoJob(string str1)
{
}
}
The two return types of these two classes are as below
public class SomeTypeA
{
public string stringA { get; set; }
public int someInt { get; set; }
}
public class SomeTypeB
{
public string someStringA { get; set; }
}
The method DoJob in the both the classes of these individual projects have almost 80% code that is the same. Project1 is the one whose MyActions
class's DoJob
method has some extra bits specific to only Project1.
Now here is the twist.. Project1 is eventually going to be scrapped and its dll will no longer be used.I want to write the code in the best possible way that ensures there is no repeat of code and so that I dont have to make any modifications to remove any un-required code in Project2 once Project1 is discontinued.
I was thinking of using inheritance and overriding the DoJob method. How would that work if their return types are different and they have different input parameters? Perhaps push one of the parameters from Project1's MyActions class to its constructor? I was also thinking of adding a link to Project2's MyActions class in Project1. But not sure about how to go ahead with implementing that and not repeating myself or possibly running into unforeseen problems later. Any tips, suggestions?
Upvotes: 2
Views: 109
Reputation: 1888
Your thought about inheritance is a good one. From your question in read between the lines that you were considering to let Project 1 inherit from Project 2. That's a possibility but probably not the best solution. Here is what I would suggest.
Create a super class for MyActions that both projects extend. Into this class you can move all the code that is shared across both projects (your 80% code of the method). The specific implementations in your MyAction in each project then implement the DoJob method as needed and make use of the provided methods from the super class.
Once you scrap project 1 there will be no changes that have to be made to project 2's code. You end up with a super class though that you do not really need any more in that case. However you won't be repeating yourself anywhere.
I am not yet familiar with the exact differences between java and C# so bear with me if there are differences. This is what code might look like in java.
abstract class AbstractMyActions {
protected SomeType commonMethodForBothProjects() {
...
}
}
public class MyActionsA extends AbstractMyActions {
public SomeType doJob(SomeParameter ..., SomeParameter ...) {
$this->commonMethodForBothProjects();
// Additional steps
}
}
You get the idea.
Upvotes: 1
Reputation: 9670
If (and only if) the parameters and the return types of the two methods in the two classes are actually different, factor out the code that is line-for-line identical, assuming it is one block, and just create a static method in a new static class, passing the parameters necessary for the common code.
If there are multiple blocks, just have multiple methods.
Call these methods as appropriate from each of the original methods.
If you wanted to create a hierarchical relationship between these classes, which you should only do if it is logical to do so, just make them both inherit a common type, and make the method above a protected method of that common class. Then just call it from the original methods.
Upvotes: 3
Reputation: 13450
public class MyActions
{
public ISomeType DoJob(ISomeParam item)
{
}
}
public class SomeTypeA : ISomeType
public class SomeTypeB : ISomeType
Upvotes: 1