设计模式
设计这个项目的一种方式。
using System;
namespace 简单工厂模式
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入想要的笔记本");
string brand = Console.ReadLine();
NoteBook nb = GetNoteBook(brand);
nb.SayHello();
}
public static NoteBook GetNoteBook(string brand)
{
NoteBook nb = null;
switch (brand)
{
case "Lenovo": nb = new Lenovo();
break;
case "Acer": nb = new Acer();
break;
case "Dell": nb = new Dell();
break;
}
return nb;
}
public abstract class NoteBook
{
public abstract void SayHello();
}
public class Dell:NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是戴尔笔记本");
}
}
public class Lenovo:NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是联想笔记本");
}
}
public class Acer:NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是宏碁笔记本");
}
}
}
}