C#简单工厂案例

using System;
namespace Application
{
    class JianDanGongChang
    {
        static void Main(string[] args)
        {
            Factory factory=new Factory();
            DianNao diannao=factory.Create("Lx");
            diannao.Print();
        }
    }
    
    public abstract class DianNao
    {
        public abstract void Print();
    }
    
    public class LianXiang:DianNao
    {
        public override void Print()
        {
            Console.WriteLine("联想电脑");
        }
    }
    
    public class DaiEr:DianNao
    {
        public override void Print()
        {
            Console.WriteLine("戴尔电脑");
        }
    }
    //创建一个工厂类
    public class Factory
    {
        private DianNao dianNao;
        //创建一个工厂方法
        public DianNao Create(string diannao){
            switch(diannao)
            {
                case "Lx":dianNao=new LianXiang();
                break;
                case "De":dianNao=new DaiEr();
                break;
                default:
                break;
            }
            return dianNao;
        }
    }
    
}

 

posted @ 2019-08-12 11:47 *Dx 阅读(...) 评论(...) 编辑 收藏