使用Activator.CreateInstance完善简单工厂

来源:互联网 发布:matlab 2016b mac安装 编辑:程序博客网 时间:2024/06/08 08:14
         

     前几天在项目中看到别人的工厂类使用Activator.CreateInstance() 之前用简单工厂都是用switch ...case ,之前没有用过便查了查资料,正是这个方法+ 反射简化了工厂模式,在需求增加的情况可以不动工厂类和客户端, 也符合开放封闭原则。  反射的地方用的不多, 后续会再加上一些例子。

     

     

     

     

    [c-sharp] view plaincopyprint?
    1. public  interface IFruit  
    2. {  
    3.           
    4. }  
    5. public class Apple:IFruit  
    6. {  
    7.         public Apple()  
    8.         {  
    9.             Console.WriteLine("An apple is got!");  
    10.         }  
    11. }  
    12.   
    13. public class Orange:IFruit  
    14.     {  
    15.         public Orange()  
    16.         {  
    17.             Console.WriteLine("An orange is get!");  
    18.         }  
    19.     }  
    20.   
    21. public class FruitFactory  
    22.     {  
    23.         public IFruit MakeFruit(String name)  
    24.         {  
    25.             ////switch (name)  
    26.             ////{   
    27.             ////    case "Orange":  
    28.             ////        return new Orange();  
    29.             ////        break;  
    30.             ////    case "Apple":  
    31.             ////        return new Apple();  
    32.             ////        break;  
    33.             ////    default:  
    34.             ////        return null;  
    35.             ////}  
    36.             IFruit MyFruit = null;  
    37.             try  
    38.             {  
    39.   
    40.                 var assembly = Assembly.GetExecutingAssembly();  
    41.                 var types = assembly.GetTypes();  
    42.                   
    43.                 foreach (var type in types)  
    44.                 {  
    45.                     if (type.Name == name)  
    46.                     {  
    47.                         Type t = Type.GetType(type.ToString());  
    48.                         MyFruit =Activator.CreateInstance(t) as IFruit;  
    49.                     }  
    50.                 }  
    51.                  
    52.             }  
    53.             catch (Exception ex)  
    54.             {  
    55.                 Console.WriteLine(ex.Message);   
    56.             }  
    57.             return MyFruit;  
    58.               
    59.               
    60.         }  
    61.     }  
    62. }  
    63.   
    64. -----------------------------------------  
    65.   
    66. class Program  
    67.     {  
    68.         static void Main(string[] args)  
    69.         {  
    70.             String fruitName = Console.ReadLine();  
    71.             Console.WriteLine("you need to get {0}", fruitName);  
    72.             IFruit fruit = null;  
    73.             FruitFactory factory = new FruitFactory();  
    74.             fruit = factory.MakeFruit(fruitName);  
    75.               
    76.       
    77.             Console.Read();  
    78.         }  
    79.           
    80.     }  
    0 0