开发者

View holds an object from the Model in a MVC pattern, is this OK?

开发者 https://www.devze.com 2023-01-27 11:39 出处:网络
I have class that holds Tree Items for a Tr开发者_C百科ee.Say the constructor looks like this ViewTreeItem(List item) and can be called like this

I have class that holds Tree Items for a Tr开发者_C百科ee. Say the constructor looks like this ViewTreeItem(List item) and can be called like this

for( List l : model.getLists() )
    getTree().add( new ViewTreeItem(l) );

My controller ControllerTreeItem uses the List item reference to modify the list directly instead of calling to model to do the work.

Is this OK to change the model in this way? If not how might I do it in an alternative way?


the whole point of MVC is to make the design flexible. i think it would probably be better to have a method in the model which you can use to modify the list instead of modifying directly, as this would negate the whole point of using the MVC pattern.


assuming that your view is observing the model then:

1) it is normal for a view to keep a reference to the model.

2) you should change the model by calling methods in the model and update your view when your views onUpdate(Model model) is called.

0

精彩评论

暂无评论...
验证码 换一张
取 消