Last active
January 2, 2024 22:03
-
-
Save einarwh/3f29e247e91422eff74a9daec7b91ba4 to your computer and use it in GitHub Desktop.
Object-oriented Hello World.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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