cactoos icon indicating copy to clipboard operation
cactoos copied to clipboard

Printer for Text

Open andreoss opened this issue 4 years ago • 3 comments

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 avatar Jul 31 '20 10:07 andreoss

@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 avatar Aug 23 '20 15:08 victornoel

@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));
    }

andreoss avatar Aug 24 '20 19:08 andreoss

@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 Text implements this interface?
  • Is Output really the object print should take, since calling its stream method can return fresh OutputStream each time?

victornoel avatar Aug 25 '20 06:08 victornoel