( davebacsi | 2021. 12. 23., cs – 12:32 )

Szerkesztve: 2021. 12. 23., cs – 12:33
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class KellemesUnnepeket{
	private static KellemesUnnepeket instance;
    public static void main(String[] args){
        instantiateKellemesUnnepeketMainClassAndRun();
    }
    
    public static void instantiateKellemesUnnepeketMainClassAndRun(){
    	instance = new KellemesUnnepeket();
    	
    }
    
    public KellemesUnnepeket(){
    	KellemesUnnepeketFactory factory = KellemesUnnepeketFactory.getInstance();
    	IKellemesUnnepeket kellemesUnnepeket = factory.createKellemesUnnepeket();
    	IKellemesUnnepeketString kellemesUnnepeketString = kellemesUnnepeket.getKellemesUnnepeket();
    	IPrintStrategy printStrategy = kellemesUnnepeket.getPrintStrategy();
    	IStatusCode code = kellemesUnnepeket.print(printStrategy, kellemesUnnepeketString);
    	if(code.getStatusCode() != 0){
    		throw new RuntimeException("Failed to print: " + code.getStatusCode());
    	}
    }
}

class StringFactory{
	private static StringFactory instance = new StringFactory();
	public static StringFactory getInstance(){
		return instance;
	}
	public KellemesUnnepeketString createKellemesUnnepeketString(String str){
		KellemesUnnepeketString s = new KellemesUnnepeketString();
		s.s = str;
		return s;
	}
}

class PrintStrategyFactory{
	private static PrintStrategyFactory instance = new PrintStrategyFactory();
	public static PrintStrategyFactory getInstance(){
		return instance;
	}
	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;
	}
}

class PrintStrategyImplementation implements IPrintStrategy{
	private OutputStream print;
	public IStatusCode setupPrinting() {
		try{
			FileDescriptor descriptor = FileDescriptor.out;
			print = new FileOutputStream(descriptor);
			return new StatusCodeImplementation(0);
		}
		catch(Exception e){
			return new StatusCodeImplementation(-1);
		}
	}
	public IStatusCode print(IKellemesUnnepeketString string) {
		try{
			print.write(string.getKellemesUnnepeketString().getKellemesUnnepeketString().concat("\n").getBytes("UTF-8"));
			return new StatusCodeImplementation(0);
		}
		catch(Exception e){
			return new StatusCodeImplementation(-1);
		}
	}
	
}

class StatusCodeImplementation implements IStatusCode{
	private int code;
	public StatusCodeImplementation(int code){
		this.code = code;
	}
	public int getStatusCode() {
		return code;
	}
}

class KellemesUnnepeketString{
	String s;
	public String getKellemesUnnepeketString(){
		return s;
	}
}

class KellemesUnnepeketStringImplementation implements IKellemesUnnepeketString{
	public KellemesUnnepeketString getKellemesUnnepeketString(){
		StringFactory factory = StringFactory.getInstance();
		KellemesUnnepeketString s = factory.createKellemesUnnepeketString("Kellemes ünnepeket!");
		return s;
	}
}

class KellemesUnnepeketFactory{
	private static KellemesUnnepeketFactory instance = new KellemesUnnepeketFactory();
	public static KellemesUnnepeketFactory getInstance(){
		return instance;
	}
	public IKellemesUnnepeket createKellemesUnnepeket(){
		IKellemesUnnepeket kellemesUnnepeket = new KellemesUnnepeketImplementation();
		return kellemesUnnepeket;
	}
}

class KellemesUnnepeketImplementation implements IKellemesUnnepeket{
	public IKellemesUnnepeketString getKellemesUnnepeket() {
		IKellemesUnnepeketString string = new KellemesUnnepeketStringImplementation();
		return string;
	}
	public IPrintStrategy getPrintStrategy() {
		PrintStrategyFactory factory = PrintStrategyFactory.getInstance();
		return factory.createIPrintStrategy();
	}
	public IStatusCode print(IPrintStrategy strategy, IKellemesUnnepeketString toPrint) {
		IStatusCode code = strategy.print(toPrint);
		return code;
	}
}

interface IKellemesUnnepeketString{
	public KellemesUnnepeketString getKellemesUnnepeketString();
}
interface IKellemesUnnepeket{
    public IKellemesUnnepeketString getKellemesUnnepeket();
    public IPrintStrategy getPrintStrategy();
    public IStatusCode print(IPrintStrategy strategy, IKellemesUnnepeketString toPrint);
}
interface IStatusCode{
	public int getStatusCode();
}
interface IPrintStrategy{
	public IStatusCode setupPrinting();
	public IStatusCode print(IKellemesUnnepeketString string);
}