Design Pattern - (Object) Builder

Card Puncher Data Processing

About

The intent of the Builder design pattern is to separate the construction of a complex object from its representation.

Same idea than a Design Pattern - (Static) Factory

Instead of using numerous constructors, the builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once.

The builder uses the Fluent Interface idiom to make the client code more readable

Why ?

  • In Java for instance, with multiple constructor, you need to have the keyword this in the first place. You cannot then have a constructor with a path to a inputstream constructor that doesn't send a exception. Why ? because you need first to transform a path into a input stream and this operations sends an exception that you need to handle.

Example

The class with a builder

public class User {
    private final String firstName; // required
    private final String lastName; // required
    private final int age; // optional
    
    private User(UserBuilder builder) {
        this.firstName = builder.firstName;
        this.lastName = builder.lastName;
        this.age = builder.age;
    }

    public static class UserBuilder {
        private final String firstName;
        private final String lastName;
        private int age;

        public UserBuilder(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public UserBuilder age(int age) {
            this.age = age;
            return this;
        }
       
        public User build() {
            return new User(this);
        }

    }
}

The construction

User user =  new
                User.UserBuilder("Nico", "Gerard")
                .age(41)
                .build();

Documentation / Reference





Discover More
Card Puncher Data Processing
Data Processing - (Pipeline | Compose | Chain)

A pipeline is a finite automata where: the data transition from one state to another via a series of transformations (work) A pipeline creates a composition relationship. A pipeline is also...
Card Puncher Data Processing
Design Pattern - (Fluent Interface|Method Chaining)

Same technique that the builder pattern to build an Domain Specific Language in declarative way. The API is primarily designed to be readable and to flow between methods/functions Spark script uses heavily...
Card Puncher Data Processing
Design Pattern - (Static) Factory

The Factory pattern creates an instance of an object according to a given specification, sometimes provided as arguments, sometimes inferred. It's a dependency resolving approach. A factory class decouples...
Card Puncher Data Processing
Design pattern - The Singleton

The singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is...
Java Conceptuel Diagram
Java - Object (instance of a class)

An java/lang/Objectobject: stores its state in fields and exposes its behaviour through methods (functions in some programming languages). Methods operate on an object's internal state and serve...
Java Conceptuel Diagram
Java Concurrency - Immutable Object

Immutable object in Java Concurrency. Don't provide “setter” methods — methods that modify fields or objects referred to by fields. Make all fields final and private. Don't allow subclasses...
Spark Query Plan Generation
Spark - Engine

In Spark, the Spark engine is a SQL’s optimized execution engine and understand as input: or and is therefore sometimes known as the SQL Engine. In Spark, functions are pipelined around their...
Spark Pipeline
Spark - Resilient Distributed Datasets (RDDs)

Resilient distributed datasets are one of the data structure in Spark. Write programs in terms of operations on distributed datasets Partitioned collections of objects spread across a cluster, stored...



Share this page:
Follow us:
Task Runner