yegor256 / cactoos

Object-Oriented Java primitives, as an alternative to Google Guava and Apache Commons
https://www.cactoos.org
MIT License
742 stars 165 forks source link

Printer for Text #1424

Open andreoss opened 4 years ago

andreoss commented 4 years ago

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());
    }
}
victornoel commented 4 years ago

@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?

andreoss commented 4 years ago

@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. It could be

    public void print(final OutputStream output) throws IOException {
            new Print(this.text).print(new OutputOf(output));
    }
victornoel commented 4 years ago

@yegor256 what do you think about this discussion from a general EO point of view?

I wonder