Design Pattern---simple factory

Source: Internet
Author: User

Simple Factory mode: The store---a lot of pastries, you need to build through the factory. The goal is to reduce the code by first writing a parent class, then having the subclass inherit the parent class, and then writing a factory class that invokes different subclasses depending on the switch.

When testing, call the factory class directly.

Directly on the code:

The following algorithm is an implementation of a calculator:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. ?
  6. namespace Designdemo
  7. {
  8. ???? class Operation2
  9. ???? {
  10. ???????? Private double _numbera;
  11. ???????? Private double _numberb;
  12. ?
  13. ???????? Public Double Numbera
  14. ???????? {
  15. ???????????? Get {return _numbera;}
  16. ???????????? Set {_numbera = value;}
  17. ????????}
  18. ?
  19. ???????? Public Double Numberb
  20. ???????? {
  21. ???????????? Get { return _numberb;}
  22. ???????????? Set {_numberb = value;}
  23. ????????}
  24. ?
  25. ???????? Public Virtual Double GetResult ()
  26. ???????? {
  27. ???????????? double result = 0d;
  28. ???????????? return result;
  29. ????????}
  30. ????}
  31. ?
  32. ???? class Operationadd:operation2
  33. ???? {
  34. ???????? Public Override Double GetResult ()
  35. ???????? {
  36. ???????????? double result = 0;
  37. ???????????? result = Numbera + Numberb;
  38. ???????????? return result;
  39. ????????}
  40. ????}
  41. ?
  42. ???? class Operationsub:operation2
  43. ???? {
  44. ???????? Public Override Double GetResult ()
  45. ???????? {
  46. ???????????? double result = 0;
  47. ???????????? result = Numbera-numberb;
  48. ???????????? return result;
  49. ????????}
  50. ????}
  51. ?
  52. ???? class Operationmul:operation2
  53. ???? {
  54. ???????? Public Override Double GetResult ()
  55. ???????? {
  56. ???????????? double result = 0;
  57. ???????????? result = Numbera * NUMBERB;
  58. ???????????? return result;
  59. ????????}
  60. ????}
  61. ?
  62. ???? class Operationdiv:operation2
  63. ???? {
  64. ???????? Public Override Double GetResult ()
  65. ???????? {
  66. ???????????? double result = 0;
  67. ???????????? if (Numberb = = 0)
  68. ???????????? {
  69. ???????????????? Throw New Exception (" divisor cannot be 0");
  70. ????????????}
  71. ???????????? result = Numbera/numberb;
  72. ???????????? return result;
  73. ????????}
  74. ????}
  75. ?
  76. ?
  77. ???? /************************************************************************/
  78. ???? /* Build a simple factory model: reduce coupling, encapsulate changing things into classes */
  79. ???? /************************************************************************/
  80. ?
  81. ???? class Operationfactory
  82. ???? {
  83. ???????? Public Static Operation2 createoperate (string operate)
  84. ???????? {
  85. ???????????? Operation2 per = null;
  86. ???????????? Switch (operate)
  87. ???????????? {
  88. ???????????????? Case "+":
  89. ???????????????????? per = new operationadd ();
  90. ???????????????????? break;
  91. ???????????????? Case "-":
  92. ???????????????????? per = new operationsub ();
  93. ???????????????????? break;
  94. ???????????????? Case "*":
  95. ???????????????????? per = new Operationmul ();
  96. ???????????????????? break;
  97. ???????????????? Case "/":
  98. ???????????????????? per = new operationdiv ();
  99. ???????????????????? break;
  100. ????????????}
  101. ???????????? return per;
  102. ????????}
  103. ????}
  104. }

Design Pattern---simple factory

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.