Streams vs. Decorators
Streams vs. Decorators
Here's one take on the problems with Java's Streams API and how decorators in your code can present a better OOP solution.
Join the DZone community and get the full member experience.Join For Free
The Streams API was introduced in Java 8, together with lambda expressions, just a few years ago. I, as a disciplined Java adept, tried to use this new feature in a few of my projects, for example here and here. I didn't really like it and went back to good old decorators. Moreover, I created Cactoos, a library of decorators, to replace Guava, which is not so good in so many places.
Here is a primitive example. Let's say we have a collection of measurements coming in from some data source, they are all numbers between zero and one:
Now, we need to show only the first 10 of them, ignoring zeros and ones, and re-scaling them to
(0..100). Sounds like an easy task, right? There are three ways to do it: procedural, object-oriented, and the Java 8 way. Let's start with the procedural way:
Why is this a procedural way? Because it's imperative. Why is it imperative? Because it's procedural. Nah, I'm kidding.
It's imperative because we're giving instructions to the computer about what data to put where and how to iterate through it. We're not declaring the result, but imperatively building it. It works, but it's not really scalable. We can't take part of this algorithm and apply it to another use case. We can't really modify it easily — for example, to take numbers from two sources instead of one, etc. It's procedural. Enough said. Don't do it this way.
Now, Java 8 gives us the Streams API, which is supposed to offer a functional way to do the same. Let's try to use it.
This will work, but will say
Probe #0 for all probes, because
forEach() doesn't work with indexes. There is no such thing as
forEachWithIndex() in the
Stream interface as of Java 8 (and Java 9 too). Here is a workaround with an atomic counter:
"What's wrong with that?" you may ask. First, see how easily we got into trouble when we didn't find the right method in the
Stream interface. We immediately fell off the "streaming" paradigm and got back to the good old procedural global variable (the counter). Second, we don't really see what's going on inside those
forEach() methods. How exactly do they work? The documentation says that this approach is "declarative" and each method in the
Stream interface returns an instance of some class. What classes are they? We have no idea by just looking at this code.
These two problems are connected. The biggest issue with this streaming API is the very interface
Stream — it's huge. At the time of writing, there are 43 methods. Forty-three, in a single interface! This is against each and every principle of object-oriented programming, starting with SOLID and then up to more serious ones.
What is the object-oriented way to implement the same algorithm? Here is how I would do it with Cactoos, which is just a collection of
simple Java classes:
Let's see what's going on here. First,
Filtered decorates our iterable
probes to take certain items out of it. Notice that
Limited, also being an
Iterable, takes only the first ten items out. Then
Mapped converts each probe into an instance of
Scalar<Boolean>, which does the line printing.
Finally, the instance of
And goes through the list of "scalars" and ask each of them to return
boolean. They print the line and return
true. Since it's
And makes the next attempt with the next scalar. Finally, its method
But wait, there are no indexes. Let's add them. In order to do that we just use another class, called
Scalar<Boolean> we now map our probes to
Func<Integer, Boolean> to let them accept the index.
The beauty of this approach is that all classes and interfaces are small and that's why they're very composable. To make an iterable of probes limited we decorate it with
Limited; to make it filtered we decorate it with
Filtered; to do something else we create a new decorator and use it. We're not stuck to one single interface like
The bottom line is that decorators are an object-oriented instrument to modify the behavior of collections, while streams is something else that I can't even find the name for.
P.S. By the way, this is how the same algorithm can be implemented with the help of Guava's
This is some weird combination of object-oriented and functional styles.
Published at DZone with permission of Yegor Bugayenko , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.