Abstract Factory Summary

Source: Internet
Author: User

Encapsulate a series of objects (dependencies exist in these series of objects). If these objects have multiple styles that need to be extended, the factory must be abstracted. If the style is not extended, but there are multiple implementations of this series of objects, there is no abstract factory, which is called a static factory.

In this way, we can deal with the extension of this series of object styles without changing the call points.

However, if the change point is on the number of objects in this series, the abstract factory is not suitable for this

  1. Package CN. Zhao. Moshi. abstractfactory;
  2. Public class fugugamefactory extends gamefactory
  3. {
  4. @ Override
  5. Public iabstractbullet getbullet ()
  6. {
  7. Return new impl54bullet ();
  8. }
  9. @ Override
  10. Public iabstracthouse gethouse ()
  11. {
  12. Return new implcottagehouse ();
  13. }
  14. @ Override
  15. Public iw.actroad getroad ()
  16. {
  17. Return new implspiralroad ();
  18. }
  19. }
  20. Package CN. Zhao. Moshi. abstractfactory;
  21. Public class gameclient
  22. {
  23. Private gamefactory gamefact = NULL;
  24. Public gameclient (gamefactory fact)
  25. {
  26. This. gamefact = fact;
  27. }
  28. Public void paly ()
  29. {
  30. Iabstractbullet butt = This. gamefact. getbullet ();
  31. Iabstracthouse Hou = This. gamefact. gethouse ();
  32. Iw.actroad road = This. gamefact. getroad ();
  33. Butt. Shoot ();
  34. Hou. opendoor ();
  35. Road. drawroad ();
  36. }
  37. Public static void main (string [] ARGs)
  38. {
  39. Gameclient game = new gameclient (New fugugamefactory ());
  40. Game. paly ();
  41. Gameclient game2 = new gameclient (New xiandaifugugamefactory ());
  42. Game2.paly ();
  43. }
  44. }
  45. Package CN. Zhao. Moshi. abstractfactory;
  46. Public abstract class gamefactory
  47. {
  48. Public abstract imo-actbullet getbullet ();
  49. Public abstract imo-acthouse gethouse ();
  50. Public abstract iw.actroad getroad ();
  51. }
  52. Package CN. Zhao. Moshi. abstractfactory;
  53. /**
  54. * Abstract bullet
  55. **/
  56. Public interface iabstractbullet
  57. {
  58. /**
  59. * Launch
  60. **/
  61. Void shoot ();
  62. }
  63. Package CN. Zhao. Moshi. abstractfactory;
  64. /**
  65. * Abstract house
  66. **/
  67. Public interface imo-acthouse
  68. {
  69. /**
  70. * Open the door
  71. **/
  72. Void opendoor ();
  73. }
  74. Package CN. Zhao. Moshi. abstractfactory;
  75. Public interface iabstractroad
  76. {
  77. /**
  78. * Draw a path
  79. **/
  80. Void drawroad ();
  81. }
  82. Package CN. Zhao. Moshi. abstractfactory;
  83. /**
  84. * 54 bullets
  85. **/
  86. Public class impl54bullet implements imo-actbullet
  87. {
  88. Public void shoot ()
  89. {
  90. System. Out. println ("fired 54 bullets in a normal style ");
  91. }
  92. }
  93. Package CN. Zhao. Moshi. abstractfactory;
  94. /**
  95. * Rural small houses
  96. **/
  97. Public class implcottagehouse implements imo-acthouse
  98. {
  99. Public void opendoor ()
  100. {
  101. System. Out. println ("opening a small rural apartment in common style ");
  102. }
  103. }
  104. Package CN. Zhao. Moshi. abstractfactory;
  105. /**
  106. * 54 Modern bullets
  107. **/
  108. Public class implmoden54bullet implements imo-actbullet
  109. {
  110. Public void shoot ()
  111. {
  112. System. Out. println ("fired 54 bullets in a modern style ");
  113. }
  114. }
  115. Package CN. Zhao. Moshi. abstractfactory;
  116. /**
  117. * Modern rural small houses
  118. **/
  119. Public class implmodencottagehouse implements imo-acthouse
  120. {
  121. Public void opendoor ()
  122. {
  123. System. Out. println ("small rural apartment opens in modern style ");
  124. }
  125. }
  126. Package CN. Zhao. Moshi. abstractfactory;
  127. /**
  128. * Modern winding road
  129. **/
  130. Public class implmodenspiralroad implements imo-actroad
  131. {
  132. Public void drawroad ()
  133. {
  134. System. Out. println ("modern style hovering path extended ");
  135. }
  136. }
  137. Package CN. Zhao. Moshi. abstractfactory;
  138. /**
  139. * Hovering path
  140. **/
  141. Public class implspiralroad implements imo-actroad
  142. {
  143. Public void drawroad ()
  144. {
  145. System. Out. println ("the general style circled path is extended ");
  146. }
  147. }
  148. Package CN. Zhao. Moshi. abstractfactory;
  149. /**
  150. * Modern style factory implementation class
  151. **/
  152. Public class xiandaifugugamefactory extends gamefactory
  153. {
  154. @ Override
  155. Public iabstractbullet getbullet ()
  156. {
  157. Return new implmoden54bullet ();
  158. }
  159. @ Override
  160. Public iabstracthouse gethouse ()
  161. {
  162. Return new implmodencottagehouse ();
  163. }
  164. @ Override
  165. Public iw.actroad getroad ()
  166. {
  167. Return new implmodenspiralroad ();
  168. }
  169. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.