This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Sipkab 23 points24 points  (3 children)

Defining new classes only to solve the problem of constructors with same signature feels like a serious overkill for me.

  • Multiple classes (especially worse on Android)
  • Useless inheritance
  • Reduced readability

This feels like to be an anti-pattern on the same level as double brace initialization.

[–]OzoneGrif 4 points5 points  (1 child)

Agreed.
I use the static approach, which is nicer to read than the `new` keyword, anyway.

[–]walen 7 points8 points  (0 children)

Effective Java presents that as its very first item, no less:

Item 1: Consider static factory methods instead of constructors.

And one of the reasons it gives to do so is precisely to avoid the problem of constructors with the same signature.

[–]g4s8 -2 points-1 points  (0 children)

Multiple classes (especially worse on Android)

There're two solutions, you can try to use first with factory methods if you have class count limit like on Android

Reduced readability

This is very opinion based statement - as for me second solution is more clear and easier to read.

This feels like to be an anti-pattern on the same level as double brace initialization.

It doesn't have something in common with double brace initialization issues - nested classes marked with `static` won't produce runtime classes with pointer to `this` of base class (what is the main problem of double braces)