Decorator pattern Starbucks coffee (C # source code)

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace Decker
  6. {
  7. Class crunmain
  8. {
  9. Static void main (string [] ARGs)
  10. {
  11. Cbeverage objbev = new cespresso ();
  12. Cbeverage objbev2 = NULL;
  13. Console. writeline (objbev. getdescription () + "$" + objbev. Cost ());
  14. Objbev2 = new chouseblend ();
  15. Objbev2 = new cmocha (objbev2 );
  16. Objbev2 = new cmocha (objbev2 );
  17. Objbev2 = new cmocha (objbev2 );
  18. Console. writeline (objbev2.getdescription () + "$" + objbev2.cost ());
  19. }
  20. };
  21. // Beverage
  22. Public abstract class cbeverage
  23. {
  24. Public Virtual string getdescription ()
  25. {
  26. Return this. m_strdescription;
  27. }
  28. Public abstract double cost ();
  29. Protected string m_strdescription;
  30. };
  31. Public abstract class ccondimentdecortor: cbeverage
  32. {
  33. Public abstract string getdescription ();
  34. };
  35. Public class cespresso: cbeverage
  36. {
  37. Public cespresso ()
  38. {
  39. This. m_strdescription = "Espresso ";
  40. }
  41. Public override double cost ()
  42. {
  43. Return 1.99;
  44. }
  45. };
  46. Public class chouseblend: cbeverage
  47. {
  48. Public chouseblend ()
  49. {
  50. This. m_strdescription = "house blend coffee ";
  51. }
  52. Public override double cost ()
  53. {
  54. Return 0.89;
  55. }
  56. };
  57. // Seasoning
  58. Public class cmocha: ccondimentdecortor
  59. {
  60. Public cmocha (cbeverage objbev)
  61. {
  62. This. m_objbev = objbev;
  63. This. m_strdescription = This. getdescription ();
  64. }
  65. Public override string getdescription ()
  66. {
  67. Return this. m_objbev.getdescription () + ", mocha ";
  68. }
  69. Public override double cost ()
  70. {
  71. If (null = This. m_objbev)
  72. {
  73. Throw new exception ("null = OBJ ");
  74. }
  75. Return 0.2 + this. m_objbev.cost ();
  76. }
  77. Private cbeverage m_objbev;
  78. };
  79. }

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.