1. //Abstract factory
    2. abstract class ComputerFactory
    3. {
    4. public abstract Computer BuildComputer(Computer.ComputerType compType);
    5. }
    6. //Concrete factory
    7. class Dell : ComputerFactory
    8. {
    9. public override Computer BuildComputer(Computer.ComputerType compType)
    10. {
    11. if (compType == Computer.ComputerType.xps)
    12. return (new xps());
    13. else if (compType == Computer.ComputerType.inspiron)
    14. return new inspiron();
    15. else
    16. return null;
    17. }
    18. }
    19. //Concrete factory
    20. class Hp : ComputerFactory
    21. {
    22. public override Computer BuildComputer(Computer.ComputerType compType)
    23. {
    24. if (compType == Computer.ComputerType.envoy)
    25. return (new envoy());
    26. else if (compType == Computer.ComputerType.presario)
    27. return new presario();
    28. else
    29. return null;
    30. }
    31. }
    32. //Abstract product
    33. public abstract class Computer
    34. {
    35. public abstract string Mhz { get; set; }
    36. public enum ComputerType
    37. {
    38. xps,inspiron,envoy,presario
    39. }
    40. }
    41. //Concrete product for DELL
    42. public class xps : Computer
    43. {
    44. string _mhz = string.Empty;
    45. public override string Mhz
    46. {
    47. get
    48. {
    49. return _mhz;
    50. }
    51. set
    52. {
    53. _mhz = value;
    54. }
    55. }
    56. }
    57. //Concrete product for DELL
    58. public class inspiron : Computer
    59. {
    60. string _mhz = string.Empty;
    61. public override string Mhz
    62. {
    63. get
    64. {
    65. return _mhz;
    66. }
    67. set
    68. {
    69. _mhz = value;
    70. }
    71. }
    72. }
    73. //Concrete product for HP
    74. public class envoy : Computer
    75. {
    76. string _mhz = string.Empty;
    77. public override string Mhz
    78. {
    79. get
    80. {
    81. return _mhz;
    82. }
    83. set
    84. {
    85. _mhz = value;
    86. }
    87. }
    88. }
    89. //Concrete product for HP
    90. public class presario : Computer
    91. {
    92. string _mhz = string.Empty;
    93. public override string Mhz
    94. {
    95. get
    96. {
    97. return _mhz;
    98. }
    99. set
    100. {
    101. _mhz = value;
    102. }
    103. }
    104. }
    105. public class BestBuy
    106. {
    107. ComputerFactory compFactory;
    108. Computer comp;
    109. public BestBuy(Computer.ComputerType compType)
    110. {
    111. if (compType == Computer.ComputerType.xps || compType == Computer.ComputerType.inspiron)
    112. compFactory = new Dell();
    113. else
    114. compFactory = new Hp();
    115. comp = compFactory.BuildComputer(compType);
    116. }
    117. public Computer Sell()
    118. {
    119. return comp;
    120. }
    121. }