码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式---简单工厂

时间:2015-09-05 16:13:57      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

简单工厂模式:就是店---很多糕点,你需要通过工厂来生成。目的是为了减少代码,先写一个父类,然后让子类继承这个父类,然后在写一个工厂类,根据switch来调用不同的子类。

测试的时候,直接调用工厂类即可。

直接上代码:

下面的算法是一个 计算器的实现:

  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("除数不能为0");
  70. ????????????}
  71. ????????????result = numberA / numberB;
  72. ????????????return result;
  73. ????????}
  74. ????}
  75. ?
  76. ?
  77. ????/************************************************************************/
  78. ????/* 建立简单工厂模式:减少耦合,把变化的东西封装成类 */
  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. }

设计模式---简单工厂

标签:

原文地址:http://www.cnblogs.com/zhuxuekui/p/4783305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!