其他分享
首页 > 其他分享> > 知识基础 -> 简单工厂设计模式

知识基础 -> 简单工厂设计模式

作者:互联网

using System;

namespace 简单工厂设计模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入你想要哪个笔记本: ");
            string brand = Console.ReadLine();
            NoteBook nb = GetBoteBook(brand);
            nb.SayHello();
            Console.ReadKey();
        }

        /// <summary>
        /// 简单工厂的核心
        /// </summary>
        public static NoteBook GetBoteBook(string brand)
        {
            NoteBook nb = null;
            switch (brand)
            {
                case "Lenovo":
                    nb = new Lenovo();
                    break;
                case "IBM":
                    nb = new IBM();
                    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 Lenovo : NoteBook
    {
        public override void SayHello()
        {
            Console.WriteLine("我是联想笔记本,你想都别想");
        }
    }

    public class Acer : NoteBook
    {
        public override void SayHello()
        {
            Console.WriteLine("我是鸿基笔记本");
        }
    }

    public class Dell : NoteBook
    {
        public override void SayHello()
        {
            Console.WriteLine("我是戴尔笔记本");
        }
    }

    public class IBM : NoteBook
    {
        public override void SayHello()
        {
            Console.WriteLine("我是IBM笔记本");
        }
    }
}

 

标签:Console,nb,知识,工厂,class,NoteBook,void,设计模式,public
来源: https://www.cnblogs.com/chengxunyuanxiaolv/p/16398621.html