Say hi to the method:
public void hello(Data data);
This method uses some data and performs… whatever. Now, written like this, the method is very specific – i.e. the argument is very specific. But now, in real life, this Data
(for example) may come from e.g. class called DataProvider
:
public class DataProvider() {
public Data getMeData(int from, int to);
}
Now most of the times when we call hello
we need to call dataProvider.getMeData()
. Since this is repetitive, you might do:
public void hello(int from, int to);
Which is essentially:
public void hello(DataProvider dp);
This looks convenient as, maybe, in future, I will need different data besides the Data
class, and no signature will be changed. In other words, method itself will know which data to take.
This second case is an example of a method with more general arguments.
Which one would make more sense:
- The first one, more strict, more focused.
- The second one, generic, but less focused.
This is not about passing custom objects. In both cases, custom objects are passed. The difference is that the scope of passing the object is different. The first one contains just raw data. The second one is more general, contains more what that method needs.
Data
object is the most feasible solution.