进度条 + Java 中的 MVC =?

发布于 2024-07-22 15:15:20 字数 1284 浏览 8 评论 0原文

因此,我在 Java Swing 中拥有了这个漂亮的 MVC 架构应用程序,现在我想添加一个进度条,但我对将 JProgressBar 合并到我的视图中的良好设计方法感到困惑。 我应该:

  • 将 DefaultBoundedRangeModel 添加到我的控制器状态并将其导出吗?

    类模型{ 
        最终私有 DefaultBoundedRangeModel 进度 
          = 新的 DefaultBoundedRangeModel(); 
    
        公共无效 getProgressModel() { 返回进度;   } 
        公共无效setProgressCount(int i){progress.setValue(i);   } 
      } 
    
      类控制器{ 
        模型模型; 
        int 进度计数; 
        无效 doSomething() 
        { 
           model.setProgressCount(++progressCount); 
        } 
      } 
    
      类视图{ 
        无效设置(型号m) 
        { 
          JProgressBar ProgressBar = /* 获取或创建进度条 */ ; 
          ProgressBar.setModel(m.getProgressModel()); 
        } 
      } 
    
      /* 困境:模型允许在技术上导出进度 
       所有的进度状态都可以由其他人设置;   应该把它放在 
       进入只读包装器?   */ 
      
  • 使用 JGoodies Binding 尝试将 JProgressBar 的视觉状态连接到我的模型的状态?

    类模型{ 
        私人国际进展; 
    
        公共无效 getProgressCount() { 返回进度;   } 
        公共无效 setProgressCount(int i) { 进度 = i;   } 
      } 
    
      类视图{ 
        无效设置(型号m) 
        { 
          ProgressBar ProgressBar = /* 获取或创建进度条 */ ; 
          CallSomeMagicMethodToConnect(m, "进度计数", 进度条, "值"); 
          // 有没有像上面那样工作的东西? 
          // 如何让它自动更新??? 
        } 
      } 
      
  • 还是其他什么???

编辑:更具体地说:有人能给我指出一个Java应用程序的实际源代码的好例子吗?该应用程序有一个包含进度条的状态栏,并且有一个不错的MVC实现?

So I have this nice spiffy MVC-architected application in Java Swing, and now I want to add a progress bar, and I'm confused about Good Design Methods to incorporate a JProgressBar into my view. Should I:

  • add a DefaultBoundedRangeModel to my controller's state, and export it?

    class Model {
      final private DefaultBoundedRangeModel progress
        = new DefaultBoundedRangeModel();
    
      public void getProgressModel() { return progress; }
      public void setProgressCount(int i) { progress.setValue(i); }
    }
    
    class Controller {
      Model model;
      int progressCount;
      void doSomething()
      {
         model.setProgressCount(++progressCount);
      }
    }
    
    class View {
      void setup(Model m)
      {
        JProgressBar progressBar = /* get or create progress bar */  ;
        progressBar.setModel(m.getProgressModel());
      }
    }
    
    /* dilemma: Model allows progress to be exported so technically
     all of the progress state could be set by someone else; should it be put
     into a read-only wrapper? */
    
  • use JGoodies Binding to try to connect the JProgressBar's visual state to my model's state?

    class Model {
      private int progress;
    
      public void getProgressCount() { return progress; }
      public void setProgressCount(int i) { progress = i; }
    }
    
    class View {
      void setup(Model m)
      {
        ProgressBar progressBar = /* get or create progress bar */  ;
        CallSomeMagicMethodToConnect(m, "progressCount", progressBar, "value");
        // is there something that works like the above?
        // how do I get it to automatically update???
      }
    }
    
  • or something else???

edit: more specifically: could someone point me to a Good Example of realistic source for an application in Java that has a status bar that includes a progress bar, and has a decent MVC implementation of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪之触 2024-07-29 15:15:20

不(对 1)和不(对 2)。 最起码,我是这么想的。

否(至 1):首先,DefaultBoundedRangeModel 是一个 javax.swing 类。 在我看来,这些类在模型中没有地位。 例如,考虑一下存在于服务器上并通过 RMI 访问的模型 - 突然将 javax.swing 类放在那里似乎“不正确”。
然而,真正的问题是您将模型的一部分(有界模型)提供给其他人,而无法控制触发的事件或发出的查询。

