rbtLong
rbtLong

Reputation: 1582

Can a ViewModel have multiple Models?

I am learning the MVVM design pattern and I am writing a random engine that generates random numbers to multiple Views. One of my view starts and stops the engine, another View takes the numbers and graphs them, another view displays the numbers--all of these view use the same ViewModel. But my last view wants to take the number and figure out whether it's prime or not and finds the occurrences of all the primes that are generated from the random engine.

For this, I have written a separate model for determining prime numbers. I am curious if I can utilize 2 models under one ViewModel. If not how do you suggest I resolve this? It doesn't make sense to me to modify the model because it is running perfectly fine and serves all of its other clients perfectly. Is there another way to do this without modifying the original model?

Upvotes: 2

Views: 1759

Answers (2)

Arushi Agrawal
Arushi Agrawal

Reputation: 629

One can make use of as many view models for association with a view as convenient. There is no problem with this approach.

Upvotes: 0

TGH
TGH

Reputation: 39278

I personally don't see a problem with a ViewModel consuming more than one model object. I have created several view models that consume composite model objects (hierarchical objects).

The way I see it, a view model drives the view. The model data is really just the data source used to compose the ViewModel.

Upvotes: 4

Related Questions