The concept is to provide a workflow which allows you to apply all the properties you want and then have that object "built", for example...
public class ButtonBuilder<Builder extends ButtonBuilder<Builder>> {
private JButton button;
public ButtonBuilder() {
button = new JButton();
}
public Builder withText(String text) {
button.setText(text);
return (Builder)this;
}
public Builder withIcon(Icon icon) {
button.setIcon(icon);
return (Builder)this;
}
public Builder borderPainted(boolean painted) {
button.setBorderPainted(painted);
return (Builder)this;
}
public Builder contentAreaFilled(boolean painted) {
button.setContentAreaFilled(painted);
return (Builder)this;
}
public Builder focusPainted(boolean painted) {
button.setFocusPainted(painted);
return (Builder)this;
}
public JButton build() {
return button;
}
}
nb: Note, a common pattern for a builder is store the properties in some kind of cache/lookup, which are then applied when you call build, but in this case, it's just easier to apply them directly to the button itself
nb: Obviously, I've only supplied a small subset of properties you might want to specify for a button, you'll need to add the rest ;)
And then you can build the button using something like...
But that's not what I asked, I don't want to have to have reapply the all the properties each time I want to create a button
Yes, I know, I was getting to it. Once you have a "base" builder, you could make one or more "custom" extensions, which could apply default values directly, for example...
public class MyCustomButtonBuilder extends ButtonBuilder<MyCustomButtonBuilder> {
public MyCustomButtonBuilder(String text) {
this(text, null);
}
public MyCustomButtonBuilder(Icon icon) {
this(null, icon);
}
public MyCustomButtonBuilder(String text, Icon icon) {
super();
withText(text)
.withIcon(icon)
.borderPainted(false)
.focusPainted(false)
.contentAreaFilled(false);
}
}
which could then be used something like...
JButton sadButton = new MyCustomButtonBuilder("Sad", new ImageIcon(ImageIO.read(getClass().getResource("/images/Sad.png"))))
.build();
发布评论
评论(1)
您可以...
使用“ nofollow noreferrer”>“建筑商”
该概念是提供一个工作流程,使您可以应用所需的所有属性,然后将该对象“构建”,例如...
nb:请注意,建造者的常见模式是将属性存储在某些中一种缓存/查找,然后在您调用构建时应用它们,但是在这种情况下,将它们直接应用于按钮本身
nb:显然,我只提供了一个小子集您可能需要为按钮指定的属性,您需要添加其余的;)
,然后您可以使用类似的内容来构建按钮
是的,我知道,我都必须重新应用所有属性。一旦拥有“基础”构建器,就可以制作一个或多个“自定义”扩展名,例如,可以直接应用默认值...
然后可以使用类似的东西...
您可以...
使用a < a href =“ https://www.javatpoint.com/factory-method-design-pattern#:%7e:text = a%20factory%20Pattern%20Pattern%20OR%20Factory,%20Instance%20Instance%20OF%20The%20CLASS。” ”
rel =“ nofollow noreferrer
”>“工厂模式 根据您的共同需求。
您可以...
结合两个概念,例如...
You could...
Use a "builder pattern"
The concept is to provide a workflow which allows you to apply all the properties you want and then have that object "built", for example...
nb: Note, a common pattern for a builder is store the properties in some kind of cache/lookup, which are then applied when you call build, but in this case, it's just easier to apply them directly to the button itself
nb: Obviously, I've only supplied a small subset of properties you might want to specify for a button, you'll need to add the rest ;)
And then you can build the button using something like...
Yes, I know, I was getting to it. Once you have a "base" builder, you could make one or more "custom" extensions, which could apply default values directly, for example...
which could then be used something like...
You could...
Use a "factory pattern", for example...
which could be used something like...
The factory pattern could allow you to supply a multiple number of different types of buttons, configured different based on your common needs.
You could...
Combine the two concepts, for example...