A Simple Hello World using Java

This is an example of a simple hello world using Java.

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

public class HelloWorld {
    public static void main(String[] args) {
        Message message = new Message("Hello, World!");
        AbstractPrinterFactory factory = AbstractPrinterFactory.getFactory();
        Printer printer = factory.getPrinter(); 
        message.print(printer);       
    }           
}

Just for fun (H)

Google+

No Comments

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked


:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)