Interfaces

  • Thread starter Thread starter jjmraz
  • Start date Start date
J

jjmraz

Hi,

I have the beginning of an Interface class that has this in it.

namespace Samples.Money
{

/// <summary>The common interface for simple Monies and
MoneyBags.</summary>

interface IMoney
{

/// <summary>Adds a money to this money.</summary>
IMoney Add(IMoney m); <- Here

I am wondering what is happening by supplying IMoney type to the
functions signature?
Why would u code something like this? What scenarios would this be good
for?

Thanks,

J
 
It looks like any class that implements that interface, will have a public
method called Add, which takes an object that also implements IMoney.. and
when it adds it, it returns the object that it just added, which of course -
implements the IMoney interface.

I don't think I've seen something quite like that - I'm not sure why you'd
do that. It is common to have a "container" class, return the object that it
returns. For example, a ListView will add a ListViewItem - and also return a
ListViewItem.

Is your confusion around the concept of how an interface works or how a
method returns an interface datatype - or something else?
 
Im not sure what the coder was trying to accomplish here.

If you look at the code example where I have "Here". I just don't
understand the signature on the method IMoney Add(IMoney m);

J
 
I don't think I've seen something quite like that - I'm not sure why you'd
do that. It is common to have a "container" class, return the object that it
returns. For example, a ListView will add a ListViewItem - and also return a
ListViewItem.

In most cases, yes, but in this specific case --- there is very little
difference between "Money" and "A collection of Money", that is, if I have
$90 and add $10, I now have $100, not "$90 & $10".

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Hi,

I have the beginning of an Interface class that has this in it.

namespace Samples.Money
{

/// <summary>The common interface for simple Monies and
MoneyBags.</summary>

interface IMoney
{

/// <summary>Adds a money to this money.</summary>
IMoney Add(IMoney m); <- Here

I am wondering what is happening by supplying IMoney type to the
functions signature?
Why would u code something like this? What scenarios would this be good
for?

Classes that implement IMoney might be value objects, i.e. classes that have
value based semantics.
For example:

IMoney fiveFrancs = new Francs(5);
IMoney twoDollars = new Dollars(2);
IMoney total = twoDollars.Add(fiveFrancs);

SP
 
So for this line:

IMoney Add(IMoney m);

That basically says that any class that implements this interface, must have
a method called Add, that takes one parameter of an object that is "of type"
IMoney.. and that same Add method much return an object that is of type
IMoney. For example:

public class MyMoney : IMoney
{
private ArrayList al = new ArrayList();
public IMoney Add(IMoney mny)
{
this.al.Add(mny);
return mny;
}
}

Again, it doesn't make much sense to me what this method would do - an
interface without any context many times doesn't make sense. But
technically, and syntactically - that's all that line does. It enforces that
any class that implements that interface, have that method (the contents of
the method can be whatever) - but it does have to take that parameter and
return that value.

Does that help?
 
Back
Top