设计模式
设计这个项目的一种方式。
简单工厂设计模式
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _03简单工厂设计模式 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 Console.WriteLine("请输入电脑的品牌");14 string input = Console.ReadLine();15 NoteBook nb = GetComupter(input);16 nb.SayHello();17 Console.ReadKey();18 }19 20 ///21 /// 简单工厂设计模式,根据用户的输入来返回一个父类,里面装的是个子类对象22 /// 23 /// 24 ///25 public static NoteBook GetComupter(string name)26 {27 NoteBook nb = null;28 switch (name)29 {30 case "戴尔":31 nb = new Dell();32 break;33 case "IBM":34 nb = new IBM();35 break;36 case "宏基":37 nb = new Acer();38 break;39 case "联想":40 nb = new Lenovo();41 break;42 }43 44 return nb;45 }46 }47 public abstract class NoteBook48 {49 public abstract void SayHello();50 }51 52 public class Lenovo : NoteBook53 {54 public override void SayHello()55 {56 Console.WriteLine("我是联想电脑");57 }58 }59 60 public class Acer : NoteBook61 {62 public override void SayHello()63 {64 Console.WriteLine("我是宏基电脑");65 }66 }67 68 public class Dell : NoteBook69 {70 public override void SayHello()71 {72 Console.WriteLine("我是戴尔电脑");73 }74 }75 76 public class IBM : NoteBook77 {78 public override void SayHello()79 {80 Console.WriteLine("我是IBM电脑");81 }82 }83 84 85 }