4 ms·
Wouldn't the Builder pattern address this issue? So you can do: object.setName("Alfred E. Neumann") .setLink("http://blog.schauderhaft.de http://blog.schaude
by cpt1138 14y ago
Wouldn't the Builder pattern address this issue? So you can do:
object.setName("Alfred E. Neumann")
.setLink("http://blog.schauderhaft.de http://blog.schauderhaft.de)
.setUltimateAnswer(42)
.setTempFile("c:\\temp\\x.txt")
.setZip(23);
- Aykroyd 14y agoThat would definitely be a nicer way to do it. Although, using the builder pattern to create parameters for every single method in your API would be a gigantic pain. Javascript APIs hack this in by taking Objects as params so that you can do {name='Alfred E. Neuman'...}. Unfortunately, Java's syntax isn't nice for creating Maps either, but maybe you could do something with that. In the end, this seems like a problem with the language syntax and any fix other than one at the language level is going to be a hack.
- moondowner 14y agoor ObjectName.getBuilder("Alfred E. Neumann", "http://blog.schauderhaft.de http://blog.schauderhaft.de, 42, "c:\\temp\\x.txt", 23).build(); maybe? It's not addressing named parameters issue, but I find it better like this.
- devgutt 14y agoI think this way is better: o.doSomething({ name : "Alfred E. Neumann", link : "http://blog.schauderhaft.de, ultimateAnswer: 42, tempFile: "c:\\temp\\x.txt", zip : 23});
- javinpaul 14y agoanother example of Builder pattern http://goo.gl/NkAhE http://goo.gl/NkAhE