Re: Class Hierarchy design problems...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Mark Broadbent" <nospam@xxxxxxxxxx> wrote in message
news:%23NGRqmVWFHA.2768@xxxxxxxxxxxxxxxxxxxxxxx
> David, Nick thanks for the input.
> I agree (as you are both suggesting) that perhaps my problem is down to an
> incorrect design, although I am not entirely sure it is as clean cut as
> that.
> The addition of a Remove method on the Items class would probably be a
> good addition too i.e. Remove(Item item), however I do still think it
> makes 'sense' to have methods that fire on an object (in this scenario)
> which operate in the context of the hierarchy.
> For instance I would be saying that ... In that basket, choose an item
> (the apple), and remove it.
> You may both disagree entirely?

It's a question of modeling. If your container is a Basket and your item is
an Apple, then the item should not have a Remove() method. As Nick said, an
Apple's state does not change when you put it in a basket. You can put
non-Apples in a Basket, and you can put Apples in a non-Basket. It makes no
sense for the Apple to hold a reference to the Basket. And so
Basket.Remove(Apple)
is a better model than
Apple.RemoveFromBaket()

However if your container is, say, an Organization and your item is an
Employee, then the situation is different. An Employee would naturally have
a reference to its Organization, and can use that reference to invoke
methods on the Organization.

>
> The question I would like to have answered though is this situation
> possible?
> The biggest problem is that the indexer is determining the subsequent
> hierarchy due to its returned type (and therefore the methods would need
> to exist on that type (which causes the problem of being able to reference
> the items class).


There is no problem. The Item should have a reference to the container in
all those situations where it is appropriate for the item to remove itself.

David


.



Relevant Pages

  • Re: Class Hierarchy design problems...
    ... my object hierarchy is slightly more complex than my Basket/item ... I'm not too bothered about the Basket example, just eager to know if points ... > is an Apple, then the item should not have a Removemethod. ... > It makes no sense for the Apple to hold a reference to the Basket. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Installed Hard Disk In Dell Vista Machine Today
    ... was in reference to about the organization of the book. ... and looking at Amazon is says 1568 pages... ... "Not recommended [Fast Format] comes from the Windows Vista Resource Kit ... Apple is being negligent to not suggest doing a FF on a drive not ...
    (comp.sys.mac.advocacy)
  • Re: Class Hierarchy design problems...
    ... My opinion is that your intent doesn't make sense. ... How does the fact that the apple is in a basket influence the nature of the ... Is it THAT MUCH more elegant than writing the following? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Easter Of Resurrection
    ... nature," etc.) for either to work and the behavior of practitioners in ... these faiths as inherent possibilities within the species that need to ... I tend to think the basket may be a source of the problem also. ... certainly can't modify the nature of an apple. ...
    (rec.music.dylan)
  • 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)