我试图学习MVC模式,但每个地方都说一些不同的话。所以现在我不知道真正的MVC是什么。
所以我猜它最纯净的MVC:
- Model is just data and notify data changes.
- View reads the messages of the Model to update the view.
- Controller reads the user input from View and changes the Model according.
<强 > 执行 强>
- Model knows no one.
- View knows the Model.
- Controller knows both View and Model.
替代代码 :
/* Model */
class Color{
color = blue;
setColor(color);
notifyUpdate();
}
/* View */
class ColorPicker(model){
model.register(update);
update(){
this.colorToExhibit = model.color;
}
}
/* Controller */
class Colorize(view, model){
view.register(update);
update(color){
model.setColor(color);
}
}
一些问题:
- Is that right?
- I can t see why the View cannot change the Model directly, but through Controller.
- Suppose I have animations to be performed after an action. Who must handle this animation: the Model, the View, or the Controller? Also: the animation logic is part of the Model, View, or Controller? More: Suppose a Poker game. After the user choose an action (say, Raise ), the system must play an animation (say, the chips going from player spot to the desk). How can I see this poker example (with animation) as a MVC? Can you explain and give a pseudocode about that?
谢谢