<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7651.59">
<TITLE>Re: [Seaside] Re: Smalltalk design advice - how toimplementgenericbi-directional pointers?</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>Except favourite pizza is usually some kind of aggregate of team members preferences depending on their number, seniority and drunkenness.<BR>
<BR>
Cheers!<BR>
<BR>
-Boris (via BlackBerry)<BR>
<BR>
----- Original Message -----<BR>
From: seaside-bounces@lists.squeakfoundation.org &lt;seaside-bounces@lists.squeakfoundation.org&gt;<BR>
To: Seaside - general discussion &lt;seaside@lists.squeakfoundation.org&gt;<BR>
Sent: Mon Dec 24 21:33:04 2007<BR>
Subject: Re: [Seaside] Re: Smalltalk design advice - how toimplementgenericbi-directional pointers?<BR>
<BR>
<BR>
Assume you have a collection of all the projects that interest you;&nbsp;<BR>
each project object (oh and be careful - there's a good chance that&nbsp;<BR>
there is a class called Project already in existence, so choose a&nbsp;<BR>
slightly more specific name) would likely instance variable for its&nbsp;<BR>
name, accounting code (accountants love their code numbers), manager,&nbsp;<BR>
purpose, company division, favourite pizza etc and, most importantly a&nbsp;<BR>
collection of employees assigned to it.<BR>
<BR>
In your project class, implement a method to query the collection of&nbsp;<BR>
employees to find out if a particular employee is assigned. As it&nbsp;<BR>
happens, the message #detect: already exists for this purpose; it&nbsp;<BR>
works like this -<BR>
Given a collection containing fred, jim, bill, sheela, alice<BR>
&nbsp; myCollection detect:[:element| element name = 'bill']<BR>
would return the first actual employee object which responded with&nbsp;<BR>
true - so be careful with your tests since you may&nbsp; give yourself&nbsp;<BR>
false positives! Let us assume your test is adequately careful,&nbsp;<BR>
perhaps because you actually test for identity rather than just a&nbsp;<BR>
partial name; you will get back the appropriate employee object.<BR>
<BR>
Unless it is not there - then you get an error. Whoops, better to use&nbsp;<BR>
#detect:ifNode: in that case.&nbsp; Like this<BR>
&nbsp;&nbsp; myCollection detect:[employee| employee == theEmployeeIWant] ifNone:<BR>
[nil]<BR>
that way you either get the employee or nil.<BR>
<BR>
So far then we can ask a particular project if a certain employee is&nbsp;<BR>
working on it. What about finding all the project s/he is working on?&nbsp;<BR>
There's a bunch of ways of iterating across collections and gathering&nbsp;<BR>
results - take a look at the Collection class and in particular the&nbsp;<BR>
'enumerating' protocol. In this case the #select: message is a&nbsp;<BR>
reasonable choice since it builds a collection of all the elements&nbsp;<BR>
that answer true when sent to the block -<BR>
&nbsp; companyProjects select:[:proj| proj teamIncludes:&nbsp;<BR>
theEmpoyeeOfInterest]<BR>
<BR>
So - we have a nice encapsulated way of asking the core question and a&nbsp;<BR>
nice way of building our list of projects the 'fred' works on. It's&nbsp;<BR>
not at all hard to extend this to find all the projects involving&nbsp;<BR>
several people, or a project that 'jim' isn't working on etc. There&nbsp;<BR>
are lots of useful enumeration methods for youto make use of.<BR>
<BR>
Please, please don't give in to the temptation to give the&nbsp;<BR>
CompanyProject class a method that simply returns the collection of&nbsp;<BR>
employees and then just use some awful C programmer crap to treat&nbsp;<BR>
objects as mere arrays of data. Encapsulation will save you time,&nbsp;<BR>
effort and sanity in the long run. Any time you see code looking like<BR>
things whotsits dooberries do:[db| db subelements do:[ ......<BR>
run, screaming, away.<BR>
<BR>
tim<BR>
--<BR>
tim Rowledge; tim@rowledge.org; <A HREF="http://www.rowledge.org/tim">http://www.rowledge.org/tim</A><BR>
Useful random insult:- Proof that evolution CAN go in reverse.<BR>
<BR>
<BR>
_______________________________________________<BR>
seaside mailing list<BR>
seaside@lists.squeakfoundation.org<BR>
<A HREF="http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside">http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside</A><BR>
</FONT>
</P>

</BODY>
</HTML>