I was trying out the Factory pattern. What do you ...
# android
g
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
Constructor, I don’t see why you need this create method, it’s perfectly work to pass model in primary constructor
g
Not in primary, that one needs to be an empty constructor. I use it in the 2nd constructor
Well thanks, found this solution 😉
r
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
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
ViewModelView, I like that!
😂 1
g
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
@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
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
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
Hmm, I thought it was just flexible 😅
Are you sure? I really liked this simple Adapter…
r
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
I dont see that as a problem. I just want to bind a specific viewmodel to a specific view
r
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
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
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
Just out of curiosity, doesn’t this pattern break if you want multiple types in a list?
r
@rook judging by the actual implementation, each type would need its on ViewModelView.