Reputation: 14711
I have an Application class in my Android App, lets call it
MyApplication.
I have utility classes that I instatiate once in it and then use everywhere in the app
Lets say:
TimeConverter
ImageManager
now, I need to pass those in some asynctask class' constructor
Is there a difference between those two ways of doing so?:
Variant 1:
I pass each of those individually
MyApplication application = (MyApplication) getApplication();
new SomeAsyncTask(application.timeConverter, application.imageManager).execute():
class SomeAsyncTask extends AsyncTask<Void, Void, Void> {
TimeConverter timeConverter;
ImageManager imageManager;
public SomeAsyncTask(TimeConverter timeConverter, ImageManager imageManager) {
this.timeConverter = timeConverter;
this.imageManager = imageManager;
}
doInBackground...
}
Variant 2:
MyApplication application = (MyApplication) getApplication();
new SomeAsyncTask(application).execute():
class SomeAsyncTask extends AsyncTask<Void, Void, Void> {
TimeConverter timeConverter;
ImageManager imageManager;
public SomeAsyncTask(MyApplication application) {
this.timeConverter = application.timeConverter;
this.imageManager = application.imageManager;
}
doInBackground...
}
So is there some tangible difference in these two ways of using a constructor from a OOP standpoint (or any other standpoint)
Upvotes: 0
Views: 26
Reputation: 2370
There is not really a different, but the variant 2 is more flexible.
Because if you want to expand the construction, the caller doesn't need to change the method, all new parameters (eg. Stringconverter or something like that) can be in the application object too.
This princip in Java is called value object or DTO (Data Transfer Object) See: http://martinfowler.com/bliki/ValueObject.html
Upvotes: 1