Skip to content
loading...

Give me a challenge; Java to JavaScript!

twitter logo github logo ・1 min read  

Give me some Java code, and I'll hand write a JavaScript equivalent, and when you get it, tell me how accurate I was, I think this will be a fun challenge!

Limitation

It must be possible with vanilla JavaScript

Cheers!

twitter logo DISCUSS (6)
markdown guide
 

How would you translate Java's Builder Pattern in JavaScript?

public static void main(String[] args) {
   Computer comp = new Computer
         .ComputerBuilder("500 GB", "2 GB")
         .setBluetoothEnabled(true)
         .setGraphicsCardEnabled(true)
   .build();
}

Why do you think people invented something like this who looks a bit cluncky at first?

 

The limitation was that it had to be fully translatable, but entirely full support isn't possible, but if JS did have full support, it would look like this:

const statictype = function(type,value) {
    if((typeof type !== " undefined" && type !== undefined) && value !== undefined) {
        if(type.constructor.name === value.constructor.name) { return value }
    }
    throw TypeError("Static types did not match")
}

const Program = function() {
    return new Program()
}
Program.main = Object.freeze(
    function main(...args) {
        let comp = statictype(
            Computer,
            Computer.ComputerBuilder("500 GB", " 2 GB").setBluetoothEnabled(true).setGraphicsCardEnabled(true)
        )
        return undefined
    }
)

Program.main()
 

I have to admit that I don't know enough of JavaScript to understand what you wrote :) But in fact that was not really the point of the pattern. The simplest equivalent of my snippet in JavaScript is this:

const computer = {
  "storage": "500 GB",
  "ram": "2GB",
  "bluetoothEnabled": true
  "graphicsCardEnabled": true
}

That's quite simple, yes, but it's not an equivalent. There are problems with using a plain Javascript object that the Java snippet does not have.

What do you think they are?

One is that JavaScript is simply not class based, this fucks alot of things up for many reasons, sadly.......

I think you can have something equivalent in the JS ecosystem. It's not about classes but about

1) type safety - that you can have with TypeScript
2) methods with too much parameters are not great so a fluent interface allows you to give a name to those parameters and default values to them
3) having immutable data, once you have .build() your object you cannot change it which makes it easier to handle - I've heard immutableJs is a thing in the JS ecosystem

You can read about it here
dzone.com/articles/design-patterns...

In Kotlin, which is what I'm using and is kind of the evolution of Java, the builder pattern is there by default when you create a data class

kotlinlang.org/docs/reference/data...

The closes to Java's variable.build() would be Object.freeze(variable), though, if you want to still edit the properties, instead, use it's sister, Object.seal()

Cheers!

Classic DEV Post from Feb 9

What do you have to Google? Every. Single. Time.

How about you? Any other stories to make me feel better?

PDS OWNER CALIN (Calin Baenen) profile image
I am a 13 (as of Oct 30 of 2019) yr/o developer (I have been developing mini-projects for 4, years now, since I was 9), who makes projects in languages like: Java, HTML, Python 3, JS, CSS, and C#.