Builder Design Pattern

Category : Design Patterns | Sub Category : Creational design patterns | By Prasad Bonam Last updated: 2023-07-09 13:05:45 Viewed : 71

Builder Design Pattern:

The Builder pattern is a creational design pattern that separates the construction of an object from its representation. It allows you to create complex objects step by step and provides a flexible way to construct objects with different variations or configurations. The Builder pattern promotes fluent and readable code by providing a clear and concise way to set object properties. Here is an example of implementing the Builder pattern in Java:

/ Product class public class Product { private String property1; private String property2; private String property3; public String getProperty1() { return property1; } public void setProperty1(String property1) { this.property1 = property1; } public String getProperty2() { return property2; } public void setProperty2(String property2) { this.property2 = property2; } public String getProperty3() { return property3; } public void setProperty3(String property3) { this.property3 = property3; } @Override public String toString() { return "Product{" + "property1=" + property1 + + ", property2=" + property2 + + ", property3=" + property3 + + }; } } // Builder class public class ProductBuilder { private Product product; public ProductBuilder() { product = new Product(); } public ProductBuilder setProperty1(String property1) { product.setProperty1(property1); return this; } public ProductBuilder setProperty2(String property2) { product.setProperty2(property2); return this; } public ProductBuilder setProperty3(String property3) { product.setProperty3(property3); return this; } public Product build() { return product; } } // Client code public class Client { public static void main(String[] args) { Product product = new ProductBuilder() .setProperty1("Value 1") .setProperty2("Value 2") .setProperty3("Value 3") .build(); System.out.println(product); } }

In this example:

  • The Product class represents the object we want to construct. It has several properties and corresponding getter/setter methods.
  • The ProductBuilder class provides a fluent API to build the Product object. It has methods for setting each property and returns the builder instance, allowing method chaining.
  • The ProductBuilder constructs a Product object by setting its properties using the provided setter methods.
  • The Client class demonstrates how to use the builder to construct a Product object with a specific configuration. It invokes the builder methods in a chain and finally calls the build() method to obtain the constructed Product instance.
  • The constructed Product object is then printed to showcase the final result.

By using the Builder pattern, you can create complex objects with various configurations in a more readable and maintainable way. It simplifies the object creation process and allows you to easily adjust or extend the construction process without modifying the client code.

Related Articles

Leave a Comment: