B
beginwithl
hi
Sorry for so many questions ( again )
What follows are excerpts from msdn article regarding the differences
between interfaces and delegates and when you should choose one over
the other. Anyways, I don’t quite understand the arguments they
present:
msdn:
“Use a delegate in the following circumstances:
• It is desirable to encapsulate a static method.
• A class may need more than one implementation of the method."
a) “It is desirable to encapsulate a static method.”
I assume they are referring to the fact that interfaces can’t declare
static methods and thus if you want to call a static method, you
should choose a delegate?
“A class may need more than one implementation of the method.”
b) What class are they referring to? An observer class or publisher
class?
c) By ”By more than one implementation of the method” are they
talking about method overloading? But, interface methods can also be
overloaded!
d) In any case, I don’t understand how needing more than one method
implementation qualifies a delegate as being more appropriate?
2)
Continuing with msdn article:
“Use an interface in the following circumstances:
• A class only needs one implementation of the method.
• The class using the interface will want to cast that interface to
other interface or class types.
• The method being implemented is linked to the type or identity of
the class: for example, comparison methods. “
a) “A class only needs one implementation of the method.”
This question is sort of continuation of the previous one: So why is
interface more appropriate if class only needs to implement one
method?
b) “The class using the interface will want to cast that interface to
other interface or class types.”
In what situations would you need to cast that interface to other
interface ( or class ) and thus would choose interface over delegate
for that reason alone?
c) “The method being implemented is linked to the type or identity of
the class: for example, comparison methods. “
Are they implying that the reason for implementing IComparable
interface, you basically identify a class to the world as having
comparison methods? Or is the above quote implying something
completely different?
3)
Continuing with msdn article:
“One good example of using a single-method interface instead of a
delegate is IComparable Although using a delegate comparison method as
the basis of a sort algorithm would be valid, it is not ideal. Because
the ability to compare belongs to the class and the comparison
algorithm does not change at run time, a single-method interface is
ideal.”
a) “Because the ability to compare belongs to a class…” --> point they
are trying to make? So what if the ability belongs to a class? Why
would that make delegate less appropriate?
b) “…and the comparison algorithm does not change at run time, a
single-method interface is ideal.”
What has comparison algorithm not changing at run time got to do with
delegates and interfaces?
And besides, what do they mean with comparison algorithm not changing
at run time?
thank you
Sorry for so many questions ( again )
What follows are excerpts from msdn article regarding the differences
between interfaces and delegates and when you should choose one over
the other. Anyways, I don’t quite understand the arguments they
present:
msdn:
“Use a delegate in the following circumstances:
• It is desirable to encapsulate a static method.
• A class may need more than one implementation of the method."
a) “It is desirable to encapsulate a static method.”
I assume they are referring to the fact that interfaces can’t declare
static methods and thus if you want to call a static method, you
should choose a delegate?
“A class may need more than one implementation of the method.”
b) What class are they referring to? An observer class or publisher
class?
c) By ”By more than one implementation of the method” are they
talking about method overloading? But, interface methods can also be
overloaded!
d) In any case, I don’t understand how needing more than one method
implementation qualifies a delegate as being more appropriate?
2)
Continuing with msdn article:
“Use an interface in the following circumstances:
• A class only needs one implementation of the method.
• The class using the interface will want to cast that interface to
other interface or class types.
• The method being implemented is linked to the type or identity of
the class: for example, comparison methods. “
a) “A class only needs one implementation of the method.”
This question is sort of continuation of the previous one: So why is
interface more appropriate if class only needs to implement one
method?
b) “The class using the interface will want to cast that interface to
other interface or class types.”
In what situations would you need to cast that interface to other
interface ( or class ) and thus would choose interface over delegate
for that reason alone?
c) “The method being implemented is linked to the type or identity of
the class: for example, comparison methods. “
Are they implying that the reason for implementing IComparable
interface, you basically identify a class to the world as having
comparison methods? Or is the above quote implying something
completely different?
3)
Continuing with msdn article:
“One good example of using a single-method interface instead of a
delegate is IComparable Although using a delegate comparison method as
the basis of a sort algorithm would be valid, it is not ideal. Because
the ability to compare belongs to the class and the comparison
algorithm does not change at run time, a single-method interface is
ideal.”
a) “Because the ability to compare belongs to a class…” --> point they
are trying to make? So what if the ability belongs to a class? Why
would that make delegate less appropriate?
b) “…and the comparison algorithm does not change at run time, a
single-method interface is ideal.”
What has comparison algorithm not changing at run time got to do with
delegates and interfaces?
And besides, what do they mean with comparison algorithm not changing
at run time?
thank you