Junior — Middle
Share your experience of implementing the Pipeline pattern in projects.
sobes.tech AI
Answer from AI
The Pipeline pattern allows organizing data processing as a sequence of steps, where the output of one step becomes the input for the next. In Java, I implemented a Pipeline using method chaining or functional interfaces.
For example, to process a collection of data, you can create a chain of functions:
import java.util.function.Function;
import java.util.List;
import java.util.stream.Collectors;
public class PipelineExample {
public static void main(String[] args) {
List<String> data = List.of(" one ", "two", " three ");
Function<String, String> trim = String::trim;
Function<String, String> toUpper = String::toUpperCase;
List<String> result = data.stream()
.map(trim.andThen(toUpper))
.collect(Collectors.toList());
System.out.println(result); // [ONE, TWO, THREE]
}
}
This approach simplifies adding new steps and improves code readability. In more complex cases, you can create custom Pipeline interfaces with methods for adding stages and executing the entire chain.