Skip to content
/ builder Public

A D library permitting easy addition of a builder pattern to a class.

Notifications You must be signed in to change notification settings

vnayar/builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A D library permitting easy addition of a builder pattern to a class.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages