Guest User

Untitled

a guest
Jul 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. import java.io.FileDescriptor;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.io.PrintStream;
  6.  
  7. public class HelloWorld{
  8. private static HelloWorld instance;
  9. public static void main(String[] args){
  10. instantiateHelloWorldMainClassAndRun();
  11. }
  12.  
  13. public static void instantiateHelloWorldMainClassAndRun(){
  14. instance = new HelloWorld();
  15.  
  16. }
  17.  
  18. public HelloWorld(){
  19. HelloWorldFactory factory = HelloWorldFactory.getInstance();
  20. IHelloWorld helloWorld = factory.createHelloWorld();
  21. IHelloWorldString helloWorldString = helloWorld.getHelloWorld();
  22. IPrintStrategy printStrategy = helloWorld.getPrintStrategy();
  23. IStatusCode code = helloWorld.print(printStrategy, helloWorldString);
  24. if(code.getStatusCode() != 0){
  25. throw new RuntimeException("Failed to print: " + code.getStatusCode());
  26. }
  27. }
  28. }
  29.  
  30. class StringFactory{
  31. private static StringFactory instance = new StringFactory();
  32. public static StringFactory getInstance(){
  33. return instance;
  34. }
  35. public HelloWorldString createHelloWorldString(String str){
  36. HelloWorldString s = new HelloWorldString();
  37. s.s = str;
  38. return s;
  39. }
  40. }
  41.  
  42. class PrintStrategyFactory{
  43. private static PrintStrategyFactory instance = new PrintStrategyFactory();
  44. public static PrintStrategyFactory getInstance(){
  45. return instance;
  46. }
  47. public IPrintStrategy createIPrintStrategy(){
  48. IPrintStrategy printStrategy = new PrintStrategyImplementation();
  49. IStatusCode code = printStrategy.setupPrinting();
  50. if(code.getStatusCode() != 0){
  51. throw new RuntimeException("Failed to create IPrintStrategy: " + code.getStatusCode());
  52. }
  53. return printStrategy;
  54. }
  55. }
  56.  
  57. class PrintStrategyImplementation implements IPrintStrategy{
  58. private OutputStream print;
  59. public IStatusCode setupPrinting() {
  60. try{
  61. FileDescriptor descriptor = FileDescriptor.out;
  62. print = new FileOutputStream(descriptor);
  63. return new StatusCodeImplementation(0);
  64. }
  65. catch(Exception e){
  66. return new StatusCodeImplementation(-1);
  67. }
  68. }
  69. public IStatusCode print(IHelloWorldString string) {
  70. try{
  71. print.write(string.getHelloWorldString().getHelloWorldString().concat("\n").getBytes("UTF-8"));
  72. return new StatusCodeImplementation(0);
  73. }
  74. catch(Exception e){
  75. return new StatusCodeImplementation(-1);
  76. }
  77. }
  78.  
  79. }
  80.  
  81. class StatusCodeImplementation implements IStatusCode{
  82. private int code;
  83. public StatusCodeImplementation(int code){
  84. this.code = code;
  85. }
  86. public int getStatusCode() {
  87. return code;
  88. }
  89. }
  90.  
  91. class HelloWorldString{
  92. String s;
  93. public String getHelloWorldString(){
  94. return s;
  95. }
  96. }
  97.  
  98. class HelloWorldStringImplementation implements IHelloWorldString{
  99. public HelloWorldString getHelloWorldString(){
  100. StringFactory factory = StringFactory.getInstance();
  101. HelloWorldString s = factory.createHelloWorldString("Hello, World!");
  102. return s;
  103. }
  104. }
  105.  
  106. class HelloWorldFactory{
  107. private static HelloWorldFactory instance = new HelloWorldFactory();
  108. public static HelloWorldFactory getInstance(){
  109. return instance;
  110. }
  111. public IHelloWorld createHelloWorld(){
  112. IHelloWorld helloWorld = new HelloWorldImplementation();
  113. return helloWorld;
  114. }
  115. }
  116.  
  117. class HelloWorldImplementation implements IHelloWorld{
  118. public IHelloWorldString getHelloWorld() {
  119. IHelloWorldString string = new HelloWorldStringImplementation();
  120. return string;
  121. }
  122. public IPrintStrategy getPrintStrategy() {
  123. PrintStrategyFactory factory = PrintStrategyFactory.getInstance();
  124. return factory.createIPrintStrategy();
  125. }
  126. public IStatusCode print(IPrintStrategy strategy, IHelloWorldString toPrint) {
  127. IStatusCode code = strategy.print(toPrint);
  128. return code;
  129. }
  130. }
  131.  
  132. interface IHelloWorldString{
  133. public HelloWorldString getHelloWorldString();
  134. }
  135. interface IHelloWorld{
  136. public IHelloWorldString getHelloWorld();
  137. public IPrintStrategy getPrintStrategy();
  138. public IStatusCode print(IPrintStrategy strategy, IHelloWorldString toPrint);
  139. }
  140. interface IStatusCode{
  141. public int getStatusCode();
  142. }
  143. interface IPrintStrategy{
  144. public IStatusCode setupPrinting();
  145. public IStatusCode print(IHelloWorldString string);
  146. }
Add Comment
Please, Sign In to add comment