Git Product home page Git Product logo

builder's Introduction

Introduction

This small project provides a boiler-plate free mechanism to add a Builder API to classes.

Advantages of a Builder API:

  • unambiguous: Parameter names rather than their order is used to set values.
  • fluent: An object can be initialized in a single expression, allowing them to be used inline as function arguments, etc.
  • consistent: When used throughout a project, class construction becomes uniform and consistent, improving code readability.
  • portable: Such patterns are common in other languages, such as Java, and using them can help people more easily transition into D projects.

Usage

Example initialization without a builder:

class A1 {
  private int a;
  private string b;
  private int c = 3;

  void setA(int a) {
    this.a = a;
  }
  // More setters...
}

// One method to build a1, which is verbose.
A1 a1 = new A1();
a1.setA(3);
a1.setB("ham");
// Another way to build a1, which can make large numbers of parameters ambiguous.
A1 a1 = new A1(3, "ham");
myFunction(a1);

Using the AddBuilder template from within a class, creates an inner class and methods to support a Builder API. Using this pattern, it is now possible to create and initialize objects as a single expression, which allows them to be built, for example, as function arguments.

import builder : AddBuilder;

// Parameter initializers and inheritance are both supported.
class A2 : A1 {
  string d = "sam";

  mixin AddBuilder!(typeof(this));
}

myFunction(A2.builder()
    .a(3)
    .b("ham")
    .d("sam")
    .build());

The Builder API is heavily influenced by Project Lombok's Builder for Java.

builder's People

Contributors

vnayar avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

zkxjzmswkwl

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.