我如何从任何工厂返回抽象类?

时间:2010-04-17 11:29:56

标签: c# visual-studio design-patterns architecture oop

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EfTestFactory
{
    public abstract class _Company
    {
        public abstract List<Personel> GetPersonel();

        public abstract List<Prim> GetPrim();

        public abstract List<Finans> GetFinans();
    }

    public abstract class _Radyoloji
    {
        public abstract List<string> GetRadyoloji();
    }
    public abstract class _Satis
    {
        public abstract List<string> GetSatis();
    }
    public abstract class _Muayene
    {
        public abstract List<string> GetMuayene();
    }

    public class Company : _Company
    {

        public override List<Personel> GetPersonel()
        {
            throw new NotImplementedException();
        }

        public override List<Prim> GetPrim()
        {
            throw new NotImplementedException();
        }

        public override List<Finans> GetFinans()
        {
            throw new NotImplementedException();
        }
    }

    public class Radyoloji : _Radyoloji
    {
        public override List<string> GetRadyoloji()
        {
            throw new NotImplementedException();
        }
    }

    public class Satis : _Satis
    {
        public override List<string> GetSatis()
        {
            throw new NotImplementedException();
        }
    }

    public class Muayene : _Muayene
    {
        public override List<string> GetMuayene()
        {
            throw new NotImplementedException();
        }
    }



    public class GenoTipController
    {
        public object CreateByEnum(DataModelType modeltype)
        {
            string enumText = modeltype.ToString(); // will return for example "Company"
            Type classType = Type.GetType(enumText); // the Type for Company class
            object t = Activator.CreateInstance(classType); // create an instance of Company class
            return t;
        }
    }

    public class AntsController
    {
        static Dictionary<DataModelType, Func<object>> s_creators =
            new Dictionary<DataModelType, Func<object>>()
            {
                { DataModelType.Radyoloji,  () => new _Radyoloji() },
                { DataModelType.Company,    () => new _Company() },
                { DataModelType.Muayene,    () => new _Muayene() },
                { DataModelType.Satis,      () => new _Satis() },
            };

        public object CreateByEnum(DataModelType modeltype)
        {
            return s_creators[modeltype]();
        }
    }

   public class CompanyView
    {
        public static List<Personel> GetPersonel()
        {
            GenoTipController controller = new GenoTipController();
            _Company company = controller.CreateByEnum(DataModelType.Company) as _Company;
            return company.GetPersonel();
        }
    }

    public enum DataModelType
    {
        Radyoloji,
        Satis,
        Muayene,
        Company
    }
}

如果我写上面的代码,我会看到一些错误:无法创建抽象类或接口的实例'EfTestFactory_Company'我该如何解决?请看下面的图片。alt text http://i44.tinypic.com/33wa8tc.png

2 个答案:

答案 0 :(得分:3)

关键字abstract的目的是使一个类不可实例化。如果你已经声明了一个类抽象而你想要实例化它,那么你就有一个设计缺陷。

答案 1 :(得分:2)

您无法实例化抽象类。您只能实例化具体的类:

static Dictionary<DataModelType, Func<object>> s_creators =
    new Dictionary<DataModelType, Func<object>>()
    {
        { DataModelType.Radyoloji,  () => new Radyoloji() },
        { DataModelType.Company,    () => new Company() },
        { DataModelType.Muayene,    () => new Muayene() },
        { DataModelType.Satis,      () => new Satis() },
    };