有一点是可能清楚的是,你的设计是有点瑕疵。打开类型并不是在通用方法中做的最好的事情,它违背了它的目的。但是不清楚的是你的课程的目的是什么。
的一些思考:
1)查看你对类AskItem
和AskTankTruckAggregate<T>
等我不认为后者是一个通用类,它是一个非常特殊的类,紧耦合AskItem
。我会重新设计它像
public static class AbstractAggregateFactory
{
public static AbstractAggregate<T> GetAggregateClient<T>() where T : ListItem
{
//use reflection to find the type that inherits AbstractAggregate<T>
//instantiate the type
//cast to AbstractAggregate<T> and return
}
}
public class AskTankTruckAggregate : AbstractAggregate<AskItem>
{
//not implemented yet
}
public class TankTruckBlogAggregate : AbstractAggregate<BlogItem>
{
//not implemented yet
}
public class ResourcesAggregate : AbstractAggregate<ResourceItem>
{
//not implemented yet
}
这样称呼它:
AbstractAggregateFactory.GetAggregateClient<AskItem>(); //etc
2)另一种方式:委托总就业创造你的listItems中。
public abstract class ListItem //or interface
{
protected abstract object Create();
}
public class AskItem : ListItem { //implement to return AskTankTruckAggregate
}
public class BlogItem : ListItem { //implement to return TankTruckBlogAggregate
}
public class ResourceItem : ListItem { //implement to return ResourcesAggregate
}
public static class AbstractAggregateFactory
{
public static AbstractAggregate<T> GetAggregateClient<T>() where T : ListItem, new()
{
return (AbstractAggregate<T>)new T().Create();
}
}
public class AskTankTruckAggregate : AbstractAggregate<AskItem>
{
//not implemented yet
}
public class TankTruckBlogAggregate : AbstractAggregate<BlogItem>
{
//not implemented yet
}
public class ResourcesAggregate : AbstractAggregate<ResourceItem>
{
//not implemented yet
}
这样称呼它:
AbstractAggregateFactory.GetAggregateClient<AskItem>(); //etc
3)或相同的,但要多一点强类型,使用泛型:
public abstract class ListItem<T> where T : ListItem<T> //or interface
{
protected abstract AbstractAggregate<T> Create();
}
public class AskItem : ListItem<AskItem> { //implement to return AskTankTruckAggregate
}
public class BlogItem : ListItem<BlogItem> { //implement to return TankTruckBlogAggregate
}
public class ResourceItem : ListItem<ResourceItem> { //implement to return ResourcesAggregate
}
public static class AbstractAggregateFactory
{
public static AbstractAggregate<T> GetAggregateClient<T>() where T : ListItem, new()
{
return new T().Create();
}
}
public class AskTankTruckAggregate : AbstractAggregate<AskItem>
{
//not implemented yet
}
public class TankTruckBlogAggregate : AbstractAggregate<BlogItem>
{
//not implemented yet
}
public class ResourcesAggregate : AbstractAggregate<ResourceItem>
{
//not implemented yet
}
呼叫它像:
AbstractAggregateFactory.GetAggregateClient<AskItem>(); //etc
4)最后,可能会使返回类型不那么通用?涉及切换案例,我不喜欢它。
public enum AggregateTypes { TankTruckBlog, AskTankTruck, Resources }
public static class AbstractAggregateFactory
{
public static AbstractAggregate GetAggregateClient(AggregateTypes type)
{
switch (type)
{
case AggregateTypes.AskTankTruck:
return new AskTankTruckAggregate<AskItem>();
case AggregateTypes.TankTruckBlog:
return new TankTruckBlogAggregate<BlogItem>();
case AggregateTypes.Resources:
return new ResourcesAggregate<ResourceItem>();
default:
throw new AggregateDoesNotExistException();
}
}
}
public abstract class AbstractAggregate
{
}
public abstract class AbstractAggregate<T> : AbstractAggregate
{
}
//or change the definition to AskTankTruckAggregate : AbstractAggregate<AskItem>
public class AskTankTruckAggregate<T> : AbstractAggregate<T>
{
//not implemented yet
}
//or change the definition to TankTruckBlogAggregate : AbstractAggregate<BlogItem>
public class TankTruckBlogAggregate<T> : AbstractAggregate<T>
{
//not implemented yet
}
//or change the definition to ResourcesAggregate : AbstractAggregate<ResourceItem>
public class ResourcesAggregate<T> : AbstractAggregate<T>
{
//not implemented yet
}
这样称呼它:
AbstractAggregateFactory.GetAggregateClient(AggregateTypes.AskTankTruck); //etc
海事组织,这种方法比反射的方法更糟糕。在将来很容易忘记一些枚举检查。
总之,第三看起来对我最好,但又不知道你的设计目标,它很难预测。几点建议:
你的工厂名听起来好像AggregateFactory
。其中的“摘要”使其更多地涉及到实施。
如果你需要一个枚举来表示类型,不要使它嵌套。嵌套的公共类型很难打电话。拿出包装静态类(如我的第五种方法)。
将您的基类重命名为Aggregate<T>
或。再次,“抽象”使它更多地关于实现,相当不必要。
感谢您的详细解释。我原本选择了一个不同的答案,但你说服了我。谢谢您的帮助! – apexdodge 2012-03-30 22:15:49
np :) - 有很多方法/答案,只要坚持保持逻辑(每一点通常有其目的或不保证一个地方),你会没事的。并看看国际奥委会,autofac,你会喜欢它(比泛型:)更多 – NSGaga 2012-03-30 22:29:57