Mvvm JavaFX模型视图视图模型我应该在哪里实现任务?

Mvvm JavaFX模型视图视图模型我应该在哪里实现任务?,mvvm,javafx,task,viewmodel,worker,Mvvm,Javafx,Task,Viewmodel,Worker,有几个关于这个主题的教程和示例,但是它们都是一种通用的构建,只在一个类中显示它是如何工作的 所以我的问题是,什么时候我想遵循MVVM模式,在这种模式下我必须实现我的所有任务 鉴于以下情况: 型号: class Model { /* When I place the Task here how can I deal with arguments and results from ViewController? */ public BufferedImage bigTask (Str

有几个关于这个主题的教程和示例,但是它们都是一种通用的构建,只在一个类中显示它是如何工作的

所以我的问题是,什么时候我想遵循MVVM模式,在这种模式下我必须实现我的所有任务

鉴于以下情况:

型号:

class Model {

   /* When I place the Task here how can I deal with arguments and results from ViewController? */

   public BufferedImage bigTask (String this, String and, Image that){
       // Some code to build a BufferedImage
   }
}
class ViewController {

     private BufferedImage myBufferedImage;

     @FXML
     private Button aButton;



     /*Should I implement my Task here? But how I get information about progress? */

     final Task<Integer> myTask = new Task<Integer>(){

          @Override
          protected Integer call() throws Exception{

               updateProgress( // How to get here? Is it the right place? )

               return null;
          }

     };



     @FXML
     void setOnAction(ActionEvent actionEvent){

          myBufferedImage = Model.bigTask("this", "that", new Image("path"));

     }
}
视图模型:

class Model {

   /* When I place the Task here how can I deal with arguments and results from ViewController? */

   public BufferedImage bigTask (String this, String and, Image that){
       // Some code to build a BufferedImage
   }
}
class ViewController {

     private BufferedImage myBufferedImage;

     @FXML
     private Button aButton;



     /*Should I implement my Task here? But how I get information about progress? */

     final Task<Integer> myTask = new Task<Integer>(){

          @Override
          protected Integer call() throws Exception{

               updateProgress( // How to get here? Is it the right place? )

               return null;
          }

     };



     @FXML
     void setOnAction(ActionEvent actionEvent){

          myBufferedImage = Model.bigTask("this", "that", new Image("path"));

     }
}
class视图控制器{
私有BuffereImage MyBuffereImage;
@FXML
私人按钮座;
/*我应该在这里执行我的任务吗?但是我如何获得有关进展的信息呢*/
最终任务myTask=新任务(){
@凌驾
受保护的整数调用()引发异常{
updateProgress(//如何到达这里?是在正确的地方吗?)
返回null;
}
};
@FXML
无效设置动作(动作事件动作事件){
MyBuffereImage=Model.bigTask(“这个”、“那个”、新映像(“路径”);
}
}
希望我能解释这个问题


提前谢谢

通常,您的任务应该在ViewModel中实现。 业务逻辑的实际实现应该在模型中完成,例如在服务类中。然后,ViewModel可以使用此服务并处理所有特定于ui的操作,如创建异步执行任务和更新进度值。但是,ViewModel可能不会直接更新ProgressIndicator,但ViewModel可以具有在ViewModel中更新的DoubleProperty“progress”。在ViewController/CodeBehind中,将实际ProgressIndicator绑定到ViewModel的此进度属性。这样,ViewModel独立于实际的UI控件,并且视图不包含任何业务逻辑

我觉得你的例子有点特别。通常我会说“BuffereImage”是一个特定于ui的类,它只属于视图,而不属于ViewModel或模型。然而,您的示例看起来像BuffereImage是业务操作的结果。在本例中,我将在ViewModel中创建一个
ObjectProperty
,并将加载图像的任务也放在ViewModel中。在ViewController中,我将向该属性添加一个侦听器,并在图像更改时将其放入ui。
这样,视图类与图像的加载方式无关。

通常,任务应该在ViewModel中实现。 业务逻辑的实际实现应该在模型中完成,例如在服务类中。然后,ViewModel可以使用此服务并处理所有特定于ui的操作,如创建异步执行任务和更新进度值。但是,ViewModel可能不会直接更新ProgressIndicator,但ViewModel可以具有在ViewModel中更新的DoubleProperty“progress”。在ViewController/CodeBehind中,将实际ProgressIndicator绑定到ViewModel的此进度属性。这样,ViewModel独立于实际的UI控件,并且视图不包含任何业务逻辑

我觉得你的例子有点特别。通常我会说“BuffereImage”是一个特定于ui的类,它只属于视图,而不属于ViewModel或模型。然而,您的示例看起来像BuffereImage是业务操作的结果。在本例中,我将在ViewModel中创建一个
ObjectProperty
,并将加载图像的任务也放在ViewModel中。在ViewController中,我将向该属性添加一个侦听器,并在图像更改时将其放入ui。
这样,视图类与图像的加载方式无关。

只是为了确保:该图像不是模型的一部分,而是基于模型数据的数据?只是为了确保:该图像不是模型的一部分,而是基于模型数据的数据?