MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Java Design Patterns | Builder
  1. Overview and Definition
  2. Class Diagram
  3. Implementation Example
    • The Product Interface
    • The Concrete Product
    • The Builder Interface
    • The Concrete Builder
    • The Director
    • Testing the Builder Pattern

  1. Overview and Definition
    The Builder pattern is a creational design pattern that separates the construction of a complex object from its representation. This pattern allows the same construction process to create different representations of the object by using different builders.

    Key Components:
    • Product: The complex object that is being constructed. This can be an interface or abstract class defining the final object.
    • Concrete Product: The specific implementation of the Product that provides the actual functionality.
    • Builder: An abstract interface that declares the construction steps for building the Product.
    • Concrete Builder: Implements the Builder interface and provides specific implementations for each construction step.
    • Director: Controls the construction process by calling the builder methods in a specific sequence to create the final product.
    • Client: Uses the Director and Builder to construct the desired object without knowing the construction details.

    Benefits:
    • Separation of Concerns: Construction logic is separated from the product representation, making the code more modular and maintainable.
    • Different Representations: The same construction process can create different representations by using different concrete builders.
  2. Class Diagram
    Builder Pattern Components:
    • Product: Session interface - defines the complex object being constructed.
    • Concrete Product: HttpSession class - the specific implementation of the session.
    • Builder: SessionBuilder interface - declares the construction steps.
    • Concrete Builder: HttpSessionBuilder class - implements the construction steps for HTTP sessions.
    • Director: SessionDirector class - controls the construction process and sequence.
    ┌───────────────────────────────┐
    │       <<interface>>           │
    │          Session              │  ← Product
    ├───────────────────────────────┤
    │ + sendRequest(String): String │
    └───────────────────────────────┘
                △
                │ implements
                │
    ┌───────────────────────────────┐
    │        HttpSession            │  ← Concrete Product
    ├───────────────────────────────┤
    │ + sendRequest(String): String │
    └───────────────────────────────┘
    
    
    ┌─────────────────────────────┐
    │       <<interface>>         │
    │       SessionBuilder        │  ← Builder
    ├─────────────────────────────┤
    │ + getSession(): Session     │
    │ + createSession(): void     │
    │ + configureSession(): void  │
    └─────────────────────────────┘
                △
                │ implements
                │
    ┌─────────────────────────────┐
    │     HttpSessionBuilder      │  ← Concrete Builder
    ├─────────────────────────────┤
    │ - session: Session          │
    │ + getSession(): Session     │
    │ + createSession(): void     │
    │ + configureSession(): void  │
    └─────────────────────────────┘
    
    
    ┌───────────────────────────────────┐
    │          SessionDirector          │  ← Director
    ├───────────────────────────────────┤
    │ - sessionBuilder: SessionBuilder  │
    │ + SessionDirector(SessionBuilder) │
    │ + build(): Session                │
    └───────────────────────────────────┘
    
  3. Implementation Example
    • The Product Interface:
      The Product interface defines the complex object that will be constructed by the builder. In this example, we create a Session interface that represents a communication session.
      public interface Session {
          public String sendRequest(final String request);
      }
    • The Concrete Product:
      The Concrete Product implements the Product interface and provides the actual functionality. Here, HttpSession represents a specific type of session for HTTP communication.
      public class HttpSession implements Session {
          public String sendRequest(final String request) {
              return "http response";
          }
      }
    • The Builder Interface:
      The Builder interface declares the abstract construction steps that all concrete builders must implement. Each method represents a step in the construction process.
      public interface SessionBuilder {
          public Session getSession();
      
          public void createSession();
      
          public void configureSession();
      }
    • The Concrete Builder:
      The Concrete Builder implements the Builder interface and provides specific implementations for each construction step. It maintains a reference to the product being built and knows how to construct it step by step.
      public class HttpSessionBuilder implements SessionBuilder {
          private Session session;
      
          public Session getSession() {
              return session;
          }
      
          public void createSession() {
              this.session = new HttpSession();
              // do something else with the created session ...
          }
      
          public void configureSession() {
              // configure the session ...
          }
      }
    • The Director:
      The Director controls the construction process by defining the order in which the builder methods should be called. It encapsulates the construction algorithm and ensures the product is built correctly.
      public class SessionDirector {
          private SessionBuilder sessionBuilder;
      
          public SessionDirector(final SessionBuilder sessionBuilder) {
              this.sessionBuilder = sessionBuilder;
          }
      
          public Session build() {
              sessionBuilder.createSession();
              sessionBuilder.configureSession();
              return sessionBuilder.getSession();
          }
      }
    • Testing the Builder Pattern:
      The client code demonstrates how to use the Builder pattern to construct complex objects. The client works with the Director and Builder without knowing the construction details.
      public class BuilderPatternTest {
          public static void main(String[] args) {
              // Create the specific builder for HTTP sessions
              final SessionBuilder sessionBuilder = new HttpSessionBuilder();
      
              // Create the director and pass the builder to it
              final SessionDirector sessionDirector = new SessionDirector(sessionBuilder);
      
              // Use the director to build the complete session
              final Session session = sessionDirector.build();
      
              // Use the constructed session
              final String response = session.sendRequest("GET /api/data");
              System.out.println("Response: " + response);
          }
      }
© 2025 mtitek