Re: Class Hierarchy design problems...
- From: "David Browne" <davidbaxterbrowne no potted meat@xxxxxxxxxxx>
- Date: Sun, 15 May 2005 10:02:43 -0500
"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
.
- Follow-Ups:
- Re: Class Hierarchy design problems...
- From: Mark Broadbent
- Re: Class Hierarchy design problems...
- References:
- Class Hierarchy design problems...
- From: Mark Broadbent
- Re: Class Hierarchy design problems...
- From: Mark Broadbent
- Class Hierarchy design problems...
- Prev by Date: Re: processor cache and performance
- Next by Date: need help
- Previous by thread: Re: Class Hierarchy design problems...
- Next by thread: Re: Class Hierarchy design problems...
- Index(es):
Relevant Pages
|