在 GUI 开发中使用构建器模式时,哪些例子会被认为是实现构建器模式的好例子?

发布于 2024-09-16 02:22:20 字数 339 浏览 3 评论 0原文

当谈到工厂类和方法、模式等的使用时,我是一个完全的新手 - 事实上,我是在浏览 Java 相关问题时在 Stackoverflow 上第一次了解到它们的:-)

回应之前的 有人建议我的问题我在 GUI 的开发中研究了 Builder 模式的使用,因此我正在寻找易于理解的示例,演示如何使用此模式和方法链接等将应用程序的用户界面组合在一起。

感谢您的阅读。

I am a complete newbie when it comes to the use of factory classes and methods, patterns, etc - in fact I first learned of them here on Stackoverflow when browsing Java related questions :-)

In response to a previous question of mine it was suggested that I look into the use of the Builder Pattern in the development of my GUI's and so I am seeking good easily understood examples demonstrating how an application's user interface could be put toghether using this pattern and method-chaining, etc.

Thanks for reading.

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

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

发布评论

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

评论(4

为人所爱 2024-09-23 02:22:20

可能还有其他(和更好的)示例,但这里是一个。

使用 GridBagConstraints 时,可以使用这个可怕的构造函数:

public GridBagConstraints(int gridx, int gridy,
                          int gridwidth, int gridheight,
                          double weightx, double weighty,
                          int anchor, int fill,
                          Insets insets, int ipadx, int ipady) 

但我认为它无法使用。人们最常最终使用空构造函数并设置各种公共属性来覆盖默认值。

作为替代方案,可以使用构建器,如下所示:

somePanel.add(
    getContent(),
    new ConstraintsBuilder()
        .gridLocation(1, 1)
        .gridSize(1, 1)
        .weight(0.0, 0.0)
        .anchor(NORTHWEST)
        .build() );

只是一个示例。

There are probably other (and better) examples but here is one.

When working with GridBagConstraints, one could use this horrible constructor:

public GridBagConstraints(int gridx, int gridy,
                          int gridwidth, int gridheight,
                          double weightx, double weighty,
                          int anchor, int fill,
                          Insets insets, int ipadx, int ipady) 

But I consider it unusable. And people most often end up using the empty constructor and setting the various public attributes to override the defaults values.

As an alternative, one could use a builder, something like this:

somePanel.add(
    getContent(),
    new ConstraintsBuilder()
        .gridLocation(1, 1)
        .gridSize(1, 1)
        .weight(0.0, 0.0)
        .anchor(NORTHWEST)
        .build() );

Just an example.

烂柯人 2024-09-23 02:22:20

Joshua Bloch 的第 2 项:考虑构建器 是始终是一个好的起点。关于GUI开发,许多布局管理器使用构建器模式。 布局管理器可视化指南 是一个很好的介绍。

Joshua Bloch's Item 2: Consider a builder is always a good place to start. Regarding GUI development, many layout managers use the builder pattern. A Visual Guide to Layout Managers is a good introduction.

断肠人 2024-09-23 02:22:20

我认为“Source Making”在介绍设计模式(以及 UML、反模式和重构)方面做得很好。您可能想查看该网站。

您可以在此处阅读有关 Builder 的信息:源码制作:Builder 设计模式

I think "Source Making" does a nice job of introducing design patterns (as well as UML, Antipatterns and Refactoring). You may want to check the site out.

You can read about the Builder here: Source Making: Builder Design Pattern

紧拥背影 2024-09-23 02:22:20

这是与构建 UI 相关的 BuilderPattern 示例。 (这里没有解释,但是如果你了解Builder Pattern就很容易理解)

http ://www.java2s.com/Code/Java/Design-Pattern/BuilderPatternExample.htm

构建器模式更多信息:

http://www.allapplabs.com/java_design_patterns/builder_pattern.htm

http://www.java2s.com/Code/Java/Design-Pattern/BuilderPatterninJava.htm

Here is good BuilderPattern example related to building UI. (There is not explanation but easy to understand if you know Builder Pattern)

http://www.java2s.com/Code/Java/Design-Pattern/BuilderPatternExample.htm

Builder Pattern more information :

http://www.allapplabs.com/java_design_patterns/builder_pattern.htm

http://www.java2s.com/Code/Java/Design-Pattern/BuilderPatterninJava.htm

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