良好的OO设计(缺乏朋友修饰符) [英] Good OO design (lack of friend modifier)

查看:49
本文介绍了良好的OO设计(缺乏朋友修饰符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我有一个客户类,其中包含ContactsCollection中的联系人。


类客户

{

...


public ContactsCollection Contacts

{

get {... }

}

}


班级联系

{

...


联系方式(客户服务员)

{

cust.Contacts.Add(this);

}


public void删除()

{

cust.Contacts.Remove(this);

}

}


现在的问题是

ContactsCollection的添加和删除方法是公众所以我无法确保其他人

尝试删除联系人而不删除它或尝试添加联系人

两次等等。

有没有办法防止这种情况发生,所以没有人愿意打电话

手动添加/删除?我不希望在这些3个类之外可以看到添加/删除




我知道如果有人尝试,我可以抛出异常,或者添加/删除

内部,但在我的程序集中有数百个类,所以内部因此它对我来说不是真的感觉对。


我想知道其他人是如何解决这个问题的,因为我认为应该有很多类似星座的程序。)

Hi!

I have a class Customer which contains Contacts in a ContactsCollection.

class Customer
{
...

public ContactsCollection Contacts
{
get {...}
}
}

class Contact
{
...

Contact(Customer cust)
{
cust.Contacts.Add(this);
}

public void Delete()
{
cust.Contacts.Remove(this);
}
}

The problem now is that the add and remove methods of the
ContactsCollection are public so I cannot ensure that somebody else
tries to remove a contact without deleting it or trying to add a contact
twice and so on.

Is there a way to prevent this so that nobody comes to the idea to call
add/remove manually? I do not want that add/remove is visible outside
these 3 classes.

I know I can throw an exception if somebody tries, or make add/remove
internal but in my assembly are hundreds of classes so internal so it
doesn''t really feel right to me.

I wonder how other people get around this problem as there should be
many programs out there with a similar constellation, I suppose .)

推荐答案



cody写道:

cody wrote:

嗨!


我有在CustomerCollection中包含联系人的类Customer。


类客户

{

...


public ContactsCollection Contacts

{

get {...}

}

}


班级联系

{

...


联系方式(客户服务) )

{

cust.Contac ts.Add(this);

}


public void删除()

{

cust.Contacts.Remove(this);

}

}


现在的问题是添加和删除方法

ContactsCollection是公开的所以我无法确保其他人

尝试删除联系人而不删除它或尝试添加联系人

两次等等。


有没有办法防止这种情况发生,所以没有人想要调用

手动添加/删除?我不希望在这些3个类之外可以看到添加/删除




我知道如果有人尝试,我可以抛出异常,或者添加/删除

内部,但在我的程序集中有数百个类,所以内部因此它对我来说不是真的感觉对。


我想知道其他人是如何解决这个问题的,因为应该有很多类似星座的节目,我猜想。)
Hi!

I have a class Customer which contains Contacts in a ContactsCollection.

class Customer
{
...

public ContactsCollection Contacts
{
get {...}
}
}

class Contact
{
...

Contact(Customer cust)
{
cust.Contacts.Add(this);
}

public void Delete()
{
cust.Contacts.Remove(this);
}
}

The problem now is that the add and remove methods of the
ContactsCollection are public so I cannot ensure that somebody else
tries to remove a contact without deleting it or trying to add a contact
twice and so on.

Is there a way to prevent this so that nobody comes to the idea to call
add/remove manually? I do not want that add/remove is visible outside
these 3 classes.

I know I can throw an exception if somebody tries, or make add/remove
internal but in my assembly are hundreds of classes so internal so it
doesn''t really feel right to me.

I wonder how other people get around this problem as there should be
many programs out there with a similar constellation, I suppose .)



我觉得奇怪的是,联系人有一个方法可以从集合中删除自己的
。将该功能

移入客户类并调用cust.RemoveContact(this_contact)>

而不是this_contact.Delete()会更有意义。 "这似乎并不可疑。


之后,您可以隐藏联系人集合本身,只有
向外部对象公开必要的部分(通过写作)一个带有索引器的自定义

代理类,通过编写单独的访问方法,通过

将其暴露为IEnumerable,无论如何)。

I find it strange that the Contact has a method to remove itself from
the collection. It would make more sense to move that functionality
into the customer class and call "cust.RemoveContact(this_contact)"
instead of "this_contact.Delete()" which does not seems suspicious.

After that, you could hide the contact collections itself and only
expose the bits of it necessary to outside objects (by writing a custom
proxy class with an indexer, by writing individual access methods, by
exposing it as an IEnumerable, whatever).


我同意删除方法应存在于父级上的事实。


另一种方法是通过使用事件/委托等。这个

将允许你在联系人上有一个删除方法,反过来提出

a" deleted"事件。然后,所有接触的容器都可以从容器中取出。我个人使用这个模式在相当一个

的几个地方,要求我对项目以及

父母进行删除。


此外,因为唯一的添加是已删除。事件你不太可能想要成为这个朋友的朋友。 ...对所有与联系人打交道的班级提供
优惠是有用的。


干杯,


Greg Young

MVP - C#
http:// codebetter.com/blogs/gregyoung


< qu ******** @ gmail.comwrote in message

news: 11**********************@b28g2000cwb.googlegr oups.com ...
I agree with the the fact that the remove method should exist on the parent.

Another way of doing this is through the use of an event/delegate etc. This
would allow you to have a delete method on the contact which in turn raised
a "deleted" event. All of the containers that held the contact could then
remove it from their containers. I personally use this "pattern" in quite a
few places that require me to have the delete on the item as well as on the
parent.

