类层次结构设计问题...... [英] Class Hierarchy design problems...

查看:70
本文介绍了类层次结构设计问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下陈述

// -------

项目i = Basket.Items [" Orange]; // indexer用于返回

类的实例项目


Basket.Items [" Apple"]。Remove(); //类项目上的方法被解雇


item i = new(" Pear);

Basket.Items.Add(item);

// ------


现在我的班级设计如下


class Basket {

公共项目项目{get;}

}

class项目{

public Add(Item item){。 ..}

public Item this [string item] {get;}

}

class Item {

public void Remove(){...} //问题在这里

}


现在我的问题就在于此。如果我的Item类是自包含的(在某种意义上

,没有其他对象包含对此项的引用),我可以实现

remove方法来删除该项。不幸的是,必须从Items中包含的集合中删除该项目

。问题是因为我需要物品的

索引器来返回一个Item类型,所以删除的实现必须

放入Item *但*没有引用我的Items实例

能够从集合中删除该项目。我该怎么做呢?


p.s.我应该说我不想在

项目中实现Remove方法,因为我希望能够使用indexer []。如上所述删除设计。


有什么想法吗?


最诚挚的问候,


马克。

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.

推荐答案



" Mark Broadbent" <无**** @ nospam.com>写在消息

新闻:Op ************** @ TK2MSFTNGP09.phx.gbl ...

"Mark Broadbent" <no****@nospam.com> wrote in message
news:Op**************@TK2MSFTNGP09.phx.gbl...
考虑以下陈述
// -------
项目i = Basket.Items [" Orange]; // indexer用于返回
类项目的实例

Basket.Items [" Apple"]。Remove(); //类项目上的方法被解雇

项目i = new(" Pear);
Basket.Items.Add(item);
// ---- -

现在我的班级设计如下

班级篮球{
公共项目项目{get;}
}
班级项目{
public Add(Item item){...}
public item this [string item] {get;}
}
class item {
public void删除(){...} //问题在这里
}

现在问题就在于此。如果我的Item类是自包含的(在某种意义上说没有其他对象包含对该项的引用),我可以实现删除方法来删除该项。不幸的是,必须从Items中包含的集合中删除该项目。问题是因为我需要Item的索引器来返回一个Item类型,所以
Remove的实现必须放在Item *中*还没有引用Items
实例我能够从集合中删除该项目。我怎么能这个呢?

p.s.我应该说我不想在
项目中实现Remove方法,因为我希望能够使用indexer []。删除上面的设计。

任意想法?
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?




通常你会在每个项目中添加对Items容器的引用。

然后Item.Remove可以调用Items.Remove(项目)。


David



Typically you would add a reference to the Items container in each item.
Then Item.Remove can invoke Items.Remove(Item).

David


>请考虑以下语句
> Consider the following statements
// -------
项目i = Basket.Items [" Orange]; // indexer用于返回
类项目的实例

Basket.Items [" Apple"]。Remove(); //类项目上的方法被解雇

项目i = new(" Pear);
Basket.Items.Add(item);
// ---- -


有趣。


我的意见是你的意图没有意义。篮子外面不存在苹果吗?

苹果在篮子里的事实如何影响
$ b的性质$ b苹果本身?它的属性应该改变吗?它会变得更好吃吗?或者它b $ b它会慢慢腐烂吗?事实上,如果你是一个微生物,只是在苹果的b / B $ B $皮肤下面,那么你能够在

苹果中发现任何变化吗?是放入篮子里还是从篮子里取出?


我怀疑,如果你仔细考虑这些想法,你会看到

它不是一个正确的假设是添加或删除应该是项目的方法

。它们是篮子物品清单的方法。


事实上,你已经在上面创建了一个不等的操作层,通过

放置Add关于项目的方法但是对于个人

项目的删除方法。项目列表本身没有删除方法吗?如果是这样,你如何选择任何或任何篮子里的物品?


你觉得你的代码更优雅,写作:

Basket.Items [" Apple"]。Remove(); //类项目上的方法被解雇


但对于关注你的开发人员来说更容易理解,并且

查看了Items列表的代码并且看不到删除方法?


是否比编写以下内容更优雅?

Basket.Items.Remove(Items [" Apple"]] );


(很抱歉写了我的大部分回答。这是深夜......

我心情不好。 :-)

-

--- Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik


免责声明:本论坛中发表的意见是我自己的意见,而不是我雇主的b $ b代表。

我不代表我的雇主回答问题。我只是一名帮助程序员的
程序员。

-

" Mark Broadbent" <无**** @ nospam.com>在消息中写道

新闻:Op ************** @ TK2MSFTNGP09.phx.gbl ...请考虑以下陈述
// --- ----
项目i = Basket.Items [" Orange]; // indexer用于返回
类项目的实例

Basket.Items [" Apple"]。Remove(); //类项目上的方法被解雇

项目i = new(" Pear);
Basket.Items.Add(item);
// ---- -

现在我的班级设计如下

班级篮球{
公共项目项目{get;}
}
班级项目{
public Add(Item item){...}
public item this [string item] {get;}
}
class item {
public void删除(){...} //问题在这里
}

现在问题就在于此。如果我的Item类是自包含的(在某种意义上说没有其他对象包含对该项的引用),我可以实现删除方法来删除该项。不幸的是,必须从Items中包含的集合中删除该项目。问题是因为我需要Item的索引器来返回一个Item类型,所以
Remove的实现必须放在Item *中*还没有引用Items
实例我能够从集合中删除该项目。我怎么能这个呢?

p.s.我应该说我不想在
项目中实现Remove方法,因为我希望能够使用indexer []。删除上面的设计。

任意最好的问候,

Mark。
//-------
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" <no****@nospam.com> wrote in message
news:Op**************@TK2MSFTNGP09.phx.gbl... 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.



David,Nick谢谢输入。

我同意(正如你们所建议的那样)或许我的问题归结为

不正确的设计,虽然我不完全确定它是干净的切割为

那个。

在Items类上添加一个Remove方法也可能是一个很好的

另外添加即删除(项目项目) ),但是我仍然认为它会使得某个对象(在这种情况下)触发的方法会触发对象的操作。层次结构。

例如我会说...在那个篮子里,选择一个项目(

苹果),并将其删除。

你们两人可能完全不同意吗?


q我想回答一下这个情况是否可以这样?

最大的问题是索引器正在确定随后的

等级到期到它的返回类型(因此该方法需要

存在于该类型上(这导致能够引用

项类的问题)。

我能想到的唯一工作就是创建一个名为

实例ItemEditor的新类型,我可以实例化并传递对

集合的引用存储在Items类中。因此,我可以通过Items索引器(提供这些方法)返回

ItemEditor。问题

然后出现我需要在Items编辑器中创建一个Item属性

来返回Item类型的对象。

例如

class ItemEditor {

Item item;

Hashtable itemlist;

public ItemEditor(Hashtable itemcollection,Item item) {...} //注意

构造函数将其localvars设置为这些引用因此我可以操作

它们

public Item

Item {// item

返回类型的属性项目

返回项目;

}

public void

删除(){//删除

方法可以对Items类中的集合进行操作

itemlist.Remove(item。姓名);

}

}


//因此课程项目需要...

class Items {

Hashtable itemlist;

public Add(Item item){...}

public Item this [string item] {

get {Item i = itemlist [item];

return new ItemEditor(itemlist,item);

}

}

}


//因此以下内容如下: -

// -------

项目i = Basket.Items [" Orange]。项目;

Basket.Items [" Apple"]。Remove(); //类ItemEditor上的方法被触发


item i = new(" Pear);

Basket.Items.Add(item);

// ------


有没有办法以不同的方式做到这一点(即让索引器

返回某些类型,例如Items)但是有可用的附加方法

哪些不存在于Items中?例如

Basket.Items [" Apple"]; //通过索引器返回类型Item

Basket.Items [" Apple"]。Remove(); //项目没有删除

实现了(它在其他地方实现了?)


知道我可以使用的内容真是太好了。 />

谢谢大家,


Br,


Mark。

= ==================================

" Mark Broadbent" ; <无**** @ nospam.com>写在消息

新闻:Op ************** @ TK2MSFTNGP09.phx.gbl ...
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" <no****@nospam.com> wrote in message
news:Op**************@TK2MSFTNGP09.phx.gbl...
考虑以下陈述
// -------
项目i = Basket.Items [" Orange]; // indexer用于返回
类项目的实例

Basket.Items [" Apple"]。Remove(); //类项目上的方法被解雇

项目i = new(" Pear);
Basket.Items.Add(item);
// ---- -

现在我的班级设计如下

班级篮球{
公共项目项目{get;}
}
班级项目{
public Add(Item item){...}
public item this [string item] {get;}
}
class item {
public void删除(){...} //问题在这里
}

现在问题就在于此。如果我的Item类是自包含的(在某种意义上说没有其他对象包含对该项的引用),我可以实现删除方法来删除该项。不幸的是,必须从Items中包含的集合中删除该项目。问题是因为我需要Item的索引器来返回一个Item类型,所以
Remove的实现必须放在Item *中*还没有引用Items
实例我能够从集合中删除该项目。我怎么能这个呢?

p.s.我应该说我不想在
项目中实现Remove方法,因为我希望能够使用indexer []。删除上面的设计。

任意最好的问候,

Mark。
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.



这篇关于类层次结构设计问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