    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Patterns Collaborations: Observer and Composite</title>
        <link>https://members.accu.org/index.php/articles/382</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Design of applications and programs + Overload Journal #51 - Oct 2002</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c67/">Design</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c194/">51</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c67-194/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c67+194/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Patterns Collaborations: Observer and Composite</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 October 2002 22:58:12 +01:00 or Wed, 02 October 2002 22:58:12 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>Patterns are a powerful tool in the software development
toolbox, because they provide documentation not only of solutions
to problems, but solutions that already have successful track
records. Therefore there are at least two concrete ways in which
they can help us to be more effective in designing software:</p>
<p>First, we can draw on patterns as a source of documented
<span class="emphasis"><em>experience</em></span> when doing our
design. Second, seeing known patterns occurring in software once we
have designed it, leaves us with a good feeling about what we have
produced; that is, we can have increased confidence that our design
is sensible, because we know we have used approaches that have
already been proven to work well!</p>
<p>I'm sure it will not be controversial to say that the best known
book about patterns within the development community in general is
the &quot;GoF book&quot; [<a href="#Gamma1995">Gamma1995</a>], a work
presenting a catalogue of twenty-three object oriented design
patterns. Patterns are at their most effective when working
together in collaboration, and while there is much to recommend
this book, it has the drawback of failing to point out many
effective collaborations.</p>
<p>There are a number of ways for patterns to collaborate. For
example, in their original (Alexandrian) setting each pattern was
an element of a <span class="emphasis"><em>pattern
language</em></span> [<a href="#Alexander1977">Alexander1977</a>].
Another way involves patterns working together in teams, as Ralph
Johnson describes in his article &quot;How Patterns Work in Teams&quot;, an
article that can be found in [<a href=
"#Rising1998">Rising1998</a>].</p>
<p>Now, in object oriented software design, two (of the many)
problems that arise often are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>How can an object notify others of changes in its state?</p>
</li>
<li>
<p>How, at run time, a client can treat a group of objects
uniformly - that is how can the group be made to appear as one
object from the client's perspective?</p>
</li>
</ul>
</div>
<p>OBSERVER Pattern addresses the first of these, and COMPOSITE
Pattern the second - both being documented (and described more
fully) in the GoF book. This article is about these two patterns.
First, it will recap on the two patterns themselves by presenting a
brief summary of each. Second, it will describe how design can
potentially benefit from their collaboration.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e54" id="d0e54"></a>OBSERVER
Pattern</h2>
</div>
<p>The purpose of the OBSERVER Pattern is to allow updates to the
state of an object to be notified to others automatically. The
following diagram shows the basic configuration.</p>
<div class="c2"><img src=
"/var/uploads/journals/resources/radford-observer_configuration.JPG" align=
"middle"></div>
<p>SUBJECT is the role name for an object that is to notify others
of updates to their state, OBSERVER is the role name for objects
that are notified of state updates in their SUBJECT, and Update is
the method that serves as notification handler. In order to keep
coupling between SUBJECTs and OBSERVERs to a minimum, the role of
OBSERVER is crystallised as an interface.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e62" id=
"d0e62"></a>COMPOSITEPattern</h2>
</div>
<p>The purpose of the COMPOSITE Pattern is to allow clients to
treat objects and compositions of objects uniformly. The following
diagram shows the basic configuration.</p>
<div class="c2"><img src=
"/var/uploads/journals/resources/radford-composite_configuration.JPG" align=
"middle"></div>
<p>All objects that are to be treated uniformly support the same
design type interface Component. An object that holds a collection
of COMPONENTs - COMPOSITECOMPONENT - also supports the COMPONENT
interface. The implementation of each method in COMPOSITECOMPONENT
simply forwards method invocations to each object in the
collection. Therefore, the client actually deals with a collection
of objects, but from the client's perspective it deals with
one.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e70" id=
"d0e70"></a>Collaboration</h2>
</div>
<p>When it comes to implementing the OBSERVER Pattern, several
issues arise, but there is one in particular that is of interest
here: the SUBJECT must bear the weight of the &quot;machinery&quot; needed to
notify between zero and many OBSERVERs. Actually, it is fair to put
it more strongly: in order to allow itself to be observed, the
SUBJECT must commit the design sin of assuming a dual role, i.e.
its design role plus that of notifying OBSERVERs of state updates.
This issue can therefore be elevated to problem status, and stated
as follows: how can the SUBJECT be relieved of its extra
responsibility and consequent machinery?</p>
<p>Obviously the SUBJECT can not be relieved of all such machinery,
but the machinery can be simplified: instead of the SUBJECT talking
to between zero and many OBSERVERs, let it talk to only one - a
COMPOSITEOBSERVER! The diagram on the following page shows the
transformation in the configuration.</p>
<p>The SUBJECT's collection of OBSERVERs is replaced by a one to
one relationship with a single COMPOSITEOBSERVER, used via an
association with the OBSERVER interface. This has indeed afforded
the benefit of the SUBJECT having a one to one association with a
single OBSERVER. Therefore, a COMPOSITEOBSERVER can now be
substituted transparently. However, this transformation has
consequences both in favour of it and against it. Consequences in
its favour are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The SUBJECT is simplified by the separation of concerns -
putting it another way, although the (unavoidable) notification
machinery is still in the system, the configuration has been
transformed such that the SUBJECT is no longer burdened by it.</p>
</li>
<li>
<p>The notification mechanism can be tested independently.</p>
</li>
<li>
<p>The SUBJECT can send update notifications without worrying about
the possibility of there being zero OBSERVERs: it's up to the
COMPOSITEOBSERVER to handle it.</p>
<div class="c2"><img src="/var/uploads/journals/resources/radford-collaboration.JPG"
align="middle"></div>
</li>
</ul>
</div>
<p>So far so good, but you don't get anything for nothing. The
consequences against are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The boundary of encapsulation has moved: the mechanism for
attaching and detaching OBSERVERs will leak into the client
code.</p>
</li>
</ul>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e96" id="d0e96"></a>Finally</h2>
</div>
<p>The OBSERVER/COMPOSITE collaboration is just one of many
examples of collaborations between two or more (object oriented
design) patterns. Further, it is just one example of how the
OBSERVER Pattern can collaborate with others.</p>
<p>The collaboration solves one problem: that of the SUBJECT being
burdened by the OBSERVER notification mechanism. However it brings
with it another problem - that of mechanism leakage. Perhaps
collaboration with another design pattern such as FA&Ccedil;ADE
[<a href="#Gamma1995">Gamma1995</a>] could be used to address this
resulting problem...(?)</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e106" id="d0e106"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Alexander1977" id=
"Alexander1977"></a>
<p class="bibliomixed">[Alexander1977] Christopher Alexander, Sara
Ishikawa and Murray Silverstein with Max Jacobson, Ingrid
Fiksdahl-King and Shlomo Angel, <span class="citetitle"><i class=
"citetitle">A Pattern Language: Towns, Buildings,
Construction</i></span>, Oxford University Press, 1977.</p>
</div>
<div class="bibliomixed"><a name="Gamma1995" id="Gamma1995"></a>
<p class="bibliomixed">[Gamma1995] Erich Gamma, Richard Helm, Ralph
Johnson and John Vlissides, <span class="citetitle"><i class=
"citetitle">Design Patterns: Elements of Reusable Object-Oriented
Software</i></span>, Addison-Wesley, 1995.</p>
</div>
<div class="bibliomixed"><a name="Rising1998" id="Rising1998"></a>
<p class="bibliomixed">[Rising1998] Editor Linda Rising,
<span class="citetitle"><i class="citetitle">The Patterns
Handbook</i></span>, Cambridge University Press.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
