question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] - Builder versus configuration object

See original GitHub issue

Question as to why you used the builder pattern instead of a configuration object pattern for configuring instances.

The following has ten total method calls just for one button.

var CustomButton = new MKButton.Builder()
  .withBackgroundColor(MKColor.Teal)
  .withShadowRadius(2)
  .withShadowOffset({width:0, height:2})
  .withShadowOpacity(.7)
  .withShadowColor('black')
  .withOnPress(() => {
    console.log('hi, raised button!');
  })
  .withTextStyle({
    color: 'white',
    fontWeight: 'bold',
  })
  .withText('RAISED BUTTON')
  .build();

The MooTools configuration pattern uses just one

var CustomButton = new MKButton({
    backgroundColor : MKColor.Teal,
    shadow : {
        radius  : 2,
        opacity : .7,
        color   : 'black',
        offset  : {
            width  : 0,
            height : 2
        }
    },
    textStyle {
        color      : 'white',
        fontWeight : 'bold'
    },
    onPress : () => {
        console.log('onPress')
    }

});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
xinthinkcommented, Jul 29, 2015

Hi @jaygarcia, thanks for the valuable question.

The same question has been frustrating me 😖, until I realize that I’m not going to obsolete the object pattern by introducing builder. I just can’t, configuration object is built into the heart of ES7 (Object Spread) and thus React (Props Transferring). For example, you’re always free to do this:

var coloredButtonProps = {
  style: {
    marginLeft: 20,
    marginRight: 20,
  },
  backgroundColor: MKColor.Purple,
  rippleLayerColor: MKColor.Lime,
  onPress: () => console.log('button clicked'),
};

var buttonTextProps = {
  pointerEvents: 'none',
  style: {
    color: 'white',
    fontWeight: 'bold',
  },
};

<MKButton {...coloredButtonProps}>
  <Text {...buttonTextProps}>
    Halo!
  </Text>
</MKButton>

the props objects can be reused & extended:

var accentButtonProps = {
  ...coloredButtonProps,
  backgroundColor: MKColor.DeepOrange,
};

<MKButton {...accentButtonProps}>
  <Text {...buttonTextProps}>
    Accent colored raised button
  </Text>
</MKButton>

So, my reasoning is that the builder is just another way to make and reuse view definitions, hoping those who feel familiar to builder pattern or method chaining will like it.

An additional reason to introduce builder pattern is that it can make the best out of IDE’s code completion feature (e.g. WebStorm). ide-code-completion

It is not an either-or situation, you can even mix them up, to leverage the pre-defined theme:

var accentButtonProps = {
  ...MKButton.coloredButton().toProps(),
  backgroundColor: MKColor.DeepOrange,
};

<MKButton {...accentButtonProps}>
  <Text {...buttonTextProps}>
    Accent colored raised button
  </Text>
</MKButton>

Hope my choice is reasonable, 🍻. I’ll leave this issue open in case of someone interested wanna join the discussion.

2reactions
kristian-pucciocommented, Nov 28, 2015

It would be great to able to combine the approaches. So use the builder pattern to configure widgets that you will use then be able to customise them at runtime using props. I know you can do this with some props but not all.

Specifically Text doesn’t work, I’m not sure about other props yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Builder pattern vs. config object - java - Stack Overflow
The builder pattern improves decoupling - your Product can be an interface and the only class that knows about the implementation (or ...
Read more >
Interact overview - Tanium Documentation
A dynamic question is one that you create and issue through the Ask a Question or Question Builder features in Interact. A saved...
Read more >
Builder - Refactoring.Guru
Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types...
Read more >
Articles Key Questions Overview
Key Questions are configured using the App Config Builder button, ... Key Questions displays all fields from the Application object.
Read more >
Create a question for a catalog item in catalog builder
The fields on the form vary based on the question type and subtype. · While you configure each question,its preview is available. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found