0
public class MyButton{
 public MyButton(){}
 public MyButton setIcon(Icon icon){return this;}
 public MyButton setText(String text){return this;}
}

And used like:

MyButton testButton = new MyButton()
 .setIcon(MyIcon)
 .setText("MyText");

I know its not a true implementation of the builder pattern, but is there something inherently wrong with this design?

asked Nov 30, 2015 at 8:43
7
  • What is the intended usage? What should happen when SetIcon or SetText are called multiple times? Commented Nov 30, 2015 at 9:18
  • Calling setIcon/setText would just override whatever you set it to before. Basically it works like a normal buttons code ie: MyButton test = new MyButton(); test.setIcon(); test.setText(); Commented Nov 30, 2015 at 9:21
  • 5
    That's not an implementation of the builder pattern, but an example of fluent setters. See this question: stackoverflow.com/questions/1345001/… Commented Nov 30, 2015 at 9:42
  • @COMEFROM could you please make that an answer, as that was exactly what I was looking for. The answers in that thread answer my question. Or could someone flag it as a duplicate of the link above? Commented Nov 30, 2015 at 10:00
  • I can't flag this question as a duplicate since the other question is from another site. I'm also a little hesitant to write an answer since I don't really have any experience of using that pattern. I've used the foo.withSomething(bar) to make a clear difference between traditional setters and fluent methods but that's really the only thing I have to say about the issue. Commented Nov 30, 2015 at 12:56

3 Answers 3

3

Is it the builder pattern? As Euphoric explained, no, it is not. A Builder is a separate class which has the purpose to create an instance of a different class.

Is there anything wrong with the design? That depends. Does your application allow buttons which don't have a text and/or icon? If yes, then this pattern is perfectly acceptable. If no, you might want to consider writing a ButtonBuilder class which throws an exception (or at least an assert) when you call buttonBuilder.build() before setting all required properties.

answered Nov 30, 2015 at 12:05
1
  • @COMEFROM answered the question perfectly in the comments below the question. I called it a builder pattern for lack of a better name, however calling it a fluent interface would have been much more appropriate. Commented Nov 30, 2015 at 12:10
2

The core part of Builder patter is the build part, not the fluent syntax part.

If your class is not building another class, then it shouldn't be called builder.

In your case, I would call it "fluent property setting" or something similar.

answered Nov 30, 2015 at 9:24
2
  • Well, I didn't really know what else to call it. Anyway, is anything inherently wrong with the design? Commented Nov 30, 2015 at 9:30
  • @Rulasmur What design? You are just setting properties using fluent syntax. This is more stylistic choice than design choice. Commented Nov 30, 2015 at 9:40
1

As mentioned by @COMEFROM this question answered the question perfectly. The style used here is called a Fluent Interface. This answer mentions a convention which is inline with standard java convention and doesn't theoretically interfere with jvm optimizations and javabean compatibility

answered Nov 30, 2015 at 12:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.