fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. interface ISort<String>
  8. {
  9. public void sort();
  10. }
  11.  
  12. interface ISortIterator<String>
  13. {
  14. boolean hasNext();
  15. ISort<String> getNext();
  16. }
  17. class ConcreteIterator<String> implements ISortIterator<String>
  18. {
  19. private List<ISort<String>> aList;
  20. private int cursor;
  21. ConcreteIterator(List<ISort<String>> al)
  22. {
  23. this.aList = al;
  24. }
  25. public boolean hasNext()
  26. {
  27. return this.cursor < this.aList.size() - 1;
  28. }
  29. public ISort<String> getNext()
  30. {
  31. cursor++;
  32. return this.aList.get(cursor-1);
  33.  
  34. }
  35. }
  36.  
  37. interface ITypeSort<String> extends ISort<String>
  38. {
  39. List<ISort<String>> getSortAlgorithms();
  40. String getTypeName();
  41. void sort();
  42. void addSortAlgorithm(final ISort<String> aSortAlgorithm);
  43. ISortIterator<String> getSortIterator();
  44. }
  45. abstract class AbstractSort<String> implements ISort<String>
  46. {
  47. public abstract void sort();
  48. }
  49. class TypeSort<String> extends AbstractSort<String> implements ITypeSort<String>
  50. {
  51. private final List<ISort<String>> listOfSortAlgorithms;
  52. private final String typeName;
  53.  
  54. public TypeSort(final String aTypeName)
  55. {
  56. this.listOfSortAlgorithms = new ArrayList<ISort<String>>();
  57. this.typeName = aTypeName;
  58. }
  59.  
  60. public void addSortAlgorithm(final ISort<String> aSortAlgorithm)
  61. {
  62. this.listOfSortAlgorithms.add(aSortAlgorithm);
  63. }
  64.  
  65. public String getTypeName()
  66. {
  67. return this.typeName;
  68. }
  69. public void sort()
  70. {
  71. Iterator<ISort<String>> iterator = this.listOfSortAlgorithms.iterator();
  72. List<String> sortedList = null;
  73. while(iterator.hasNext())
  74. {
  75. final ISort<String> sortAlgorithm = (ISort<String>) iterator.next();
  76. sortAlgorithm.sort();
  77. }
  78. }
  79. public List<ISort<String>> getSortAlgorithms()
  80. {
  81. return this.listOfSortAlgorithms;
  82. }
  83. public ISortIterator<String> getSortIterator()
  84. {
  85. return new ConcreteIterator<String>(this.getSortAlgorithms());
  86. }
  87. }
  88. class InsertionSort<String> extends AbstractSort<String> implements ISort<String>
  89. {
  90. public void sort()
  91. {
  92. System.out.println("Insertion Sort");
  93. }
  94. }
  95. class BubbleSort<String> extends AbstractSort<String> implements ISort<String>
  96. {
  97. public void sort()
  98. {
  99. System.out.println("Bubble Sort");
  100. }
  101. }
  102. class MergeSort<String> extends AbstractSort<String> implements ISort<String>
  103. {
  104. public void sort()
  105. {
  106. System.out.println("Merge Sort");
  107. }
  108. }
  109. class Factory
  110. {
  111. private static Factory factory;
  112.  
  113. private Factory()
  114. {
  115.  
  116. }
  117. public static Factory getInstance()
  118. {
  119. if(factory == null)
  120. {
  121. System.out.println("----Factory Started----");
  122. factory = new Factory();
  123. }
  124. return factory;
  125. }
  126. public ISort<String> getInsertionSortAlgorithm()
  127. {
  128. return new InsertionSort<String>();
  129. }
  130. public ISort<String> getBubbleSortAlgorithm()
  131. {
  132. return new BubbleSort<String>();
  133. }
  134. public ISort<String> getMergeSortAlgorithm()
  135. {
  136. return new MergeSort<String>();
  137. }
  138.  
  139. public ISort<String> getInternalSortAlgorithm()
  140. {
  141. final ITypeSort<String> internalSorts = new TypeSort<String>("Internal Sorts");
  142. internalSorts.addSortAlgorithm(this.getBubbleSortAlgorithm());
  143. internalSorts.addSortAlgorithm(this.getInsertionSortAlgorithm());
  144. internalSorts.addSortAlgorithm(this.getMergeSortAlgorithm());
  145. return internalSorts;
  146. }
  147.  
  148. }
  149. class Ideone
  150. {
  151. public static void main (String[] args) throws java.lang.Exception
  152. {
  153. ISort<String> isort = Factory.getInstance().getInsertionSortAlgorithm();
  154. isort.sort();
  155.  
  156. ISort<String> isort1 = Factory.getInstance().getInternalSortAlgorithm();
  157. isort1.sort();
  158.  
  159. final ITypeSort<String> ITS = (ITypeSort<String>) isort1;
  160. ISortIterator<String> iterator = ITS.getSortIterator();
  161. System.out.println(iterator.hasNext());
  162. }
  163. }
Success #stdin #stdout 0.06s 2184192KB
stdin
Standard input is empty
stdout
----Factory Started----
Insertion Sort
Bubble Sort
Insertion Sort
Merge Sort
true