Skip to content

Instantly share code, notes, and snippets.

@einarwh
Last active January 2, 2024 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/3f29e247e91422eff74a9daec7b91ba4 to your computer and use it in GitHub Desktop.
Save einarwh/3f29e247e91422eff74a9daec7b91ba4 to your computer and use it in GitHub Desktop.
Object-oriented Hello World.
import java.io.*;
public interface Printer {
public void print(PrintStream stream);
}
public class ChainPrinter implements Printer {
private final char _c;
private final Printer _next;
public ChainPrinter(char c, Printer next) {
_c = c;
_next = next;
}
public void print(PrintStream stream) {
stream.print(_c);
_next.print(stream);
}
}
public class Terminator implements Printer {
public void print(PrintStream stream) {
stream.println();
}
}
public class HelloWorld {
public static void main(String[] args) {
new ChainPrinter('H',
new ChainPrinter('e',
new ChainPrinter('l',
new ChainPrinter('l',
new ChainPrinter('o',
new ChainPrinter(',',
new ChainPrinter(' ',
new ChainPrinter('W',
new ChainPrinter('o',
new ChainPrinter('r',
new ChainPrinter('l',
new ChainPrinter('d',
new Terminator()))))))))))))
.print(System.out);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment