用于分离代码和GUI的设计 [英] Design for separating code and GUI

查看:125
本文介绍了用于分离代码和GUI的设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个播放列表,我想通过使用两个类来分隔代码/逻辑和GUI:

I need to create a Playlist, I want to separate code/logic and GUI by using two classes:


  • 播放列表(代码/逻辑)

  • PlaylistGui (GUI)

  • Playlist (code/logic)
  • PlaylistGui (GUI)

应该可以单独使用播放列表类,例如在某种命令行环境中。现在的问题是,在使用GUI时,如何将两个类一起插入?我的想法一直到现在:

It shall be possible to use the Playlist class standalone, e.g. in some kind of command line environment. The question now is, how would one plug both classes together, when using a GUI? My ideas until now:


  • 播放列表类中公开一个可观察列表,创建 PlaylistGui 上的方法 setItemSource(播放列表来源)

  • 不确定如果可能在Java中,只需从.NET知道:让播放列表类触发事件并让 PlaylistGui 捕获它们,应该导致解耦代码? : - )

  • Expose an observable list from the Playlist class, create a method setItemSource(Playlist source) on the PlaylistGui class
  • Not sure if possible in Java, just know this from .NET: Let Playlist class fire events and let PlaylistGui catch them, should lead to uncoupled code? :-)

打开新的想法:-)注意我使用的是Java 7和JavaFX,不过我猜JavaFX并没有限制可能性,只是扩展它们。

Open for new ideas :-) Note I am using Java 7 and JavaFX, though I guess JavaFX doesn't limit possiblities, just extends them.

推荐答案

我认为你的基本概念是健全的,责任归属。

I think your basic concept is sound, delegation of responsibility.

我会创建一个模型的接口,只展示你认为任何基本控制器/查看器想要的那些方法。这意味着您可以更改实现而不影响任何依赖它的组件。

I would create an interface of the model, exposing only those methods you think that any basic controller/viewer would want. This means you can change the implementation without effecting any of the components that rely on it.

监听器本质上只是一个回调机制,感兴趣的各方自己注册(通过一个公共接口)在他们感兴趣的事情发生时得到通知。

Listeners are, essentially, just a call back mechanism, where interested parties register themselves (through a common interface) to be notified when something occurs that they are interested.

看看编写事件监听器以获取更多信息。

我将基本开始一个公共库,它定义了各方需要知道的基本接口(例如 PlayList 和监听器)。

I would basic start out with a common library which defines the basic interfaces that all parties would need to know out (such as the PlayList and listeners).

这将允许根据您的需要设计播放列表的'n'实现

This would allow to design 'n' implementations of the playlist based on your needs

这篇关于用于分离代码和GUI的设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