image.png

    1. using System;
    2. namespace _086_简单工厂设计模式
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. Console.WriteLine("请输入你想要的笔记本");
    9. string brand = Console.ReadLine();
    10. NoteBook nb = GetNoteBook(brand);
    11. nb.SayHello();
    12. Console.ReadKey();
    13. }
    14. /// <summary>
    15. /// 简单工厂的核心,根据用户的输入创建对象赋值给父类
    16. /// </summary>
    17. /// <param name="brand">用户的输入</param>
    18. /// <returns>父类</returns>
    19. public static NoteBook GetNoteBook(string brand)
    20. {
    21. NoteBook nb = null;
    22. switch (brand)
    23. {
    24. case "Lenovo":
    25. nb = new Lenovo();
    26. break;
    27. case "Acer":
    28. nb = new Acer();
    29. break;
    30. case "Dell":
    31. nb = new Dell();
    32. break;
    33. case "IBM":
    34. nb = new IBM();
    35. break;
    36. default:
    37. break;
    38. }
    39. return nb;
    40. }
    41. }
    42. }
    43. using System;
    44. using System.Collections.Generic;
    45. using System.Text;
    46. namespace _086_简单工厂设计模式
    47. {
    48. abstract class NoteBook
    49. {
    50. public abstract void SayHello();
    51. }
    52. }
    53. using System;
    54. using System.Collections.Generic;
    55. using System.Text;
    56. namespace _086_简单工厂设计模式
    57. {
    58. class Lenovo : NoteBook
    59. {
    60. public override void SayHello()
    61. {
    62. Console.WriteLine("我是联想笔记本");
    63. }
    64. }
    65. }
    66. using System;
    67. using System.Collections.Generic;
    68. using System.Text;
    69. namespace _086_简单工厂设计模式
    70. {
    71. class Acer : NoteBook
    72. {
    73. public override void SayHello()
    74. {
    75. Console.WriteLine("我是鸿基笔记本");
    76. }
    77. }
    78. }
    79. using System;
    80. using System.Collections.Generic;
    81. using System.Text;
    82. namespace _086_简单工厂设计模式
    83. {
    84. class Dell : NoteBook
    85. {
    86. public override void SayHello()
    87. {
    88. Console.WriteLine("我是戴尔笔记本");
    89. }
    90. }
    91. }
    92. using System;
    93. using System.Collections.Generic;
    94. using System.Text;
    95. namespace _086_简单工厂设计模式
    96. {
    97. class IBM : NoteBook
    98. {
    99. public override void SayHello()
    100. {
    101. Console.WriteLine("我是IBM笔记本");
    102. }
    103. }
    104. }