dbulgakov dbulgakov - 2 months ago 17
Android Question

Base class for Dialogs in Android

I'm trying to implement two types of

Dialogs
in my
Android
application (Java), depending on device screen size.

So, in case if device screen width is less, then some value, then I want to show
BottomSheetDialogFragment
. And if it's more, I want to show simple
DialogFragment
. I've written logic for
BottomSheetDialogFragment
and understand now that some code will be duplicated in
DialogFragment
.

So is there any way to reduce the number of duplicated code in such situation?
For example, is it possible to create some base class for these dialogs?

Answer

I'd suggest some kind of facade pattern for a situation like this.

With that you would create a common facade for both dialog types (or for even more if you need later on), so you can implement the common logic there.

The facade could have a createBottomSheetDialogFragment and a createDialogFragment method, and both calling an inner method for the common logic.

Or you could implement the logic which decides which dialog fits for the current situation right inside your own class, and decide which dialog you need.

Comments