Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. // Abstract Products
  2. public interface Letter {
  3. public String getIntro();
  4. public String getBody();
  5. }
  6.  
  7. public interface Resume {
  8. public String getIntro();
  9. public String getBody();
  10. }
  11.  
  12. // Concrete Products for Resume
  13. public class FancyResume implements Resume {
  14. public void getIntro() {
  15. // Get fancy intro here
  16. }
  17. public void getBody() {
  18. // Get fancy body here
  19. }
  20. }
  21.  
  22. public class ModernResume implements Resume {
  23. public void getIntro() {
  24. // Get modern intro here
  25. }
  26. public void getBody() {
  27. // Get modern body here
  28. }
  29. }
  30.  
  31. // Concrete Products for Letter
  32. public class FancyLetter implements Letter {
  33. public void getIntro() {
  34. // Get fancy intro here
  35. }
  36. public void getBody() {
  37. // Get fancy body here
  38. }
  39. }
  40.  
  41. public class ModernLetter implements Letter {
  42. public void getIntro() {
  43. // Get modern intro here
  44. }
  45. public void getBody() {
  46. // Get modern body here
  47. }
  48. }
  49.  
  50. // Abstract Factory
  51. public interface DocumentCreator {
  52. public Letter createLetter();
  53. public Resume cerateResume();
  54. }
  55.  
  56. // Concrete Factory 1
  57. public class FancyDocumentCreator implements DocumentCreator {
  58. // Create fancy documents
  59. public Letter createLetter() {
  60. return new FancyLetter();
  61. }
  62. public Resume createResume() {
  63. return new Resume();
  64. }
  65. }
  66.  
  67. // Concrete Factory 2
  68. public class ModernDocumentCreator implements DocumentCreator {
  69. // Create modern documents
  70. public Letter createLetter() {
  71. return new ModernLetter();
  72. }
  73. public Resume createResume() {
  74. return new ModernResume();
  75. }
  76. }
  77.  
  78. // Client class
  79. public class DocumentBuilder {
  80. public void buildResume(DocumentCreator documentCreator) {
  81. Resume resume = documentCreator.createResume();
  82. // do stuff here presumably
  83. }
  84. public void buildLetter(DocumentCreator documentCreator) {
  85. Letter letter = documentCreator.createLetter();
  86. }
  87. }
  88.  
  89. // Mechanism to build new documents
  90. public class Main {
  91. public static void main(String[] args) {
  92. // Assume first argument is fancy/modern and second is resume/letter
  93. DocumentBuilder builder = new DocumentBuilder();
  94. DocumentCreator docCreator = null;
  95.  
  96. // Specify fancy or modern based on first input
  97. if (args[0].equals("fancy")) {
  98. docCreator = new FancyDocumentCreator();
  99. }
  100. else if (args[0].equals("modern")) {
  101. docCreator = new ModernDocumentCreator();
  102. }
  103. else {
  104. throw new RuntimeException("Make your mind up - fancy or modern?");
  105. }
  106.  
  107. // Specify letter or resume based on second input
  108. if (args[1].equals("resume")) {
  109. docCreator.createResume();
  110. }
  111. else if (args[1].equals("letter")) {
  112. docCreator.createLetter();
  113. }
  114. else {
  115. throw new RuntimeException("Make your mind up - letter or resume?");
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement