在复合设计模式中使用泛型来避免拳击 [英] Using generics in the Composite Design Pattern to avoid Boxing

查看:50
本文介绍了在复合设计模式中使用泛型来避免拳击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想用叶子实现复合设计模式

是参考或值类型,但即使使用泛型我也看不出

避免装箱(使用ArrayList或Object)


这是否可能,或者是复合模式注定要永远使用

System.Object类型?


我尝试使用带有泛型的接口,但我总是偶然发现

Value属性(当试图返回特定节点的值时)


我想把孩子们录制成一个

System.Collections.Generic.Dictionary< string,Node>


我无法创建任何填充空白的Node类,无论我如何尝试。

我总是遇到必须为

节点项 - 一个用于推荐类型,一个用于值类型,如果我必须区分,那么我就失去了拍拍的好处燕鸥,如果我不是...... b $ b不...我只能看到一个出路就是拳击......

我想做点什么''这里不可能吗?

Basically I''d like to implement the composite design pattern with leaves that
are either of reference or value types, but even using generics I can''t seem
to avoid boxing (using ArrayList or Object)

Is this even possible, or is the composite pattern doomed to use the
System.Object type forever?

I have tried using interfaces with generics, but I always stumble on the
Value property (when trying to return the value of a particular node)

I''d like to record the children in a
System.Collections.Generic.Dictionary<string, Node>

I can''t create any Node class that fills in the blank, no matter how I try.
I always run into having to have two differnt kinds of Value properties for
the Node item -- one for refernce types and one for value types, and if I
have to differentiate, then I lose the benefit of the pattern, and if I
don''t... all I can see as a way out is Boxing...
Am I trying to do something that''s impossible here?

推荐答案

dave.dolan写道:
dave.dolan wrote:

基本上我是'' d喜欢使用

叶子来实现复合设计模式,这些叶子是参考或值类型,但即使使用

泛型我也似乎无法避免装箱(使用ArrayList或Object)


这是否可能,或者复合模式注定永远使用

System.Object类型?
Basically I''d like to implement the composite design pattern with
leaves that are either of reference or value types, but even using
generics I can''t seem to avoid boxing (using ArrayList or Object)

Is this even possible, or is the composite pattern doomed to use the
System.Object type forever?



只有在对象引用引用的实例(例如,
)时,才会对值类型进行限制。在一个arraylist。当你在arraylist

中存储

a引用类型(所有不是值类型)时,拳击不会发生,因为拳击不是必要的:存储的值

已经是一个对象。


我对复合模式的理解是我不认为它是

对于valuetypes有用的模式:valuetypes通常是小结构,

包含很少的行为和数据。如果你使用更大的对象作为
valuetypes,我认为最好转移到类类型。可以

您举例说明您使用的典型值类型需要

是否为您的值类型?


FB


-

------------------------ ------------------------------------------------ <无线电通信/>
LLBLGen Pro的首席开发人员,.NET的高效O / R映射器

LLBLGen Pro网站: http://www.llblgen.com

我的.NET博客: http://weblogs.asp.net/fbouma

Microsoft MVP(C#)

- -------------------------------------------------- ---------------------

Boxing only occurs with value types when an instance of these is
referenced by an object reference, e.g. in an arraylist. When you store
a reference type (everything that''s not a valuetype) in an arraylist
for example, boxing doesn''t occur, as boxing isn''t necessary: the value
stored is already an object.

My understanding of the composite pattern is that I don''t see it as a
useful pattern for valuetypes: valuetypes are often small structs which
contain very few behavior and data. If you use bigger objects as
valuetypes, I think it''s better to move to a class type instead. Could
you give an example of a typical value type you''re using which needs to
be a valuetype according to you?

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


戴夫,


如果你想让Node来自一个独立的Type,那么可以使用唯一可以用于
的Type是Object,因为它是所有东西的基础。哪个

意味着你永远是拳击。


(你的对象持有它所引用的地址,它可能太值了或

另一个对象)虽然最后一个很少被视为拳击。


但拳击并不是那么糟糕,因为它通常在网络中作为想法给出。有人

在这个新闻组中非常活跃,我不相信我,并且在经过一天的测试后确实相信我现在越来越多。


:-)

Cor


" dave.dolan" < da ******* @ discuss.microsoft.comschreef in bericht

news:64 ********************* ************* @ microsof t.com ...
Dave,

If you want Node to be from an undependent Type, than the only Type that can
be used for that is Object, because it is the base of everything. Which
means that you are forever boxing.

(Your object holds the address it is referencing which can be too a value or
an other object) Although the last is seldom seen as boxing.

But boxing is not that bad as it is often given as idea in Net. Somebody
very active in this newsgroup did not believe me about that and does believe
me now more and more after a day testing.

:-)

Cor

"dave.dolan" <da*******@discussions.microsoft.comschreef in bericht
news:64**********************************@microsof t.com...

基本上我想实现复合设计模式叶子



是参考类型或价值类型,但即使使用泛型,我也不能

似乎

避免装箱(使用ArrayList或Object)


这是否可能,或者是复合模式注定要永远使用

System.Object类型?


我尝试使用带有泛型的接口,但我总是偶然发现

Value属性(当试图返回特定节点的值时)


我想把孩子们录制成一个

System.Collections.Generic.Dictionary< string,Node>


我无法创建任何填充空白的Node类,无论我怎么样b $ b尝试。

我总是遇到有两种不同类型的Value属性



Node项 - 一个用于引用类型,一个用于值类型,如果我

必须区别对待,然后我失去了模式的好处,如果我没有... b $ b不...我只能看出一个出路就是拳击......


我试图做一些在这里不可能的事情吗?
Basically I''d like to implement the composite design pattern with leaves
that
are either of reference or value types, but even using generics I can''t
seem
to avoid boxing (using ArrayList or Object)

Is this even possible, or is the composite pattern doomed to use the
System.Object type forever?

I have tried using interfaces with generics, but I always stumble on the
Value property (when trying to return the value of a particular node)

I''d like to record the children in a
System.Collections.Generic.Dictionary<string, Node>

I can''t create any Node class that fills in the blank, no matter how I
try.
I always run into having to have two differnt kinds of Value properties
for
the Node item -- one for refernce types and one for value types, and if I
have to differentiate, then I lose the benefit of the pattern, and if I
don''t... all I can see as a way out is Boxing...
Am I trying to do something that''s impossible here?



" dave.dolan" < da ******* @discussion.microsoft.comaécritdansle message

de news: 64 ******************** **************@microsoft.com ...


|基本上我想用叶子实现复合设计模式



|是参考或价值类型,但即使使用泛型我也不能
似乎

|避免装箱(使用ArrayList或Object)

|

|这甚至是可能的,还是复合模式注定要使用

| System.Object永远类型?

|

|我尝试过使用泛型的接口,但我总是偶然发现

| Value属性(当试图返回特定节点的值时)

|

|我想把孩子们记录在

|中System.Collections.Generic.Dictionary< string,Node>

|

|我无法创建任何填充空白的Node类,无论我怎么试试。

|我总是遇到两种不同类型的Value属性

for

| Node项 - 一个用于引用类型,一个用于值类型,如果我是
|必须区分,然后我失去了模式的好处,如果我

|不是......我只能看到一个出路就是拳击......

|

|

|我想做一些在这里不可能的事情吗?


???????????????


正常的复合模式可以这样实现:


公共类复合< itemTwhere itemT:new()

{

private List< itemTitems = new List< itemT>();


public itemT Add()

{

itemT newItem = new itemT();

items.Add(newItem);

返回newItem;

}


公共项目此[int index]

{

退货[索引];

}

...

}


现在,如果你按预期使用它:


{

复合< inttest = new Composite< int>();


int item = test.Add();


item = 123;


控制台WriteLine(测试[0]);

}


因为你正在使用一个值类型,你赋给item的值不是

应用于test [0]因为item是列表中保存的项目的副本,因为

包含所有值类型。


由于复制赋值语义的这种行为,我会比如说,除了Frans'断言你通常不希望*在复合材料中使用价值

类型,你不能*使用价值类型时,除了
复合模式。


和Frans一样,我也想知道你为什么要使用价值类型




Joanna


-

Joanna Carter [TeamB]

顾问软件工程师
"dave.dolan" <da*******@discussions.microsoft.coma écrit dans le message
de news: 64**********************************@microsoft.com...

| Basically I''d like to implement the composite design pattern with leaves
that
| are either of reference or value types, but even using generics I can''t
seem
| to avoid boxing (using ArrayList or Object)
|
| Is this even possible, or is the composite pattern doomed to use the
| System.Object type forever?
|
| I have tried using interfaces with generics, but I always stumble on the
| Value property (when trying to return the value of a particular node)
|
| I''d like to record the children in a
| System.Collections.Generic.Dictionary<string, Node>
|
| I can''t create any Node class that fills in the blank, no matter how I
try.
| I always run into having to have two differnt kinds of Value properties
for
| the Node item -- one for refernce types and one for value types, and if I
| have to differentiate, then I lose the benefit of the pattern, and if I
| don''t... all I can see as a way out is Boxing...
|
|
| Am I trying to do something that''s impossible here?

????????????????

The normal composite pattern can be achieved like this :

public class Composite<itemTwhere itemT : new()
{
private List<itemTitems = new List<itemT>();

public itemT Add()
{
itemT newItem = new itemT();
items.Add(newItem);
return newItem;
}

public itemT this[int index]
{
return items[index];
}
...
}

Now, if you use this as expected :

{
Composite<inttest = new Composite<int>();

int item = test.Add();

item = 123;

Console WriteLine(test[0]);
}

Because you are using a value type, the value you assign to item is not
applied to test[0] because item is a copy of the item held in the list, as
with all value types.

Because of this behaviour of copy on assignment semantics, I would say that,
apart from Frans'' assertion that you would not normally *want* to use value
types in a composite, you *cannot* use value types in the Composite pattern.

Like Frans, I would also like to know why you would want to use value types
?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


这篇关于在复合设计模式中使用泛型来避免拳击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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