否(对2):呃。 绑定很有趣,但(至少在我看来)应该用于 UI 模型和 UI 组件之间的同步,而不是数据模型和 UI 模型之间的同步。 再次考虑一下,如果您的数据模型位于通过 RMI 访问的远程服务器上,会发生什么情况。

所以呢? 好吧,这只是一个建议,但我会添加一个事件侦听器接口并添加标准事件侦听器订阅方法(addListner(...)、removeListener(...))。 当我有更新时,我会从模型中调用这些侦听器。 当然,我会确保记录调用线程(或者说无法确定),以便客户端(在本例中为 UI)能够正确同步(invokeLater 和朋友)。 由于监听器服务将由控制器公开,这将允许模型存在于任何地方(甚至允许远程调用或池化监听器)。 此外,这会将模型与 UI 分离,从而可以构建更多包含它的模型(翻译器/装饰器/依赖模型)。

希望这可以帮助。

No (to 1) and NOOOO (to 2). At least in my opinion.

No (to 1): First, DefaultBoundedRangeModel is a javax.swing class. In my opinion, these classes have no place in models. For example, think about the model living on the server, being accessed via RMI - All of the sudden putting a javax.swing class there seems "not right".
However, the real problem is that you're giving a part of your model (the bounded model) to someone else, with no control over events fired or queries made.

No (to 2): Ugh. Binding is fun but (at least in my opinion) should be used to synchronize between UI model and UI components, not between data model and UI model. Again, think what would happen if your data model lived on a remote server, accessed by RMI.

So what? Well, this is only a suggestion, but I'd add an event listener interface and add the standard event listener subscription methods (addListner(...), removeListener(...)). I'd call these listeners from within my model when I have updates going on. Of course, I'd make sure to document the calling thread (or say it cannot be determined) in order for the client (the UI in this case) to be able to synchronize correctly (invokeLater and friends). Since the listener service will be exposed by the controller, this will allow the model to live anywhere (even allowing for listeners to be remotely invoked or pooled). Also, this would decouple the model from the UI, making it possible to build more models containing it (translators / decorators / depending models).

Hope this helps.

满栀 2024-07-29 15:15:20

在我们的应用程序(MVC,大约 100 KLOC)中,我们有这样的(实际上是模式观察者):

/**
 * Observer on progress changes
 */
public interface IProgressListener {
  public void setProgress(ProgressEvent e);
}

public class ProgressEvent extends ... {
  private int progressCount;
  // setter + getter
  ...
}

class Model {
  public void addProgressListener(IProgressListener l);
  protected void fireProgressChange(ProgressEvent e);   // call .setProgress() on listeners
}

class Controller {
  private Model model;
}

class View extends ProgressBar implements IProgressListener {
  ...
  // IProgressListener implementation
  public void setProgress(ProgressEvent e) {
    this.setValue(e.getProgress());
  }
  ...
}

In our app (MVC, about 100 KLOC) we have it like that (pattern Observer, actually):

/**
 * Observer on progress changes
 */
public interface IProgressListener {
  public void setProgress(ProgressEvent e);
}

public class ProgressEvent extends ... {
  private int progressCount;
  // setter + getter
  ...
}

class Model {
  public void addProgressListener(IProgressListener l);
  protected void fireProgressChange(ProgressEvent e);   // call .setProgress() on listeners
}

class Controller {
  private Model model;
}

class View extends ProgressBar implements IProgressListener {
  ...
  // IProgressListener implementation
  public void setProgress(ProgressEvent e) {
    this.setValue(e.getProgress());
  }
  ...
}
泪痕残 2024-07-29 15:15:20

我想说的是,还有别的事。

我在使用 MVC 时遇到的问题是定义模型的抽象级别。

  • 模型可以是 UI 组件的某种对象

  • 模型也可以是程序本身的某种其他类型的对象。

模型

  • 可以与商业模型一样高。

在这种情况下,我将为进度条分离模型/组件对,并在单独的控制器类中处理它们。

这篇文章描述了 Swing 架构和可能会阐明它内部使用模型的方式。

I would say, something else.

The problem I have had with MVC, is to define the level of abstraction of the model.

  • Model could be some sort of objects for the UI components

  • Model could also be some other sort of objects for the program it self.

and

  • Model could be as high as business models.

In this case I would have separated model/component pairs for the progress bar and handle them in a separate controller class.

This article describes swing architecture and might clarify the way it uses models inside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文