dbulgakov dbulgakov - 1 month ago 5
Android Question

Base class for Dialogs in Android

I'm trying to implement two types of

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

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

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?


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.