简单工厂设计模式的核心:把所有的子类当作父类来看待
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleFactoryPattern
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入你需要的笔记本品牌");
//根据用户的输入返回相应的笔记本:父类
string brand = Console.ReadLine();
NoteBook nb = GetNoteBook(brand);
if (nb!=null)
{
nb.SayHello();
}
else
{
Console.WriteLine("没有您需要笔记本");
}
}
static NoteBook GetNoteBook(string brand)
{
NoteBook nb = null;
switch (brand)
{
case "Lenove":
nb = new Lenovo();
break;
case "Acer":
nb = new Acer();
break;
case "IBM":
nb = new IBM();
break;
case "Dell":
nb = new Dell();
break;
}
return nb;
}
}
abstract class NoteBook
{
public abstract void SayHello();
}
class Lenovo : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是Lenovo笔记本");
}
}
class Dell : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是Dell笔记本");
}
}
class Acer : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是Acer笔记本");
}
}
class IBM : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是IBM笔记本");
}
}
}