B
Baris
Given the C# code below, can anyone think of a better class design? What
really gets my goat is that the code within derived classes D1 and D2 is
identical and in my mind should be refactored into a base class, but how
can I move this particular 'shared functionality' into a base class?
If anyone can suggest a better newsgroup for this, such as one focussed
primarily on C# class design, please let me know.
[This could be easily done with an intermediate template class in the
hierarchy in C++, I think.]
// quandary.cs
// compile with csc.exe quandary.cs
class app
{
public static void Main()
{
D1 d1 = new D1();
D2 d2 = new D2();
D1.SetFoo("something marvellous, specific to class D1");
D2.SetFoo("something wonderful , specific to class D2");
System.Console.WriteLine(d1.GetFoo());
System.Console.WriteLine(d2.GetFoo());
}
}
abstract class Base
{
public abstract string GetFoo();
}
class D1 : Base
{
private static string _foo;
public static void SetFoo(string s)
{
_foo = s;
}
public override string GetFoo()
{
return _foo;
}
}
class D2 : Base
{
private static string _foo;
public static void SetFoo(string s)
{
_foo = s;
}
public override string GetFoo()
{
return _foo;
}
}
// quandary.cs ends
really gets my goat is that the code within derived classes D1 and D2 is
identical and in my mind should be refactored into a base class, but how
can I move this particular 'shared functionality' into a base class?
If anyone can suggest a better newsgroup for this, such as one focussed
primarily on C# class design, please let me know.
[This could be easily done with an intermediate template class in the
hierarchy in C++, I think.]
// quandary.cs
// compile with csc.exe quandary.cs
class app
{
public static void Main()
{
D1 d1 = new D1();
D2 d2 = new D2();
D1.SetFoo("something marvellous, specific to class D1");
D2.SetFoo("something wonderful , specific to class D2");
System.Console.WriteLine(d1.GetFoo());
System.Console.WriteLine(d2.GetFoo());
}
}
abstract class Base
{
public abstract string GetFoo();
}
class D1 : Base
{
private static string _foo;
public static void SetFoo(string s)
{
_foo = s;
}
public override string GetFoo()
{
return _foo;
}
}
class D2 : Base
{
private static string _foo;
public static void SetFoo(string s)
{
_foo = s;
}
public override string GetFoo()
{
return _foo;
}
}
// quandary.cs ends