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?

  • What is the intended usage? What should happen when SetIcon or SetText are called multiple times? – Euphoric Nov 30 '15 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(); – Rulasmur Nov 30 '15 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/… – COME FROM Nov 30 '15 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? – Rulasmur Nov 30 '15 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. – COME FROM Nov 30 '15 at 12:56
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.

  • @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. – Rulasmur Nov 30 '15 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.

  • Well, I didn't really know what else to call it. Anyway, is anything inherently wrong with the design? – Rulasmur Nov 30 '15 at 9:30
  • @Rulasmur What design? You are just setting properties using fluent syntax. This is more stylistic choice than design choice. – Euphoric Nov 30 '15 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.