[英]MVC - Model-View communication
First of all I want to say that I searched a lot, but I found a lot of different solutions on the internet that contradict each other.首先我想说我搜索了很多,但是我在互联网上发现了很多相互矛盾的不同解决方案。
To simplify my question, I'll use an example.为了简化我的问题,我将使用一个示例。 Let's say I want to generate a map for a 2D game using the MVC design pattern:
假设我想使用 MVC 设计模式为 2D 游戏生成 map:
MapView.java MapView.java
public class MapView extends TheViewComponent {
public MapView() {
// Create the view
}
}
MapController.java MapController.java
public class MapController {
// …
public MapController(Map map, MapView mapView) {
this.map = map;
this.mapView = mapView;
}
}
Map.java Map.java
public class Map {
public void generate() {
// Should create tiles in my MapView.
}
}
Main.java主.java
public class Main {
public Main() {
Map map = new Map();
MapView mapView = new MapView();
MapController mapController = new MapController(map, mapView);
}
}
In the generate()
method (Map.java), I don't understand how I can to update the view.在
generate()
方法(Map.java)中,我不明白如何更新视图。 In my Map class I don't have the reference to the controller or the view.在我的 Map class 中,我没有提到 controller 或视图。
new Map(mapView)
?new Map(mapView)
将视图传递给 model 吗? (seems weird…) Map.java
, and let's say for the example that I have operations to do on Tile and TileView (like my map).Map.java
中有一个瓷砖列表,并且假设我在 Tile 和 TileView 上有操作要做(比如我的地图)。 I want to store an ArrayList<Tile>
and not an ArrayList<TileController>
… So this second solution seems weird too…ArrayList<Tile>
而不是ArrayList<TileController>
......所以第二个解决方案似乎也很奇怪......The domain model Map
should only hold the structure of the map and should make it accessible by the outer world (eg getTiles()
).域 model
Map
应仅包含 map 的结构,并且应使其可供外部世界访问(例如getTiles()
)。 Then, the view should use that domain data to render a map accordingly.然后,视图应使用该域数据相应地呈现 map。
For manipulation, the controller should provide entry points for the relevent actions and dispatch them to a service layer that in turn does update the domain model.对于操作,controller 应该为相关操作提供入口点并将它们分派到服务层,该服务层反过来会更新域 model。
MVC is an architectural pattern with the goal to separate responsibilities MVC 是一种旨在分离职责的架构模式
Model Model
This is your business layer (domain and logic) and should not have any say on how you are going to "view" the data.这是您的业务层(域和逻辑),不应该对您将如何“查看”数据有任何发言权。 For example the Model provides you an object/list/map with the information to be displayed.
例如,Model 为您提供了一个包含要显示的信息的对象/列表/地图。
Tip: here your code should not rely on web frameworks or HTTP Servlet API提示:这里您的代码不应依赖于 web 框架或 HTTP Servlet API
View看法
This is the layer responsible to render the data in the User Interface: it should not include business rules or security, but only presentation logic这是负责在用户界面中呈现数据的层:它不应包含业务规则或安全性,而应仅包含表示逻辑
Controller Controller
It sits in the middle: it dispatches the request to the relevant Model and forward the outcome to the appropriate View它位于中间:它将请求分派给相关的 Model 并将结果转发给相应的视图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.