Factroy pattern (factory mode) of pizza (C # source code)

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Using system. collections;
  6. Namespace Factory
  7. {
  8. Class crunmain
  9. {
  10. Static void main (string [] ARGs)
  11. {
  12. Cpizzastore objfac1 = new cnypizzastore ();
  13. Cpizzastore objfac2 = new cchicagopizzastore ();
  14. Cpizza objpiz1 = objfac1.orderpizza ("Cheese ");
  15. Cpizza objpiz2 = objfac2.orderpizza ("Cheese ");
  16. Console. writeline ("Andy Love Pizza:" + objpiz1.getname ());
  17. Console. writeline ("congfeng love pizza:" + objpiz2.getname ());
  18. }
  19. };
  20. Public abstract class cpizzastore
  21. {
  22. Public cpizzastore ()
  23. {
  24. }
  25. Public cpizza orderpizza (string strtype)
  26. {
  27. Cpizza objpiz;
  28. Objpiz = This. createpizza (strtype );
  29. If (null = objpiz)
  30. {
  31. Return NULL;
  32. }
  33. Console. writeline (objpiz. Prepare ());
  34. Console. writeline (objpiz. Bake ());
  35. Console. writeline (objpiz. Cut ());
  36. Console. writeline (objpiz. Box ());
  37. Return objpiz;
  38. }
  39. Protected abstract cpizza createpizza (string strtype );
  40. };
  41. Public abstract class cpizza
  42. {
  43. Public cpizza ()
  44. {
  45. This. m_arrtoppings = new arraylist ();
  46. }
  47. Public String prepare ()
  48. {
  49. Stringbuilder strbld = new stringbuilder ();
  50. Strbld. append ("prepare" + this. m_strname + "/N ");
  51. Strbld. append ("tossing" + this. m_strdough + "/N ");
  52. Strbld. append ("adding" + this. m_strsauce + "/N ");
  53. Strbld. append ("adding toppings:" + "/N ");
  54. Foreach (string strtop in this. m_arrtoppings)
  55. {
  56. Strbld. append ("/t" + strtop + "/N ");
  57. }
  58. Return strbld. tostring ();
  59. }
  60. Public Virtual string bake ()
  61. {
  62. Return "bake for 25 minutes at 350/N ";
  63. }
  64. Public Virtual string cut ()
  65. {
  66. Return "cutting the pizza Into Diagonal slices! /N ";
  67. }
  68. Public Virtual string box ()
  69. {
  70. Return "place Pizza in official pizzastore box! /N ";
  71. }
  72. Public String getname ()
  73. {
  74. Return this. m_strname;
  75. }
  76. Protected string m_strname;
  77. Protected string m_strdough;
  78. Protected string m_strsauce;
  79. Protected arraylist m_arrtoppings;
  80. };
  81. Public class cnystylecheesepizza: cpizza
  82. {
  83. Public cnystylecheesepizza ()
  84. {
  85. This. m_strname = "NY Style Sauce and cheese pizza ";
  86. This. m_strsauce = "martinara sauce ";
  87. This. m_strdough = "thin crust dough ";
  88. This. m_arrtoppings.add ("greated reggiano cheese ");
  89. }
  90. }
  91. Public class cchicagstylecheesepizza: cpizza
  92. {
  93. Public cchicagstylecheesepizza ()
  94. {
  95. This. m_strdough = "extra thick crust dough ";
  96. This. m_strname = "Chicago style deep dish cheese pizza ";
  97. This. m_strsauce = "shredded Mozzarella cheese ";
  98. }
  99. Public override string cut ()
  100. {
  101. Return "cutting the pizza into square slices/N ";
  102. }
  103. }
  104. Public class cchicagopizzastore: cpizzastore
  105. {
  106. Public cchicagopizzastore ()
  107. {
  108. }
  109. Protected override cpizza createpizza (string strtype)
  110. {
  111. If (strtype. Equals ("Cheese "))
  112. {
  113. Return new cchicagstylecheesepizza ();
  114. }
  115. Return NULL;
  116. }
  117. };
  118. Public class cnypizzastore: cpizzastore
  119. {
  120. Public cnypizzastore ()
  121. {
  122. }
  123. Protected override cpizza createpizza (string strtype)
  124. {
  125. If (strtype. Equals ("Cheese "))
  126. {
  127. Return new cnystylecheesepizza ();
  128. }
  129. Return NULL;
  130. }
  131. }
  132. }
Related Article

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.