Trying to understand interfaces

A

AMP

Hello,
I am using the Head First book to try and understand interfaces.There
is one thing I dont understand about the following code:

public void Sort()
{
cards.Sort(new CardComparer_bySuit());
}

Calls the function Compare()

in this class:
namespace __Two_Decks
{
class CardComparer_bySuit : IComparer<Card>
{
public int Compare(Card x, Card y)
{
if (x.Suit > y.Suit)
return 1;
if (x.Suit < y.Suit)
return -1;
if (x.Value > y.Value)
return 1;
if (x.Value < y.Value)
return -1;
return 0;
}
}
}

I understand the that the class has to implement Compare(), but what
if there were 2 methods that ICompared had signatures for. As I said,
the function (cards.Sort()) calls the class, not the method
Compare().
I dont get it.
Thanks
Mike

The code is on the Oreilly site.
 
A

Andy

Hello,
I am using the Head First book to try and understand interfaces.There
is one thing I dont understand about the following code:

public void Sort()
{
cards.Sort(new CardComparer_bySuit());
}

Calls the function Compare()

in this class:
namespace __Two_Decks
{
class CardComparer_bySuit : IComparer<Card>
{
public int Compare(Card x, Card y)
{
if (x.Suit > y.Suit)
return 1;
if (x.Suit < y.Suit)
return -1;
if (x.Value > y.Value)
return 1;
if (x.Value < y.Value)
return -1;
return 0;
}
}

}

I understand the that the class has to implement Compare(), but what
if there were 2 methods that ICompared had signatures for. As I said,
the function (cards.Sort()) calls the class, not the method
Compare().
I dont get it.
Thanks
Mike

The code is on the Oreilly site.

If there were two methods, both would have to be implemented.

cards.Sort does call the Compare method. You just don't see it because
you don't have the source code for the Sort method.
 
J

Jeff Louie

AMP... This is an example of using an interface when you need to defer
the final
implementation to a more knowledgeable class. In other words, it is a
special
case of using an interface in which you have PART of the implementation
but the
final user must provide the REMAINDER of the implementation. So there is
a
complex quick sort implementation that calls the Compare method. To use
this
quick sort implementation, the custom class must implement the Compare
method to provide the ordinality of the custom class.

In general, the a compare(obj1, obj2) works with nulls, whilst a call to
an
instance method obj1.CompareTo(obj2) fails if obj1 is null. If you are
interested
in this level of understanding see p275 the runtime structure of objects
in
Object Oriented Software Construction 2nd edition by Betrand Meyer.

It seems to me the code you posted will fail if Card x or y was null.

Regards,
Jeff
 
B

Ben Voigt [C++ MVP]

It seems to me the code you posted will fail if Card x or y was null.

It seems to me that Card is most appropriately a value type, which can't be
null.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top