C#面向对象17 23种设计模式

1.简单工厂模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂
{
    class Program
    {
        static void Main(string[] args)
        {
            string tt= Console.ReadLine();
            Notabook tb = Getbook(tt);
            tb.Say();
            Console.ReadKey();
        }

        /// <summary>
        /// 简单工厂核心,根据输入 创建对象赋值给父类~
        /// </summary>
        /// <param name="style"></param>
        /// <returns></returns>
        public static Notabook Getbook(string style)
        {
            Notabook tb = null;
            switch(style)
            {
                case "lenvon":
                    tb=new lenvon();
                    break;
                case "IBM":
                    tb = new IBM();
                    break;
                case "DELL":
                    tb = new DELL();
                    break;
            }
            return tb;
        }
    }

    public abstract class Notabook
    {
        public abstract void Say();
    }

    public class lenvon: Notabook
    {
        public override void Say()
        {
            Console.WriteLine("this is lenvon!");
        }
    }

    public class IBM:Notabook
    {
        public override void Say()
        {
            Console.WriteLine("this is IBM!");
        }
    }

    public class DELL:Notabook
    {
        public override void Say()
        {
            Console.WriteLine("this is DELL!");
        }
    }
}

 

posted @ 2018-03-28 10:06  youguess  阅读(176)  评论(0编辑  收藏  举报