Skip to content

Commit 8c349d8

Browse files
Update README.md
Fixes iluwatar#2263 Explanation for Step Builder. Updating Readme.md of Step Builder.
1 parent cb2d794 commit 8c349d8

File tree

1 file changed

+86
-2
lines changed

1 file changed

+86
-2
lines changed

Diff for: step-builder/README.md

+86-2
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,100 @@ tag:
66
- Instantiation
77
---
88

9+
# Step Builder Pattern
10+
11+
## Explanation
12+
13+
The Step Builder pattern is a creational design pattern used to construct a complex object step by step. It provides a fluent interface to create an object with a large number of possible configurations, making the code more readable and reducing the need for multiple constructors or setter methods.
14+
915
## Intent
1016
An extension of the Builder pattern that fully guides the user through the creation of the object with no chances of confusion.
1117
The user experience will be much more improved by the fact that he will only see the next step methods available, NO build method until is the right time to build the object.
1218

13-
## Class diagram
14-
![alt text](./etc/step-builder.png "Step Builder")
19+
## Real World Example
20+
21+
Imagine you are building a configuration object for a database connection. The connection has various optional parameters such as host, port, username, password, and others. Using the Step Builder pattern, you can set these parameters in a clean and readable way:
22+
23+
```java
24+
DatabaseConnection connection = new DatabaseConnection.Builder()
25+
.setHost("localhost")
26+
.setPort(3306)
27+
.setUsername("user")
28+
.setPassword("password")
29+
.setSSL(true)
30+
.build();
31+
```
32+
33+
## In Plain Words
34+
35+
The Step Builder pattern allows you to construct complex objects by breaking down the construction process into a series of steps. Each step corresponds to setting a particular attribute or configuration option of the object. This results in more readable and maintainable code, especially when dealing with objects that have numerous configuration options.
36+
37+
## Wikipedia Says
38+
39+
According to Wikipedia, the Step Builder pattern is a creational design pattern in which an object is constructed step by step. It involves a dedicated 'director' class, which orchestrates the construction process through a series of 'builder' classes, each responsible for a specific aspect of the object's configuration. This pattern is particularly useful when dealing with objects that have a large number of optional parameters.
40+
41+
## Programmatic Example
42+
43+
Assuming you have a class `Product` with several configurable attributes, a Step Builder for it might look like this:
44+
45+
```java
46+
public class Product {
47+
private String name;
48+
private double price;
49+
private int quantity;
50+
51+
// private constructor to force the use of the builder
52+
private Product(String name, double price, int quantity) {
53+
54+
this.name = name;
55+
this.price = price;
56+
this.quantity = quantity;
57+
58+
}
59+
60+
public static class Builder {
61+
private String name;
62+
private double price;
63+
private int quantity;
64+
65+
public Builder setName(String name) {
66+
this.name = name;
67+
return this;
68+
}
69+
70+
public Builder setPrice(double price) {
71+
this.price = price;
72+
return this;
73+
}
74+
75+
public Builder setQuantity(int quantity) {
76+
this.quantity = quantity;
77+
return this;
78+
}
79+
80+
public Product build() {
81+
return new Product(name, price, quantity);
82+
}
83+
}
84+
}
85+
86+
// Usage
87+
Product product = new Product.Builder()
88+
.setName("Example Product")
89+
.setPrice(29.99)
90+
.setQuantity(100)
91+
.build();
92+
```
93+
94+
This example demonstrates how you can use the Step Builder pattern to create a `Product` object with a customizable set of attributes. Each method in the builder corresponds to a step in the construction process.
1595

1696
## Applicability
1797
Use the Step Builder pattern when the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled the construction process must allow different representations for the object that's constructed when in the process of constructing the order is important.
1898

99+
## Another example with class diagram
100+
![alt text](./etc/step-builder.png "Step Builder")
101+
102+
19103
## Credits
20104

21105
* [Marco Castigliego - Step Builder](http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html)

0 commit comments

Comments
 (0)