Static View Models / Presentation Models
Would it be incorrect to make all my View 开发者_C百科Models / Presentation Models static classes so that if any other View Presenter wanted to change a view model other than its own it could easily aquire a reference to it?
If this is the wrong approach how would you achieve it?
If you want to make it static you'd be better off implementing a proper Singleton pattern. You will find it extremely difficult to test all the classes that consume your static models as you won't be able to use any Dependency Inversion techniques.
Also, be aware of concurrency if you only have 1 class - you'll need to lock pretty much everything. Not good.
Finally, you might want to consider using a Factory pattern. Easily accessible but will create a new model object for each class that wants to use it. Concurrency issue (mostly) solved.
Static ViewModels sounds like an awful idea (I consider static evil as a general principle). This would mean that you can't have more than one instance of a ViewModel. I can think of many examples of UI where there are several instances of the same View type, but that wouldn't be possible with static ViewModels.
If you want to enable communication across Views, Publish/Subscribe (events) are a much better option.
Remember that when we talk about ViewModels/Presentaion Models they usually expose underlying Domain Objects. If you have sevaral Views that display parts of the same Domain Object, you can have the Domain Object raise events when it changes state, and any ViewModel that display data from that Domain Object can subscribe to those events and update their controls accordingly.
精彩评论