c# - How to avoid duplication? -
i'm making c# application. application has 2 classes , multiple methods. while writing code stumbled on problem. use same 2 variables (xlist , ylist) , 1 method in both classes. , need more classes code. created duplication problem. below simple version of code:
public class { private testentities db = new testentities(); public list<int> xlist = new list<int>(); public list<int> ylist = new list<int>(); public void getallinfo() { // data database , add list xlist = db.table1.tolist(); ylist = db.table2.tolist(); } public void dostuff() { // stuff xlist , 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() { // data database , add list (the same in class a) xlist = db.table1.tolist(); ylist = db.table2.tolist(); } public void dodifferentstuff() { // ddifferent stuff xlist , ylist in class } }
my question best way solve duplication problem?
after research found can solve inheritance or composition. read people choose composition on inheritance. wrote following code solve 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() { // data database , add list xlist = db.table1.tolist(); ylist = db.table2.tolist(); } // implement other methods } public class { public void methodname() { datapreparation datapreparation = new datapreparation(); datapreparation.getallinfo(); usedatax(datapreparation.xlist); usedatay(datapreparation.ylist); // implementation usedatax() , usedatay() } } public class b { public void methodname() { datapreparation datapreparation = new datapreparation(); datapreparation.getallinfo(); visualizedatax(datapreparation.xlist); visualizedatay(datapreparation.ylist); // implementation visualizedatax() , visualizedatay() } }
as can see made class handles getting data database. , class , b use datapreparation class. best way solve duplication? or should use inheritance or different?
i think should have 1 method called dostuff()
rather 1 called dostuff()
, called dodifferentstuff()
.
then can create abc implement common code, , have abstract dostuff()
method 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() { // data database , add list (the same in class a) xlist = db.table1.tolist(); ylist = db.table2.tolist(); } public abstract void dostuff(); } public class a: base { public override void dostuff() { // stuff xlist , ylist } } public class b: base { public override void dostuff() { // ddifferent stuff xlist , ylist in class } }
(i think it's bad idea have public fields - i'm guessing/hoping sample code , real code doesn't have those...)
other code (except code creates a
or b
) use object via base
class type.
Comments
Post a Comment