//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%的共同功能,但约束性/无约束力可能非常具体。
如果没有将通用声明透露给最终用户,这种通用声明是否有一个名称?