是否有使用泛型的这种模式的名称? [英] Is there a name for this pattern of using generics?
问题描述
//this class (or interface if you like) is set up as generic...
public abstract class GenericBase<T>
{
public T PerformBasicTask(T in) { ... }
}
//... but is intended to be inherited by objects that close the generic...
public class ConcreteForDates:GenericBase<DateTime>
{
public DateTime PerformSpecificTask(DateTime in) { ... }
}
//... so that consuming code never knows that a generic is involved
var myDateConcrete = new ConcreteForDates(); //look ma, no GTP!
//These two methods look alike, and there is no generic type inference,
//even with PerformBasicTask().
var basicResult = myDateConcrete.PerformBasicTask(DateTime.Now);
var specificResult = myDateConcrete.PerformSpecificTask(DateTime.Today);
//does not compile because T is understood by inheritance to be a DateTime,
//even though PerformBasicTask()'s implementation may well handle an int.
var anotherBasicResult = myDateConcrete.PerformBasicTask(1);
我见过和使用这种模式几次,这是非常有用的跨越提供常用功能一系列特定类型的子类。例如,这可能是对特定于类型的域对象的那是中央的类用于控制所述网页控制器/演示的模型;像检索/持久基本操作可能会使用100%的通用功能,而且结合/解除绑定可能是非常具体的。
I've seen and used this pattern several times, and it's very useful for providing common functionality across a series of type-specific subclasses. For instance, this could be a model for Controllers/Presenters specific to a type of domain object that is central to the page(s) the class is used to control; basic operations like retrieval/persistence may use 100% common functionality, but binding/unbinding may be very specific.
有没有通用的声明这种模式的名称不暴露通用的最终用户?
Is there a name for this pattern of generic declaration without exposing the generic to the end user?
推荐答案
我相信这不是一个模式,但它由一个具体类型替换泛型类型参数(在你的榜样DATETIME)生成运行时具体类型的.NET Framework的泛型类型子系统的具体细节。关于共享一个共同行为的所有其他事情被称为继承
I believe this is not a pattern but specifics of Generic Type subsystem of the .NET Framework which generates concrete type in runtime by substituting generic type parameter by a concrete type (in your example DateTime). All other things regarding sharing a common behaviour is known as Inheritance
这篇关于是否有使用泛型的这种模式的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!