Design: Object method vs separate class's method which takes Object as parameter?
https://softwareengineering.stackexchange.com/questions/77690
-
01-11-2019 - |
题
For example, is it better to do:
Pdf pdf = new Pdf();
pdf.Print();
or:
Pdf pdf = new Pdf();
PdfPrinter printer = new PdfPrinter();
printer.Print(pdf);
Another example:
Country m = new Country("Mexico");
double ratio = m.GetDebtToGDPRatio();
or:
Country m = new Country("Mexico");
Country us = new Country("US");
DebtStatistics ds = new DebtStatistics();
double usRatio = ds.GetDebtToGDPRatio(us);
double mRatio = ds.GetDebtToGDPRatio(m);
My concern in the last example is there are potentially endless statistics (but let's say even just 10) you might want to know about a country; do they all belong on the country object?
e.g.
Country m = new Country("Mexico");
double ratio = m.GetGDPToMedianIncomeRatio();
These are simple ratios but lets assume the statistics are complicated enough to warrant a method.
Where is that line between operations that are intrinsic to an object vs operations that can be performed on an object but are not part of it?
没有正确的解决方案