|
i have a scenario as follow:
public class A : class B
{
public SomeMethodA()
{
//.... do some logics here
SomeMethodB();
}
private SomeMethodB()
{
//.... do some logics here
}
}
class B : class C
class X : class Y
{
public SomeMethodA()
{
//.... do some logics here
SomeMethodB();
}
private SomeMethodB()
{
//.... do some logics here
}
}
class Y : class Z
class A has the exact same method as class X does. Now the question is: How do I combine this method in centralised so that my code has DRY (don't repeat yourself) without modifying/touching class Y, Z and class B, C? Class B, C, Y, and Z are our legacy code and we are NOT trying to alter/modify these classes.
any thoughts?
|
|
|
|
|
Aggregation.
Create a class D and put the two methods in. In the constructors of A and X, instantiate D; later on, forward the call to D.
Something like:
internal class D
{
public SomeMethodA()
{
SomeMethodB();
}
private SomeMethodB()
{
}
}
public class A : class B
{
protected D _D;
public A()
{
_D = new D();
}
public SomeMethodA()
{
_D.SomeMethodA();
}
}
|
|
|
|
|
ron01234 wrote: any thoughts?
Refactoring is something that must be expected in software development.
Of course deferring it is an option but only at the expense of increasing complexity and likely more fragile code. Which I suspect your current path is going to start experiencing.
|
|
|
|
|
I agree with jschell - unfortunately sometimes you just cannot have your cake and eat it, otherwise you can end up creating a spaghetti like mess of code(I know of this because that is how I learnt to code by creating a mess that I had to clean up).
The simple, and perhaps simplistic, answer is to create an abstract class and inherit from that overrriding those methods that are not the same across classes.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
GuyThiebaut wrote: that is how I learnt to code by creating a mess that I had to clean up
Which is how I learned as well.
|
|
|
|
|
I learnt to write spagetti code in a very ancient BASIC that didn't have any structured programming features.
There weren't any college courses on programming back then. They still get cancelled today due to lack of enrollment.
Q. Hey man! have you sorted out the finite soup machine?
A. Why yes, it's celery or tomato.
|
|
|
|