标签:
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 Program 10 { 11 static void Main(string[] args) 12 { 13 Console.WriteLine("请输入您想要的笔记本品牌"); 14 string brand = Console.ReadLine(); 15 NoteBook nb = GetNoteBook(brand); 16 nb.SayHello(); 17 Console.ReadKey(); 18 } 19 20 21 /// <summary> 22 /// 简单工厂的核心 根据用户的输入创建对象赋值给父类 23 /// </summary> 24 /// <param name="brand"></param> 25 /// <returns></returns> 26 public static NoteBook GetNoteBook(string brand) 27 { 28 NoteBook nb = null; 29 switch (brand) 30 { 31 case "Lenovo": nb = new Lenovo(); 32 break; 33 case "IBM": nb = new IBM(); 34 break; 35 case "Acer": nb = new Acer(); 36 break; 37 case "Dell": nb = new Dell(); 38 break; 39 } 40 return nb; 41 } 42 } 43 44 public abstract class NoteBook 45 { 46 public abstract void SayHello(); 47 } 48 49 public class Lenovo : NoteBook 50 { 51 public override void SayHello() 52 { 53 Console.WriteLine("我是联想笔记本,你联想也别想"); 54 } 55 } 56 57 58 public class Acer : NoteBook 59 { 60 public override void SayHello() 61 { 62 Console.WriteLine("我是鸿基笔记本"); 63 } 64 } 65 66 public class Dell : NoteBook 67 { 68 public override void SayHello() 69 { 70 Console.WriteLine("我是戴尔笔记本"); 71 } 72 } 73 74 public class IBM : NoteBook 75 { 76 public override void SayHello() 77 { 78 Console.WriteLine("我是IBM笔记本"); 79 } 80 } 81 }
标签:
原文地址:http://www.cnblogs.com/liuslayer/p/4478369.html