Method composition provides a flexible way of combining two or more functions into a single function. This offers flexibility that will otherwise not be present. We illustrate how this technique can be implemented using the Function
interface. Its compose
and andThen
methods support the execution of functions before or after another function. We also demonstrated the usefulness of this technique by passing the composite functions to other methods allowing it be executed when needed.
Fluent interfaces are common in functional programming languages. We discussed the difference between this type of interface and the chaining and cascading techniques. While similar, chaining methods do not necessarily return the same object each time. Cascading does return the same object, and fluent interfaces add a more natural set of method names.
Given the importance of fluent interfaces in a class's design, we demonstrated how to create fluent interfaces. We followed up by showing how it can be used...