在 MVC 或 MVP 应用程序中创建视图和控制器有哪些模式?

发布于 2024-07-14 08:34:39 字数 315 浏览 3 评论 0原文

我正在开发用于编辑文档的 MVC/MVP GUI。 该文档具有树结构,其中一些节点代表文本,其他节点代表图像。 应用程序模型还包括一个命令堆栈,命令直接在模型上运行。

由于不同的节点具有完全不同的控制,因此我计划为每个节点实现单独的 MVC/MVP 三元组。 我遇到麻烦的是如何同步对等 V 和 C 组件的创建和销毁。

我的想法:
- 侦听每个节点上的“childAdded”类型事件,然后根据这些事件创建对等点
- 使用工厂在模型中创建节点,并让该工厂也创建对等点

动态视图/控制器生成的一些常见模式或最佳实践是什么?

I'm working on a MVC/MVP GUI for editing a document. The document has a tree structure, with some nodes representing text, others images. The app model also includes a command stack, with commands operating directly on the model.

Since different nodes have radically different controls, I'm planning on implementing individual MVC/MVP triads for each node. Where I'm running into trouble is how to synchronize creation and destruction of the peer V and C components.

My ideas:
- listen on each node for "childAdded" type events, and then create peers based on those events
- use a factory for creating nodes in the model, and have that factory also create the peers

What are some common patterns or best practices for dynamic view/controller generation?

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

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

发布评论

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

评论(1

哀由 2024-07-21 08:34:39

我建议在这个 网站 上查看一些 UI 模式

至于您的具体问题,我会这样做以下

显示节点的表单将实现 INodeView 接口

INodeTreeForm 接口的一种方法是能够添加单个节点。 它将传递在 NodeTreeScreen 对象中创建的 Node 类。 将有两个关键属性。 第一个是节点的键,第二个是节点的类型。

您可以以这样的方式添加节点,即控件上有一个具有键的字段。

除了 INodeTreeForm 接口之外,您还会有一个 INodeView 接口。 这将覆盖视图区域或您也将切换的新表单。

当您单击节点时,它会将键传递给 NodeTreeScreen,后者会查看它是什么类型的节点。 它有一个 INodeView 对象的集合。 INodeView 的属性之一是它是哪种类型的节点。

您检索正确的 INodeView。 您关闭当前的 INodeview,将当前的 INodeView 设置为您检索到的 INodeView,将节点传递给它,并告诉它显示自身。

您可以选择 INodeView 将要执行的操作。 您可以为每种不同类型注册一个表单/控件。 例如,INodeViewText、INodeViewImage 等。或者只是一个综合 INodeViewForm,让实现 INodeView 的对象负责实际绘图。

根据您的 GUI 工具包,这可能意味着一种表单可以实现许多不同的界面。

至于添加、删除和创建节点,这将通过实现 INodeTreeForm 和 NodeTreeScreen 的表单之间的交互来完成。 这将要做的事情之一是执行您已经创建的用于修改模型的命令。

通过实现接口背后的所有内容,您可以更改实现,而不会搞砸软件的其余部分。 实现不同接口的对象可以更改,而不会影响其他对象,只要它们继续正确地实现接口即可。

这是 Martin Fowler 网站上被动视图的一个变体。

I recommend looking at some of the UI Patterns at this site

As for your specific question I would do the following

The form displaying the nodes would implement a INodeView Interface

One Method of the INodeTreeForm interface would be the ability to add a individual node. It would be passing a Node class created in the NodeTreeScreen object. There will be two key property. The first is the key of the node, and the second is the type of node.

You would add the node in such a way that there is a field on the control that has the key.

Along with INodeTreeForm Interface you would have a INodeView interface. This would cover the view area or the new form you will be switching too.

When you click on the node it passes the key to NodeTreeScreen which tend looks at what type of node it is. It has a collection of INodeView objects. One of the properties of INodeView will be which type of node it is.

You retrieve the right INodeView. You close the current INodeview, set the current INodeView to the one you retrieved, pass it the node, and tell it do display itself.

You have a choice as to what INodeView will talk do. You can have a form/control registered for each of the different type. For example a INodeViewText, INodeViewImage, etc. Or just one omnibus INodeViewForm and let the object implementing INodeView take care of the actual drawing.

Depending on your GUI toolkit this could mean that one form could be implementing a lot of different interfaces.

As for adding, deleting, and creating nodes, this would be done through the interaction between the form implementing INodeTreeForm and NodeTreeScreen. Among the things this will be doing is executing the commands you already created to modify the model.

By implementing every thing behind an interface you can change the implementation without screwing up the rest of the software. The object implementing the different interfaces can change without impacting the other objects as long as they continue to implement the interfaces correctly.

This is a variant of Passive View on Martin Fowler's site.

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