Re: Class Hierarchy design problems...
- From: "Nick Malik [Microsoft]" <nickmalik@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 15 May 2005 00:41:18 -0700
> 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.
>
>
>
.
- Follow-Ups:
- Re: Class Hierarchy design problems...
- From: James Curran
- Re: Class Hierarchy design problems...
- References:
- Class Hierarchy design problems...
- From: Mark Broadbent
- Class Hierarchy design problems...
- Prev by Date: Re: Invalid IL generated by Emit method?
- Next by Date: Re: Removing circular dependency
- Previous by thread: Re: Class Hierarchy design problems...
- Next by thread: Re: Class Hierarchy design problems...
- Index(es):
Relevant Pages
|
Loading