Re: Class Hierarchy design problems...
- From: "Mark Broadbent" <nospam@xxxxxxxxxx>
- Date: Sun, 15 May 2005 15:23:27 +0100
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?
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).
The only work around I can think off is to create a new type called for
instance ItemEditor which I can instanciate and pass a reference to the
collection stored in the Items class . I could therefore return the
ItemEditor via the Items indexer (providing these methods). The problem
arises then that I would need to create an Item property in the Items editor
to return an object of type Item.
e.g.
class ItemEditor{
Item item;
Hashtable itemlist;
public ItemEditor(Hashtable itemcollection, Item item){...} //note
the constructor sets its localvars to these references so I can operate on
them
public Item
Item{ //item
property to return type Item
return item;
}
public void
Remove(){ //remove
method that can operate on collection within Items class
itemlist.Remove(item.Name);
}
}
//therefore class Items would need to be...
class Items{
Hashtable itemlist;
public Add(Item item){...}
public Item this[string item]{
get { Item i = itemlist[item];
return new ItemEditor(itemlist, item);
}
}
}
//Therefore the following would work as thus :-
//-------
Item i = Basket.Items["Orange"].Item;
Basket.Items["Apple"].Remove(); //method on class ItemEditor is fired
item i = new ("Pear");
Basket.Items.Add(item);
//------
Is there a way to do this any differently (i.e. to have the indexer
returning a certain type such as Items) but have addtional methods available
which are not present in Items? e.g.
Basket.Items["Apple"]; //returns type Item through indexer
Basket.Items["Apple"].Remove(); //item does not have remove
implemented (it is implemented elsewhere?)
It would be just good to know what is available to me.
Thanks guys,
Br,
Mark.
===================================
"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: David Browne
- Re: Class Hierarchy design problems...
- References:
- Class Hierarchy design problems...
- From: Mark Broadbent
- Class Hierarchy design problems...
- Prev by Date: Re: 1000's UDP packets arrive same time problem
- Next by Date: Re: building a program similiar to "task manager"
- Previous by thread: Re: Class Hierarchy design problems...
- Next by thread: Re: Class Hierarchy design problems...
- Index(es):
Relevant Pages
|