Skip to content

Builder pattern needed? #6

Description

@jruts

Hi,

Since node/javascript does not have the problem that a builder pattern fixes, since you can just pass along an object literal, why would you want to apply all this overhead?

If we take a look at the example: https://github.com/torokmark/design_patterns_in_typescript/blob/master/builder/builder.ts

We could just use an object literal instead:

// user.js
const create = args => {
  const options = Object.assign({}, defaultOptions, args); // Normalize the options
  return {
    name: options.name,
    age: options.age,
    phone: options.phone, 
    address: options.address
  }
}

module.exports.create = create;

// test.js
const user = require('user.js');

const myUser = user.create({
  name: 'a name',
  age: 30
});

This achieves the same thing as the builder pattern. The main difference here is that user.js is a lot smaller than the builder.ts example. (10 lines vs 60+)
Passing along an object literal, which is very common in node, is just as readable as with a builder, but with a lot less overhead or boilerplate.

So my question is: why would you do this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions