builder 0.2.1
A library permitting easy addition of a builder pattern to a class.
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
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.
- 0.2.1 released a year ago
- vnayar/builder
- LGPL
- Copyright © 2023, Vijay Nayar
- Authors:
- Dependencies:
- none
- Versions:
-
0.2.1 2023-Jul-28 0.2.0 2023-Jul-27 0.1.0 2023-Jan-05 ~master 2023-Jul-28 - Download Stats:
-
-
0 downloads today
-
21 downloads this week
-
94 downloads this month
-
1489 downloads total
-
- Score:
- 2.1
- Short URL:
- builder.dub.pm