Also since the only addition is a "Deleted" event it is unlikely that you
would even want to make this "Friend" ... it is useful functionality to
offer to all classes that deal with contacts.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<qu********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

> ;

cody写道:
>
cody wrote:

>嗨!

我有一个类Customer,其中包含一个联系人联系我们收集。

课程客户
{
...

公共联系人联系方式
{
获取{...}
}
}

课程联系
{
...

联络(客户服务)
{
cust.Contacts.Add(this);
}
公共无效删除()
{
cust.Contacts.Remove(this);
}
}

现在的问题是
ContactsCollection的添加和删除方法是公开的,所以我无法确保其他人试图删除联系人而不删除它或尝试添加联系人两次等等。

有没有办法防止这种情况,以至于没有人愿意手动调用
添加/删除?我不希望在这三个类之外可以看到添加/删除。

我知道如果有人尝试,我可以抛出异常,或者在内部添加/删除
我的集会是数百个课程,所以内部所以它对我来说真的不对。

我想知道其他人如何解决这个问题因为应该有很多我想这里有一个类似星座的程序。)
>Hi!

I have a class Customer which contains Contacts in a ContactsCollection.

class Customer
{
...

public ContactsCollection Contacts
{
get {...}
}
}

class Contact
{
...

Contact(Customer cust)
{
cust.Contacts.Add(this);
}

public void Delete()
{
cust.Contacts.Remove(this);
}
}

The problem now is that the add and remove methods of the
ContactsCollection are public so I cannot ensure that somebody else
tries to remove a contact without deleting it or trying to add a contact
twice and so on.

Is there a way to prevent this so that nobody comes to the idea to call
add/remove manually? I do not want that add/remove is visible outside
these 3 classes.

I know I can throw an exception if somebody tries, or make add/remove
internal but in my assembly are hundreds of classes so internal so it
doesn''t really feel right to me.

I wonder how other people get around this problem as there should be
many programs out there with a similar constellation, I suppose .)



我觉得奇怪的是,联系人有一种方法可以从

中删除自己集合。将该功能

移入客户类并调用cust.RemoveContact(this_contact)>

而不是this_contact.Delete()会更有意义。 "这似乎并不可疑。


之后,您可以隐藏联系人集合本身,只有
向外部对象公开必要的部分(通过写作)一个带有索引器的自定义

代理类,通过编写单独的访问方法,通过

将其暴露为IEnumerable,无论如何)。


I find it strange that the Contact has a method to remove itself from
the collection. It would make more sense to move that functionality
into the customer class and call "cust.RemoveContact(this_contact)"
instead of "this_contact.Delete()" which does not seems suspicious.

After that, you could hide the contact collections itself and only
expose the bits of it necessary to outside objects (by writing a custom
proxy class with an indexer, by writing individual access methods, by
exposing it as an IEnumerable, whatever).



这是一种可能性。这个模式有几种变化

可能更适合你的情况,但我认为这将是你开始的




公共类客户

{

私有MyContractCollection合约=新MyContractCollection();


公共ContractCollection合约

{

获得{返回合同; }

}


私人MyContractCollection:ContractCollection

{

public void Add(... ){}


public void删除(...){}

}

}

公共抽象类ContractCollection:ICollection

{

//将您想要公开的方法和属性放在这里。

}


Brian


cody写道:
Here is one possibility. There are several variations on this pattern
that may be more suitable for your situation, but I think this will get
you started.

public class Customer
{
private MyContractCollection contracts = new MyContractCollection();

public ContractCollection Contracts
{
get { return contracts; }
}

private MyContractCollection : ContractCollection
{
public void Add(...) { }

public void Remove(...) { }
}
}

public abstract class ContractCollection : ICollection
{
// Put methods and properties you want to expose to everyone here.
}

Brian

cody wrote:

嗨!


我有一个客户类,其中包含ContactsCollection中的联系人。


类客户

{

...


public ContactsCollection Contacts

{

get {...}

}

}


班级联系

{

...


联系方式(客户托管)

{

cust.Contacts.Add(this);

}


public void删除()

{

cust.Contact s.Remove(this);

}

}


现在的问题是<的添加和删除方法br />
ContactsCollection是公开的所以我无法确保其他人

尝试删除联系人而不删除它或尝试添加联系人

两次所以on。


有没有办法防止这种情况,以至于没有人接到想法来调用

手动添加/删除?我不希望在这些3个类之外可以看到添加/删除




我知道如果有人尝试,我可以抛出异常,或者添加/删除

内部,但在我的程序集中有数百个类,所以内部因此它对我来说不是真的感觉对。


我想知道其他人是如何解决这个问题的,因为我应该有很多类似星座的节目。)
Hi!

I have a class Customer which contains Contacts in a ContactsCollection.

class Customer
{
...

public ContactsCollection Contacts
{
get {...}
}
}

class Contact
{
...

Contact(Customer cust)
{
cust.Contacts.Add(this);
}

public void Delete()
{
cust.Contacts.Remove(this);
}
}

The problem now is that the add and remove methods of the
ContactsCollection are public so I cannot ensure that somebody else
tries to remove a contact without deleting it or trying to add a contact
twice and so on.

Is there a way to prevent this so that nobody comes to the idea to call
add/remove manually? I do not want that add/remove is visible outside
these 3 classes.

I know I can throw an exception if somebody tries, or make add/remove
internal but in my assembly are hundreds of classes so internal so it
doesn''t really feel right to me.

I wonder how other people get around this problem as there should be
many programs out there with a similar constellation, I suppose .)


这篇关于良好的OO设计(缺乏朋友修饰符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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