I came across the following code in our code base:
public interface ICloneable<T>
{
T Clone();
}
public class MyObject : ICloneable<MyObject>
{
public Stuff SomeStuff { get; set; }
T Clone()
{
return new MyObject { SomeStuff = this.SomeStuff };
}
}
The pattern of using a generic interface and then doing class A : Interface<A>
looks pretty bizarre to me. I have a feeling it's either useless or that it could be changed into something less intricated.
Can someone explain if this is right/wrong, and how it should be changed?
2 Answers 2
You see this same pattern in the framework for IEquatable<T>
. The most frequent use case for this is writing a generic method where you constrain the generic parameter type to implement the interface. This then allows you to write code in terms of a statically typed method Equals<T>
. Another example is IComparable<T>
, which is very handy for implementing methods relying on sorting without having to use the older style of providing an external Comparator class. For instance, the default comparison mechanism for the LINQ OrderBy
method uses IComparable<T>
, if it's available. In both of these cases, it's very natural to say that an instance of a type is comparable or equatable to other instances of the same type.
-
\$\begingroup\$ good point, it definitely makes sense it that case. Thanks \$\endgroup\$d--b– d--b2013年05月30日 14:00:40 +00:00Commented May 30, 2013 at 14:00
This pattern is called a Prototype object creation pattern. Usually it is used to do a deep cloning of objects (that is if Stuff
is a reference type then it should also be cloned).
-
\$\begingroup\$ almaz, what I don't get is why make the interface generic \$\endgroup\$d--b– d--b2013年05月29日 14:00:46 +00:00Commented May 29, 2013 at 14:00
-
\$\begingroup\$ So that you get a typed instance. If you declare interface as non-generic, return value would be
object
. When you're cloning aMyObject
instance it's logical to have aMyObject
return value \$\endgroup\$almaz– almaz2013年05月29日 14:08:55 +00:00Commented May 29, 2013 at 14:08 -
\$\begingroup\$ almaz: yes I understand the intent, but isnt'it disturbing to have
class A : I<A>
. \$\endgroup\$d--b– d--b2013年05月29日 14:35:51 +00:00Commented May 29, 2013 at 14:35 -
1\$\begingroup\$ ok, but if MyObject was ICloneable, I could do that too \$\endgroup\$d--b– d--b2013年05月29日 15:43:35 +00:00Commented May 29, 2013 at 15:43
-
1\$\begingroup\$ Generic interface gives you ability to use result either generally (as
object
) or as specific type (when you know it). Non-genericICloneable
interface will require you to cast result in case you know the type. \$\endgroup\$almaz– almaz2013年05月29日 16:20:34 +00:00Commented May 29, 2013 at 16:20
Clone
should returnMyObject
notT
. \$\endgroup\$