Article View: borland.public.csharpbuilder.language.csharp
Article #314Re: Sharing object with notification on deletion, "best practice" ?
From: Joanna Carter (TeamB)
Date: Mon, 25 Jul 2005 19:04
Date: Mon, 25 Jul 2005 19:04
100 lines
3081 bytes
3081 bytes
"OD" <OD[.@.]e-naxos.com> a οΏ½crit dans le message de news:
mn.cca47d57c87d5b9d.18651@0e-naxos.com...
> once again with a problem of modeling under C# more than a syntax
> problem.
> here is the problem :
>
> Class A is showing a property (or accessors, no matter) for a
> collection of object of class P.
>
> users of class A can add and suppress instances of P
>
> Now, I've got a class B that is referencing some instances of P stores
> in A.
>
> Well. Here is the question : what is the best pattern to be sure that
> when the user is deleting an object from the list stores in A, the
> object is no more referenced by the instance of class B ?
You could use the Observer pattern, but in C#, you would simply declare an
event on A or P and add a handler/delegate on B that adds its delegate to
the event on A/P. Since .NET events are multicast, you have the Observer
pattern for free :-))
Try the following untested but possibly correct code :
public delegate void OnRemovePHandler(P item);
public class P
{
public void OnRemove()
{
if (Remove != null)
Remove(this);
}
public event OnRemovePHandler Remove;
}
public class A
{
ArrayList internalList = new ArrayList;
public AddP(P item)
{
internalList.Add(item);
}
public RemoveP(P item)
{
internalList.Remove(item);
item.OnRemove();
}
public P this[int index]
{
get
{
return internalList[index];
}
}
}
public class B
{
private P pRef;
public P PRef
{
get { return pRef; }
set
{
if (pRef != value)
{
if(pRef != null)
pRef.Remove -= new OnRemovePHandler(HandleRemoveP);
pRef = value;
if (pRef != null)
pRef.Remove += new OnRemovePHandler(HandleRemoveP);
}
}
}
private void HandleRemoveP(P item)
{
if (pRef == item)
PRef = null;
}
}
Joanna
--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
Technical Details
Message-ID:
<42e537ad@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!nx01.iad01.newshosting.com!newshosting.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<mn.cca47d57c87d5b9d.18651@0e-naxos.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,342976a6aefaf8ae X-Google-Attributes: gid1efe35,public X-Google-Language: ENGLISH,ASCII From: "Joanna Carter \(TeamB\)" <joanna@nospam.co.uk> Newsgroups: borland.public.csharpbuilder.language.csharp References: <mn.cca47d57c87d5b9d.18651@0e-naxos.com> Subject: Re: Sharing object with notification on deletion, "best practice" ? Date: Mon, 25 Jul 2005 20:04:01 +0100 Lines: 102 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 82.69.239.102 Message-ID: <42e537ad@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1122318253 82.69.239.102 (25 Jul 2005 12:04:13 -0700) X-Authenticated-User: jcarter Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!nx01.iad01.newshosting.com!newshosting.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail