简单工厂模式小例子 写的测试过了

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 笔记本品牌简单工厂模式
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("请输入电脑的品牌:");
14             string brand = Console.ReadLine();
15             NoteBook note = Factory.SelectNoteBook(brand);
16             if (note != null)
17             {
18                 note.GetBrand();
19             }
20             else
21             {
22                 Console.WriteLine("没有该品牌...");
23             }
24             Console.ReadKey();
25 
26         }
27     }
28 
29     abstract class NoteBook
30     {
31         public abstract void GetBrand();
32     }
33 
34     class Lenevo:NoteBook
35     {
36         public override void GetBrand()
37         {
38             Console.WriteLine("我是联想电脑");
39         }
40     }
41     class Acer : NoteBook
42     {
43         public override void GetBrand()
44         {
45             Console.WriteLine("我是宏碁电脑");
46         }
47     }
48 
49     class Factory
50     {
51         //在这里实现了简单工厂模式  需要一个父类对象,实例化子类对象,返回父类对象
52         public static NoteBook SelectNoteBook(string brand)
53         { 
54             NoteBook note = null;
55             switch (brand)
56             {
57                 case "联想":
58                     note = new Lenevo();
59                     break;
60                 case "宏碁":
61                     note = new Acer();
62                     break;
63                 default:
64                     break;
65             }
66             return note;
67         }
68     }
69
70 }


这是我刚学习的,本文如果有什么错误欢迎提出来。谢谢大家的赐教啦...

 

posted @ 2015-01-07 21:46  zhuanglijie  阅读(418)  评论(1编辑  收藏  举报