java 120 lines · 2 tabs

Builder pattern for object construction

David Kumar Jan 2026
2 tabs
package com.example.demo.model;

import lombok.Builder;
import lombok.Data;
import java.time.LocalDateTime;

@Data
@Builder
public class LombokUser {
    private Long id;
    private String name;
    private String email;
    private String phone;
    private String address;

    @Builder.Default
    private LocalDateTime createdAt = LocalDateTime.now();

    @Builder.Default
    private boolean active = true;
}

// Usage with Lombok:
// LombokUser user = LombokUser.builder()
//     .name("John Doe")
//     .email("john@example.com")
//     .build();
2 files · java Explain with highlit

The Builder pattern creates complex objects step-by-step, improving readability and flexibility. I implement builders with static inner classes—fluent methods return the builder for chaining. Required fields use constructor parameters, optional fields use builder methods. The build() method validates and constructs the final object. Lombok's @Builder annotation generates builders automatically. Builders prevent telescoping constructors and make object creation self-documenting. They're ideal for objects with many parameters, especially when some are optional. Immutable objects benefit from builders since all fields are set before construction. The pattern enhances code clarity—User.builder().name("John").email("john@example.com").build() reads like natural language. Spring Boot's auto-configuration heavily uses builders internally.


Related snips

graphql
type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post!]!
    createdAt: String!

GraphQL API with Spring Boot

java graphql spring-boot
by David Kumar 3 tabs
java
package com.example.starter.config;

import com.example.starter.properties.CustomProperties;
import com.example.starter.service.CustomService;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

Custom Spring Boot starters

java spring-boot starter
by David Kumar 4 tabs
java
package com.example.demo.config;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

Messaging with Apache Kafka

java kafka messaging
by David Kumar 3 tabs
rust
pub struct Server {
    host: String,
    port: u16,
    workers: usize,
}

Builder pattern for complex struct initialization

rust patterns builder
by Marcus Chen 1 tab
ruby
class UserRegistrationService
  class Result
    attr_reader :user, :errors

    def initialize(success:, user: nil, errors: [])
      @success = success

Service objects for business logic encapsulation

ruby rails service-objects
by Sarah Mitchell 2 tabs
java
package com.example.demo.controller;

import com.example.demo.dto.FileMetadata;
import com.example.demo.service.FileStorageService;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;

File upload and download handling

java spring-boot file-upload
by David Kumar 2 tabs

Share this code

Here's the card — post it anywhere.

Builder pattern for object construction — share card
Link copied