I was using a Stream.Builder
and I stumbled upon the fact that this interface has both the methods accept(T t)
and add(T t)
. The only difference is that the former returns void
and the latter returns a Stream.Builder
.
The documentation even mentions these methods to have the same default implementation:
The default implementation behaves as if:
accept(t) return this;
Note that they forgot a semicolon, but that's another story.
My question is: why do they have two methods to add something to the stream builder? I think this clutters the API, and I thought they wanted to avoid that.
Is there any compelling reason to do so?
A stream can have multiple listeners and all those listeners can get the pipeline. puting in all will get equal value. The way you put values on the stream is by using the Stream Controller. A stream builder is a widget that can convert user-defined objects into a stream.
There are two types of streams in Flutter: single subscription streams and broadcast streams. Single subscription streams are the default. They work well when you're only using a particular stream on one screen.
Stream.Builder build() in Java Java 8Object Oriented ProgrammingProgramming. The build() method of the Stream.Builder class builds the stream, transitioning this builder to the built state. The syntax is as follows − Stream<T> build() Following is an example to implement the build() method of the Stream.Builder class −
My guess:
Stream.Builder
extends Consumer<T>
so it must implement the accept(T)
method.
But accept(T)
returns void
, so I think they added add(T)
for convenience: methods in a builder pattern implementation often return this
to be able to chain the building and finally call build()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With