English 中文(简体)
是否有使用通用物的这种模式的名称?
原标题: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%的共同功能,但约束性/无约束力可能非常具体。

如果没有将通用声明透露给最终用户,这种通用声明是否有一个名称?

最佳回答

我认为,这并不是“NET”框架中通用型次系统的一种模式,而是其具体特征,在操作时间以特定类型(例如,日期)取代通用参数。 关于共同行为的所有其他内容,均称为

问题回答

如果说是这样的话,我将把它称作Specifics,我同意Aliostad,因为它是一位反家长。

Generics is used to reuse a conduct that needs to be specified than the打/em> - or at minimum within the restrictions enforced by the where article.

您所见的例子,IMHO,是generics anti-pattern。 类型不应存在,或者如果确实存在,只应在限制中加以界定,即:,处

基本上,期望所有分流执行摘要的通用摘要类别为,不使用通用性。 我不能简单地开始使用<代码>Generic<MyType>,即通用名称。

我认为这败坏了。

In this case there is a slight benefit in using a generic abstract class/interface and that is type safety achieve for PerformSpecificTask.


UPDATE

我知道这是一个有争议的问题,我将向左边和右边开枪,但我认为情况确实如此。

A class can happily subclass a generic class and add more functionality. But in this case, it is the extra functionality that defines the identity of that class. When I cannot just say Generic<MyOwnTypeOfVolition> then I have defeated the objective of generics. Sometimes, however, it is not the generics which I am concerned with - it is the interface which seems to be the case here.


UPDATE 2

一个典型的例子是IConvertible in .NET Framework。

You could setup a generic interface/abstract class for it and ask all subclasses to implement it, but the framework makes it an optional case and supports it only for classes implementing the interface.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...