I am currently on a project and need to use a class created by another developer inside an existing class I developed.
Not the real code, but this is similar to the current code (I removed the interface for the sake of brevity)
class MyClass // this is my class
{
uint data;
// other methods here, omitting it
}
class AnotherClass<T> // class from another developer, I need to include this inside my class
{
void MyMethod()
{
T data;
}
}
This AnotherClass needs to go inside MyClass (composition).
The MyClass has been widely used on the program (it has an interface but I removed it here for simplicity of the example) and being instantiated by a factory.
class MyClassFactory
{
public static MyClass GetNewInstance()
{
return new MyClass();
}
}
public class TestClass
{
public TestClass()
{
var c = MyClassFactory.GetNewInstance();
var d = MyClassFactory.GetNewInstance();
var e = MyClassFactory.GetNewInstance();
var f = MyClassFactory.GetNewInstance();
var g = MyClassFactory.GetNewInstance();
}
}
I am having a discussion with the developer and he wants me to change my MyClass to be a generic based, which means, that will introduce changes on lots of area
public class TestClass
{
public TestClass()
{
var c = MyClassFactory.GetNewInstance<uint>(); // i need to revisit ALL the getnewinstance call and add the generic type
var d = MyClassFactory.GetNewInstance<byte>();
var e = MyClassFactory.GetNewInstance<long>();
}
}
class MyClassFactory
{
public static MyClass<T> GetNewInstance<T>()
{
return new MyClass<T>();
}
}
class MyClass<T> // this is my class
{
uint data;
AnotherClass<T> anotherclass;
// other methods here, omitting it
}
class AnotherClass<T> // class from another developer, I need to include this inside my class
{
void MyMethod()
{
T data;
}
}
It seems not a scalable solution since if I have 1,000 calls to MyClassFactory.GetNewInstance, all the 1,000 calls needs to be revisited and change to appropriate data type. To avoid the domino effect changes, I instead suggested to just provide the type via enum.
enum DataType
{
Byte,
Int
}
class MyClass
{
uint data; // this is an existing variable that is being used on myclass computations, anotherclass doesn't need this variable. This doesn't need to be generic
object anotherClass;
MyClass(DataType d = DataType.Int) // default parameter, most of the types are int and this will lessen the change
{
switch(d)
{
case DataType.Byte:
anotherClass = AnotherClass<byte>();
break;
case DataType.Int:
anotherClass = AnotherClass<int>();
break;
}
}
}
This will result in less changes, but I personally don't like the casting that will be needed for the object. I can use dynamic datatype but still, there is no compiler-check safety.
Is there a methodology or design pattern to solve this kind of problem?
Edit : just to elaborate, the generic specifier is only for the anotherclass. The myclass doesn't need the generic specifier. Basically, the aim of the author of the anotherclass is to use the smallest datatype possible, hence the use of generics on the another class.
All the computations inside the myclass uses uint. But the anotherclass uses the smallest datatype possible.
I guess the question is, how to fit it in with an existing class that is already being heavily used on the source code with minimal modifications.
Aucun commentaire:
Enregistrer un commentaire