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("請輸入你需要的筆記本品牌");
//根據(jù)用戶的輸入返回相應(yīng)的筆記本:父類
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筆記本");
}
}
}