Swing中的java创作设计模式

发布于 2024-10-06 13:38:37 字数 825 浏览 0 评论 0原文

我有一系列这种形式的静态实用程序方法:

StringBuffer Util.doA(String arg0, String arg1, SomeEnum e);  
StringBuffer Util.doB(String arg0, String arg1);  
StringBuffer Util.doC(String arg0, String arg1, String arg2,String arg3);  
StringBuffer Util.doD(String arg0, String arg1, String arg2,String arg3, AnotherEnum e);  
etc

我想显示一个动态创建的 JDialog,例如通过构建器或其他模式,一旦按下按钮,我希望根据对话框的类型调用适当的实用程序方法。
例如,如果 JDialog 的类型为 A,则按钮的动作监听器应调用 Util.doA,从输入字段作为参数传递数据,如果 JDialog 的类型为 B /code> 我想要按钮的动作监听器调用 Util.doB
A 型、B 型等是我的自定义类型。不强制使用。我只是让它们来区分每个 JDialog。
A 类型的 JDialog 与 B 类型的 JDialog 在某些输入字段上有所不同。但整体外观很相似。
总共有大约20种类型和20种对应的util方法。
我遇到的这个问题有标准模式吗?
我开始扩展 JDialog,以便每个自定义 JDialog 正确创建自己,并且 actionlistener 的方法调用适当的实用程序方法,但最终我得到了 20 个新类。
所以我想是否有更好的方法来解决这个问题。 有什么想法吗?

谢谢

I have a series of static utility methods of this form:

StringBuffer Util.doA(String arg0, String arg1, SomeEnum e);  
StringBuffer Util.doB(String arg0, String arg1);  
StringBuffer Util.doC(String arg0, String arg1, String arg2,String arg3);  
StringBuffer Util.doD(String arg0, String arg1, String arg2,String arg3, AnotherEnum e);  
etc

I want to display a JDialog that is created dynamically e.g. via builder or other pattern and once a button is pressed I want the appropriate utility method to be called depending on the type of dialog.
E.g. If JDialog is of type A the actionlistener of button should call the Util.doA passing as arguments data from the input fields and if JDialog is of type B I want the actionlistener of button to call Util.doB etc.
Types A, B etc are my custom types. Are not mandatory to be used. I just have them to distinguish between each JDialog.
JDialog of type A differs from JDialog of type B in some of the input fields. But the overall appearance is similar.
Totally there are about 20 types and 20 corresponding util methods.
Is there a standard pattern to this problem I have?
I started extending JDialog so that each custom JDialog creates itself properly and the method of actionlistener calls the appropriate utility method, but I end up with 20 new classes.
So I thought if there is a better approach to this.
Any ideas?

Thank you

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

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

发布评论

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

评论(1

娜些时光,永不杰束 2024-10-13 13:38:37

将相应的util方法放入20种类型中。如果类型之间存在任何重复,您可以将其移至超类。

至于你的问题,我认为这是针对你的情况的最佳方法。如果对话框和实用程序代码都在一个类中,仅此而已,那么该类的用途就很清楚了。这也意味着,如果您需要删除、更改、添加新类型,您只需删除一个类、修改一个类或创建一个新类。将所有相关代码放在一起可以防止“霰弹枪手术”。

Put the corresponding util method inside the 20 types. If there is any duplication between the types you might be able to move it to a super class.

As to your question, I think this is the optimal approach in your case. If you have the dialog and util code all in one class and nothing more, then it is clear what that class's purpose is. It also means that if you need to remove, change, add a new type you are only having to delete one class, modify one class, or create one new class. Keeping all the related code together prevent's "Shotgun surgery".

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