Skip to content

Instantly share code, notes, and snippets.

@PaulStSmith
Forked from lolzballs/HelloWorld.java
Last active March 17, 2025 01:24
Show Gist options
  • Save PaulStSmith/640f83a3fbcaa55de180b4eee10ee2f5 to your computer and use it in GitHub Desktop.
Save PaulStSmith/640f83a3fbcaa55de180b4eee10ee2f5 to your computer and use it in GitHub Desktop.
Hello World Enterprise Edition
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* Main class for the HelloWorld application.
*/
public class HelloWorld {
private static HelloWorld instance;
/**
* Entry point of the application.
* @param args Command line arguments.
*/
public static void main(String[] args) {
instantiateHelloWorldMainClassAndRun();
}
/**
* Instantiate the main class and run its constructor.
*/
public static void instantiateHelloWorldMainClassAndRun() {
instance = new HelloWorld();
}
/**
* Constructor for HelloWorld class.
* Initializes and prints the HelloWorld string.
*/
public HelloWorld() {
HelloWorldFactory factory = HelloWorldFactory.getInstance();
IHelloWorld helloWorld = factory.createHelloWorld();
IHelloWorldString helloWorldString = helloWorld.getHelloWorld();
IPrintStrategy printStrategy = helloWorld.getPrintStrategy();
IStatusCode code = helloWorld.print(printStrategy, helloWorldString);
if (code.getStatusCode() != 0) {
throw new RuntimeException("Failed to print: " + code.getStatusCode());
}
}
}
/**
* Factory class for creating HelloWorldString objects.
*/
class StringFactory {
private static StringFactory instance = new StringFactory();
/**
* Get the singleton instance of StringFactory.
* @return The singleton instance.
*/
public static StringFactory getInstance() {
return instance;
}
/**
* Create a HelloWorldString object.
* @param str The string to be wrapped.
* @return A HelloWorldString object.
*/
public HelloWorldString createHelloWorldString(String str) {
HelloWorldString s = new HelloWorldString();
s.s = str;
return s;
}
}
/**
* Factory class for creating IPrintStrategy objects.
*/
class PrintStrategyFactory {
private static PrintStrategyFactory instance = new PrintStrategyFactory();
/**
* Get the singleton instance of PrintStrategyFactory.
* @return The singleton instance.
*/
public static PrintStrategyFactory getInstance() {
return instance;
}
/**
* Create an IPrintStrategy object.
* @return An IPrintStrategy object.
*/
public IPrintStrategy createIPrintStrategy() {
IPrintStrategy printStrategy = new PrintStrategyImplementation();
IStatusCode code = printStrategy.setupPrinting();
if (code.getStatusCode() != 0) {
throw new RuntimeException("Failed to create IPrintStrategy: " + code.getStatusCode());
}
return printStrategy;
}
}
/**
* Implementation of the IPrintStrategy interface.
*/
class PrintStrategyImplementation implements IPrintStrategy {
private OutputStream print;
/**
* Setup the output stream for printing.
* @return Status code of the setup operation.
*/
public IStatusCode setupPrinting() {
try {
FileDescriptor descriptor = FileDescriptor.out;
print = new FileOutputStream(descriptor);
return new StatusCodeImplementation(0);
} catch (Exception e) {
return new StatusCodeImplementation(-1);
}
}
/**
* Print the HelloWorld string to the output stream.
* @param string The HelloWorld string to be printed.
* @return Status code of the print operation.
*/
public IStatusCode print(IHelloWorldString string) {
try {
print.write(string.getHelloWorldString().getHelloWorldString().concat("\n").getBytes("UTF-8"));
return new StatusCodeImplementation(0);
} catch (Exception e) {
return new StatusCodeImplementation(-1);
}
}
}
/**
* Implementation of the IStatusCode interface.
*/
class StatusCodeImplementation implements IStatusCode {
private int code;
/**
* Constructor to set the status code.
* @param code The status code.
*/
public StatusCodeImplementation(int code) {
this.code = code;
}
/**
* Return the status code.
* @return The status code.
*/
public int getStatusCode() {
return code;
}
}
/**
* Class representing a HelloWorld string.
*/
class HelloWorldString {
String s;
/**
* Return the HelloWorld string.
* @return The HelloWorld string.
*/
public String getHelloWorldString() {
return s;
}
}
/**
* Implementation of the IHelloWorldString interface.
*/
class HelloWorldStringImplementation implements IHelloWorldString {
/**
* Factory pattern to create HelloWorldString instance.
* @return A HelloWorldString object.
*/
public HelloWorldString getHelloWorldString() {
StringFactory factory = StringFactory.getInstance();
HelloWorldString s = factory.createHelloWorldString("Hello, World!");
return s;
}
}
/**
* Factory class for creating IHelloWorld objects.
*/
class HelloWorldFactory {
private static HelloWorldFactory instance = new HelloWorldFactory();
/**
* Get the singleton instance of HelloWorldFactory.
* @return The singleton instance.
*/
public static HelloWorldFactory getInstance() {
return instance;
}
/**
* Create an IHelloWorld object.
* @return An IHelloWorld object.
*/
public IHelloWorld createHelloWorld() {
IHelloWorld helloWorld = new HelloWorldImplementation();
return helloWorld;
}
}
/**
* Implementation of the IHelloWorld interface.
*/
class HelloWorldImplementation implements IHelloWorld {
/**
* Return the HelloWorld string implementation.
* @return An IHelloWorldString object.
*/
public IHelloWorldString getHelloWorld() {
IHelloWorldString string = new HelloWorldStringImplementation();
return string;
}
/**
* Return the print strategy implementation.
* @return An IPrintStrategy object.
*/
public IPrintStrategy getPrintStrategy() {
PrintStrategyFactory factory = PrintStrategyFactory.getInstance();
return factory.createIPrintStrategy();
}
/**
* Print the HelloWorld string using the provided strategy.
* @param strategy The print strategy to use.
* @param toPrint The HelloWorld string to print.
* @return Status code of the print operation.
*/
public IStatusCode print(IPrintStrategy strategy, IHelloWorldString toPrint) {
IStatusCode code = strategy.print(toPrint);
return code;
}
}
/**
* Interface representing a HelloWorld string.
*/
interface IHelloWorldString {
/**
* Return the HelloWorld string.
* @return A HelloWorldString object.
*/
public HelloWorldString getHelloWorldString();
}
/**
* Interface representing a HelloWorld object.
*/
interface IHelloWorld {
/**
* Return the HelloWorld string implementation.
* @return An IHelloWorldString object.
*/
public IHelloWorldString getHelloWorld();
/**
* Return the print strategy implementation.
* @return An IPrintStrategy object.
*/
public IPrintStrategy getPrintStrategy();
/**
* Print the HelloWorld string using the provided strategy.
* @param strategy The print strategy to use.
* @param toPrint The HelloWorld string to print.
* @return Status code of the print operation.
*/
public IStatusCode print(IPrintStrategy strategy, IHelloWorldString toPrint);
}
/**
* Interface representing a status code.
*/
interface IStatusCode {
/**
* Return the status code.
* @return The status code.
*/
public int getStatusCode();
}
/**
* Interface representing a print strategy.
*/
interface IPrintStrategy {
/**
* Setup the output stream for printing.
* @return Status code of the setup operation.
*/
public IStatusCode setupPrinting();
/**
* Print the HelloWorld string to the output stream.
* @param string The HelloWorld string to be printed.
* @return Status code of the print operation.
*/
public IStatusCode print(IHelloWorldString string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment