https://kotlinlang.org logo
#android
Title
# android
g

Geert

04/12/2019, 8:25 AM
I was trying out the Factory pattern. What do you guys recommend? Using a constructor or factory pattern? (I know the use of isXavailable and isXabsent is not smart, but copied something from iOS sourceCode, still need to fix this)
g

gildor

04/12/2019, 8:26 AM
Constructor, I don’t see why you need this create method, it’s perfectly work to pass model in primary constructor
g

Geert

04/12/2019, 8:28 AM
Not in primary, that one needs to be an empty constructor. I use it in the 2nd constructor
Well thanks, found this solution 😉
r

ribesg

04/12/2019, 8:38 AM
Depending on why you needed an empty constructor, this may not work. Actually, I’m not sure.
If you have some reflection looking for a constructor without arguments, would it find something if you only have a constructor with default values for all parameters?
g

Geert

04/12/2019, 8:44 AM
Thanks for mentioning, I tried val viewModel = MedicationAgreementRequesterViewModel::class.java.newInstance() and this just works 😉
Seems like I don’t even need the init
p

Paul Woitaschek

04/12/2019, 9:20 AM
ViewModelView, I like that!
😂 1
g

Geert

04/12/2019, 9:21 AM
ViewModelView :
Copy code
abstract class ViewModelView : ViewModel(), LayoutResIdProvider
🤔 1
LayoutReIdProvider:
Copy code
interface LayoutResIdProvider {
    var layoutResId: Int
}
Used in a RecyclerView in combination with DataBinding 😉
Damn, I still need a init, because I have this updateForChanged() method which accesses the properties. Guess it’s time to learn about delegates, I understand them, but don’t know when or how to use them exactly
r

rkeazor

04/12/2019, 2:38 PM
@Geert this seems weird. ViewModelView, has a layoutResProvider, which seems like a anti pattern. The layout shouldn't be coupled to a ViewModel at all...Even with databinding. For one or more constructors, you will need to create a inner class that inherits from ViewModelProvider.Factory and override the create(modelClass: Class<T>). if not Viewmodel won't work as intended.
g

Geert

04/12/2019, 2:39 PM
Well I use the ViewModel inside a recyclerview and and use the layoutResId as viewType to create a viewholder. So it saves me a lot of work
r

rkeazor

04/12/2019, 2:40 PM
Yea its a anti pattern. instead ,specify the layout res in your recyclerview
It may work but its a bad practice, and also leads to things not being flexible
g

Geert

04/12/2019, 2:42 PM
Hmm, I thought it was just flexible 😅
Are you sure? I really liked this simple Adapter…
r

rkeazor

04/12/2019, 2:44 PM
Nah, not at all. .. if you put specify the layout res in the viewmodel, that means the viewmodel should only work with that layout.
💯 1
g

Geert

04/12/2019, 2:45 PM
I dont see that as a problem. I just want to bind a specific viewmodel to a specific view
r

rkeazor

04/12/2019, 2:46 PM
lol whats the point of putting list[position].layoutResId ,if its always going to be the same Layout Re
s
why not just put the LayoutRes itself
g

Geert

04/12/2019, 2:47 PM
No, I can add a generic list into this adapter
Each viewmodel has its own layout, and I made a factory pattern to bind a object to a layout.
r

rkeazor

04/12/2019, 2:57 PM
Oh ok... But at this point ,your factory method isn't using the Viewmodel provider.factory, so is Extending from Viewmodel even useful at this point?
why not just extend from LayoutResIdProvider directly
r

rook

04/12/2019, 6:11 PM
Just out of curiosity, doesn’t this pattern break if you want multiple types in a list?
r

rkeazor

04/13/2019, 12:16 PM
@rook judging by the actual implementation, each type would need its on ViewModelView.
5 Views