cactoos
cactoos copied to clipboard
Printer for Text
Is it possible to have a smart class for Text which realizes concept of printer? https://www.yegor256.com/2016/04/05/printers-instead-of-getters.html
final class Print implements Text {
private final Text text;
public Print(final Text txt) {
this.text = txt;
}
@Override
public String asString() throws Exception {
return this.text.asString();
}
public void print(final Output output) throws Exception {
output.stream().write(new BytesOf(this).asBytes());
}
}
@andreoss is the need to be able to print a Text to an Output?
I believe the printer concept is more about an object that takes a Text in its print method than the opposite as you propose.
WDYT?
@victornoel
We can think that Output is a medium, and Text prints itself to it.
It seems to be common in takes for an object to have two methods for print(), the first one returns a string and the other one
writes to OutputStream. 1 2. It could be
public void print(final OutputStream output) throws IOException {
new Print(this.text).print(new OutputOf(output));
}
@yegor256 what do you think about this discussion from a general EO point of view?
I wonder
- Does it mean we would need to introduce a general interface for objects printable to an
Output? - Should
Textimplements this interface? - Is
Outputreally the objectprintshould take, since calling itsstreammethod can return freshOutputStreameach time?