Created
March 26, 2013 23:42
-
-
Save vndmtrx/5250327 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| interface Printer { | |
| void print(Message message); | |
| } | |
| class Message { | |
| private String message; | |
| public Message(String message) { | |
| this.message = message; | |
| } | |
| public void print(Printer printer) { | |
| printer.print(this); | |
| } | |
| public String toString() { | |
| return message; | |
| } | |
| } | |
| abstract class AbstractPrinterFactory { | |
| public static AbstractPrinterFactory getFactory() { | |
| return new SystemOutPrinterFactory(); | |
| } | |
| public abstract Printer getPrinter(); | |
| } | |
| class SystemOutPrinterFactory extends AbstractPrinterFactory { | |
| public Printer getPrinter() { | |
| return new SystemOutPrinter(); | |
| } | |
| } | |
| class SystemOutPrinter implements Printer { | |
| public void print(Message message) { | |
| System.out.println(message); | |
| } | |
| } | |
| class HelloWorld { | |
| public static void main(String[] args) { | |
| Message message = new Message("Hello, World!"); | |
| AbstractPrinterFactory factory = SystemOutPrinterFactory.getFactory(); | |
| Printer printer = factory.getPrinter(); | |
| message.print(printer); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment