How to avoid duplication?
I am making a C # application. The application has two classes and several methods. While writing the code, I came across a problem. I am using the same two variables (XList and YList) and one method in both classes. And I probably need more classes with this code. So I created a duplication problem. Below is a simple version of my code:
public class A {
private testEntities db = new testEntities();
public List<int> XList = new List<int>();
public List<int> YList = new List<int>();
public void GetAllInfo()
{
// Get the data from a database and add to a list
XList = db.Table1.ToList();
YList = db.Table2.ToList();
}
public void DoStuff()
{
// Do Stuff with XList and YList
}
}
public class B {
private testEntities db = new testEntities();
public List<int> XList = new List<int>();
public List<int> YList = new List<int>();
public void GetAllInfo()
{
// Get the data from a database and add to a list (the same as in class A)
XList = db.Table1.ToList();
YList = db.Table2.ToList();
}
public void DoDifferentStuff()
{
// Do ddifferent stuff with XList and YList then in class A
}
}
My question is, is this the best way to solve this duplication problem?
After some research, I found that I can probably solve this with inheritance or composition. I also read that people choose composition over inheritance. So I decided to write the following code to eliminate duplication:
public class DataPreparation
{
private testEntities db = new testEntities();
public List<int> XList = new List<int>();
public List<int> YList = new List<int>();
public void GetAllInfo()
{
// Get the data from a database and add to a list
XList = db.Table1.ToList();
YList = db.Table2.ToList();
}
// Implement other methods
}
public class A
{
public void MethodName()
{
DataPreparation dataPreparation = new DataPreparation();
dataPreparation.GetAllInfo();
UseDataX(dataPreparation.XList);
UseDataY(dataPreparation.YList);
// Implementation UseDataX() and UseDataY()
}
}
public class B
{
public void MethodName()
{
DataPreparation dataPreparation = new DataPreparation();
dataPreparation.GetAllInfo();
VisualizeDataX(dataPreparation.XList);
VisualizeDataY(dataPreparation.YList);
// Implementation VisualizeDataX() and VisualizeDataY()
}
}
As you can see, I have created a class that processes data from the database. And that class A and B are using DataPreparation class. But is this the best way to solve the duplication problem? Or should I be using inheritance or something else?
source to share
I think that you should probably only have one method DoStuff()
, not one under the name DoStuff()
and the other - DoDifferentStuff()
.
Then you can create an ABC to implement common code and have an abstract method DoStuff()
that is implemented differently in derived classes:
public abstract class Base
{
private testEntities db = new testEntities();
public List<int> XList = new List<int>();
public List<int> YList = new List<int>();
public void GetAllInfo()
{
// Get the data from a database and add to a list (the same as in class A)
XList = db.Table1.ToList();
YList = db.Table2.ToList();
}
public abstract void DoStuff();
}
public class A: Base
{
public override void DoStuff()
{
// Do Stuff with XList and YList
}
}
public class B: Base
{
public override void DoStuff()
{
// Do ddifferent stuff with XList and YList then in class A
}
}
(I also think it is a bad idea to have public fields like this, but I assume / hope this is just a sample code, and your real code does not have these ...)
Other code (other than code that generates A
or B
) will use the object using the class type Base
.
source to share
One easy option is to use inheritance . Create a base class with common functionality, and A
and B
can inherit from this. For example:
public abstract class Base
{
private testEntities db = new testEntities();
public List<int> XList = new List<int>();
public List<int> YList = new List<int>();
public void GetAllInfo()
{
// Get the data from a database and add to a list
XList = db.Table1.ToList();
YList = db.Table2.ToList();
}
}
public class A : Base
{
public void DoStuff()
{
// Do Stuff with XList and YList
}
}
public class B : Base
{
public void DoDifferentStuff()
{
// Do ddifferent stuff with XList and YList then in class A
}
}
source to share