1、简单工厂

 1、简单工厂 (调用静态方法实例化需要的类)

 

   class Program
    {
        static void Main(string[] args)
        {

 //1、简单工厂
            IMan man = Factory.Create(FactoryType.Student);
            man.Go();


        }
    }


 public class Factory
    {
        public static IMan Create(FactoryType factoryType)
        {

            switch (factoryType)
            {
                case 0:
                    return new Student();
                       
                default:
                    return new Teacher();
            }

        }
    }

    public enum FactoryType
    {
        Student =0,
        Teacher =1
    }

 public class Student:IMan
    {
        public void Go()
        {
            Console.WriteLine("Student go !");
        }
         
    }

   public class Teacher : IMan
    {
        public void Go()
        {
            Console.WriteLine("Teacher go !");
        }

    }

 

posted @ 2021-12-21 14:09  艾特-天空之海  阅读(23)  评论(0编辑  收藏  举报