Re: Class Hierarchy design problems...



> Consider the following statements
> //-------
> Item i = Basket.Items["Orange"]; //indexer is used to return instance of
> class Item
>
> Basket.Items["Apple"].Remove(); //method on class item is fired
>
> item i = new ("Pear");
> Basket.Items.Add(item);
> //------

Interesting.

My opinion is that your intent doesn't make sense. Doesn't an apple exist
outside of the basket?
How does the fact that the apple is in a basket influence the nature of the
apple itself? Should it's properties change? Does it become more tasty, or
does it decay more slowly? In fact, if you were a microbe just under the
skin of the apple, would you be able to detect any change at all in the
apple at the point when it is placed into, or removed from, the basket?

I suspect that, if you consider these ideas more carefully, you will see
that it is not a correct assumption that "Add" or "Remove" should be methods
of the item. They are methods of the basket's list of items.

In fact, you have created an unequal heirarchy of operations above, by
placing the Add method on "Items" but the Remove method on the individual
item. Is there no Remove method on the items list itself? If so, how do
you pick "any" item from the basket?

You feel that your code is more elegant by writing:
Basket.Items["Apple"].Remove(); //method on class item is fired

but is it more understandable to the developer who follows you, and who
looks at the code for the Items list and sees no Remove method?

Is it THAT MUCH more elegant than writing the following?
Basket.Items.Remove(Items["Apple"]);

(sorry for writing most of my response in questions. It is late at night...
I'm in a questioning mood. :-)
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Mark Broadbent" <nospam@xxxxxxxxxx> wrote in message
news:OphEyRNWFHA.2684@xxxxxxxxxxxxxxxxxxxxxxx
> Consider the following statements
> //-------
> Item i = Basket.Items["Orange"]; //indexer is used to return instance of
> class Item
>
> Basket.Items["Apple"].Remove(); //method on class item is fired
>
> item i = new ("Pear");
> Basket.Items.Add(item);
> //------
>
> Now my class design would be as follows
>
> class Basket{
> public Items Items{get;}
> }
> class Items{
> public Add(Item item){...}
> public Item this[string item]{get;}
> }
> class Item{
> public void Remove(){...} //problem is here
> }
>
> Now my problem is thus. If my Item class was self contained (in the sense
> that no other objects contain references to this item), I could implement
> the remove method to remove the item. Unfortunately the item must be
> deleted from a collection contained within Items. Problem is that since I
> need the indexer of Items to return a type of Item, the implementation of
> Remove must be put in Item *yet* there is no reference to the Items
> instance for me to be able to remove the item from the collection. How can
> I takle this?
>
> p.s. I should say that I do not want to implement a Remove method within
> items because I want to be able to use the indexer[].Remove design as
> above.
>
> Any ideas?
>
> Best Regards,
>
> Mark.
>
>
>


.



Relevant Pages

  • Re: The king of france is ...
    ... in my basket are red" is true when there are no apples in the basket. ... I'm not convinced that we always interpret such sentences as having an ... implicit claim that there are more than one apple in my basket. ...   ...
    (sci.logic)
  • Re: The king of france is ...
    ... when there is only one apple in the basket. ... Are you saying that the singular of "all the apples in my basket are ... "So why are mathematicians NOT what most people suppose? ...
    (sci.logic)
  • Re: The king of france is ...
    ... nothing about the number of apples in the basket. ...   ... that there is more than one apple in the basket. ... is most implausible and I was not suggesting that we interpret the ...
    (sci.logic)
  • logic design flow help needed
    ... Let's say we have a database table with the following pieces of fruit: ... Each basket ... The next basket distribution iteration looks like this: ...
    (comp.lang.php)
  • Re: Mac Mini GMA950 vs. GeForce 2
    ... the Mac range had nothing that you could fit comfortably into ... here we are back to the bad old days where there's no budget ... It's an opinion formed from my own observations. ... "Apple has a strong product marketing department." ...
    (comp.sys.mac.advocacy)

Loading