Java程序使用插件类型结构,需要反馈正确的设计模式
很抱歉,如果这个问题之前已经被问过/回答过,但我不确定如何正确地表达它,因此我的搜索可能措辞不正确。
我正在用 Java 构建一个 Paint 应用程序作为作业。我决定使用“插件”类型的方法来做到这一点。主应用程序提供的功能很少,但进行动态类加载并注册不同的插件等。本质上,这允许我添加不同的组件,而不必将任何内容硬编码到主程序中:存在一个 LoadComponents() 方法,它可以查找子包中的所有不同插件并加载它们。这非常有效。
然后,每个组件负责创建其 UI 元素并将其注册到核心应用程序的 JFrame,以及实现其自己的 EventListener。到目前为止,所有这些都运行良好。
这是我的问题:我有一个用作 Canvas 的 JPanel,它显然实现了 PaintComponent() 方法以便进行实际的绘画。它包含在核心应用程序中,因此提供的功能非常少。随着不同组件的添加,我想向 Canvas 基类添加越来越多的功能,但我似乎找不到一个好的、干净的和有效的方法来做到这一点。
目前,我能想到的就是创建 Canvas 的子类来继承它的功能。然而,为了使用在运行时动态加载插件和功能的当前方法来做到这一点,我需要提供一个 setCanvas() 方法或类似的方法,该方法允许我覆盖实际 UI 中的基本 Canvas 类引用,该方法对我来说,这不是很有效,也不是常见的做法。
还有一个问题是,多个插件向 Canvas 基类添加功能,然后确定应该先执行哪个。
对此事的任何想法、建议或评论将不胜感激。
Sorry if this question has been asked/answered before, but I was unsure how to formulate it properly, and thus my searches may have been improperly worded.
I am building a Paint application in Java as an assignment. I have decided to do this using a "plugin" type approach. The main Application offers very little in terms of functionality, but does dynamic class loading and registers the different plugins and such. Essentially, this allows me to add different components, without having to hard-code anything into the main program: a LoadComponents() method exists which finds all the different plugins within a sub-package and loads them. This works perfectly.
Each component is then in charge of creating and registering it's UI elements with the core application's JFrame, as well as implementing its own EventListeners. All of this works beautifully so far.
Here's my issue: I have a JPanel that I use as a Canvas, which obviously implements the paintComponent() method in order to do the actual painting. This is contained within the core application, and thus offers very meager functionality. As different components get added, I would like to add more and more functionality to the base Canvas class, but I can't seem to find a good, clean and effective way to do so.
For the time being, all I can think of is to make subclasses of Canvas that inherit it's functionality. However, in order to do so using the current approach where plugins and functionality is dynamically loaded at runtime, I need to offer a setCanvas() method or such that allows me to override the base Canvas class reference in the actual UI, which, to me, isn't very effective nor common practice.
There's also the problem of multiple plugins adding functionality to the base Canvas class, and then figuring out which should go first.
Any ideas, suggestions or comments on the matter would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为“插件”一词要么不完全是您正在做的事情,要么它不是该应用程序的最佳设计路径。
我相信你想要的可能是一个命令模式,其中 UI 组件将“命令”发送到“画布”,“画布”绘制(并不断重新绘制)每个命令。因此,每个绘画 UI 项目都必须能够创建命令,但“画布”不必知道它来自哪里。
I think the term "Plug-in" is either not exactly what you are doing or it is not the best design path for this application.
I believe what you want to possibly a Command Pattern where UI components send "Commands" to the "Canvas" which draws (and keeps repainting) each command. So each painting UI item would have to be able to create commands but the "Canvas" does not have to know where it came from.