嘿嘿,新手参考学习~~~大鸟表笑话俺,哈哈~~using System;namespace DesignPatterns
{
    public interface Fruit //具体产品的抽象
    {
        string Color { get;set;}
    }    public class Orange : Fruit
    {
        private string color;        public Orange(){ }        public string Color
        {
            get { return color; }
            set { color = value; }
        }
    }
    
    public class Apple : Fruit
    {
        private string color;        public Apple() { }        public string Color
        {
            get { return color; }
            set { color = value; }
        }
    }
    public class FruitFactory //工厂类
    {
        /*==========================================================================
         * 注意:这里的name格式为:namespace.class!否则type的返回值一定是null
         *==========================================================================*/
        public static Fruit CreateFruit(string name)
        {
            Fruit fruit = null;
            /*=============================================================================
             * System.Type类:     它对于反射起着核心的作用。我们可以使用 Type 对象的
             *                      方法、 字段、属性和嵌套类来查找有关该类型的所有信息。
             * 
             * System.Activator类:它包含特定的方法,用以在本地或从远程创建对象类型,
             *                      或获取对现有远程对象的引用。
             *=============================================================================*/
            Type type = Type.GetType(name, false);
            if (type != null)
                fruit = (Fruit)Activator.CreateInstance(type);
            return fruit;
        }
    }    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please write the class name that you want to create:");
            string name = Console.ReadLine();
            Console.WriteLine("OK,Try to create the class that named {0}",name);
            Fruit fruit = FruitFactory.CreateFruit(name);
            if (fruit != null)
                Console.WriteLine("Success!the class has been created!");
            else
                Console.WriteLine("Failed!there must be some error!");
        }
    }
}测试时可以输入:DesignPatterns.Apple  or DesignPatterns.Orange然后就可以看见成功的输出了~~!!!

解决方案 »

  1.   

    Red_angelX(八戒) 你真快,呵呵~~~,大虾~~有没有设计模式相关的好文章,推荐一下啦
      

  2.   

    这个例子基本上可以做到OOP设计原则里的:开闭原则(OCP)也就是说如果有新的实体加进来的话,很容易扩展而不用修改已有的实体类呵呵~~我的理解,如果不正确请大虾纠正~~
      

  3.   

    楼主的这个算起来比较简单,下面是Head first design pattern里面的例子:
    public class Pizza
    {
       public Pizza orderPizza(){
          Pizza pizza = createPizza("chineese");
          .............................
          return pizza;
       }   public abstract Pizza createPizza(string pizzatype); //工厂,更高度的抽象,由子类实现更利于扩展。适用于某一系列的对象,对于新的对象则开发新的子类
    }
      

  4.   


    好像在51aspx上见过完整源码,记不大清楚了
      

  5.   

    谢谢哥们czhenq(...... fucking life. I want to change.)的补充~~~您说的应该是设计模式中的最难的一种了:抽象工厂模式~~目前还未能完全领会,所以不敢乱贴~~呵呵~~~~~努力学习中ing
      

  6.   

    cfreez(回再少帖子也有人不揭) 谢哥们教诲,在您看来这也许不神秘但对于我们这样的新手看到这样的代码,也许会像"酒鬼第一次喝到茅台,怎一个'爽'字了得".呵呵~~
      

  7.   

    TO:seesea125(执著)哦~~TKS FOR YOUR HELP那一般大型的项目是不会采用这种方式也实例化一个对象了?
      

  8.   

    一些持久化框架和容器 采用类似的做法
    一般+ singleton第一次加载时略慢(比不反射)
    以后都很方便反射工厂的优点:可以动态配置,低耦合
    ------------------------------------------可以看看Spring.net
      

  9.   

    我觉得如果反射加泛型一起结合写项目架构的时候要同意BEM层就牛了 别传递参数