There are some methods and member variables in some classes that look so much similar and I thought I can refactor and abstract them away… but I am not sure if this is the correct way of doing this:
so for example let’s say I have these two classes:
public class CaseField
{
private int mlObjectKey = CommonIndepClass.GetNextObjectKey();
public int ObjectKey
{
get
{
return mlObjectKey;
}
}
// some other specific methods...
}
and then another class:
public class AsOfField
{
private int mlObjectKey = CommonIndepClass.GetNextObjectKey();
public int ObjectKey
{
get
{
return mlObjectKey;
}
}
// some specific methods ...
}
So I was thinking if it is correct to create an Abstract class and factor those methods and member variables and put them in that abstract class:
public abstract class CommonFields
{
private int mlObjectKey = CommonIndepClass.GetNextObjectKey();
public int ObjectKey
{
get
{
return mlObjectKey;
}
}
}
public class CaseField: CommonFields
{
// just its own specific methods and member variables.
}
public class AsOfField: CommonFields
{
// just its own specific methods and member variables.
}
That seems not unreasonable.
An alternative is to implement this functionality using mixins. That would allow you to define one class with common functionality, but you wouldn’t have to implement a common base class. See this SO answer for more details.