    <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  :: Almost a Pattern</title>
        <link>https://members.accu.org/index.php/journals/589</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>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #27 - Aug 1998 + Design of applications and programs</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/journals/">All</a>

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c176/">27</a>
                    (10)
<br />

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

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

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

                                            <a href="https://members.accu.org/index.php/journals/c176-67/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c176+67/">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;Almost a Pattern</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 27 August 1998 18:22:41 +01:00 or Thu, 27 August 1998 18:22:41 +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>Introduction</h2>
</div>
<p>This article describes a recurring problem in program design and
presents both a method of design and an implementation of part of
that solution. The problem in question is that of separating the
application logic that governs the changes a user may make to
objects within the application from the detail of the user
interface.</p>
<p>I first documented this problem and solution as part of the
development of Experian's &quot;Micromarketer&quot; application. This formed
the basis of my presentation at the AGM. Kevlin Henney informs me
that he's used a similar design for a similar problem. (This makes
two uses: one more and I can call it a pattern!)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e25" id="d0e25"></a>The
context</h2>
</div>
<p>Many applications (including &quot;Micromarketer&quot;) can be divided
into three conceptual layers:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>GUI</p>
</li>
<li>
<p>Business abstractions</p>
</li>
<li>
<p>Core functionality</p>
</li>
</ul>
</div>
<p>Each of these provides services to the layers above and makes
use of services provided by the layers below.</p>
<p>Abstraction layer components have attributes (e.g. names) that
may be accessed and amended via the user interface (in the case of
Micromarketer, wizards &amp; property dialogs). The mechanisms for
validating these updates should be independent of the user
interface. For instance, the same component attributes may be
exposed through several parts of the user interface and the
validation needs to be consistent.</p>
<p>Some early parts of Micromarketer were developed with the
validation of changes in the user interface. It has proved
difficult to ensure that these remain consistent. In particular it
is possible to change the name of most component in three ways: via
the component &quot;browser&quot; (similar to &quot;Windows Explorer&quot;), via the
component properties dialog, or via a wizard. At one stage it was
possible to place &quot;invalid&quot; characters into a component name via
the browser and to subsequently crash the property dialog by
cancelling out of it.</p>
<p>A related problem is that changing some component attributes via
wizard page/property sheet may impact another wizard page or
property sheet. This could be because the value is displayed there,
or because there are some options that may be enabled/disabled
accordingly. In practice an approach in which the wizard pages or
property sheets implement these notifications has proven error
prone, hard to maintain and clearly breaks encapsulation.</p>
<p>Finally it may be observed that changes may not be made (and
validated) directly on the component because:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>the component may not yet exist (as in a wizard that creates the
component),</p>
</li>
<li>
<p>the changes may not be complete (so that the attributes are
temporarily inconsistent), or</p>
</li>
<li>
<p>because the wizard/properties may be dismissed without
performing the update.</p>
</li>
</ul>
</div>
<p>Consequently, the wizard/property dialog needs to keep a copy of
the component attributes.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e62" id="d0e62"></a>The
Solution</h2>
</div>
<div class="c2"><img src=
"/var/uploads/journals/resources/griffiths_almost_a_pattern_classes.png" align=
"middle"></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e67" id="d0e67"></a>The Property
Template</h3>
</div>
<p>The behaviour of a &quot;property&quot; is generic (and is templated on
the value type):</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>it holds a value,</p>
</li>
<li>
<p>if an attempt is made to change a value then the change is
&quot;validated&quot;,</p>
</li>
<li>
<p>&quot;interested&quot; objects are notified of value changes.</p>
</li>
</ul>
</div>
<p>Validation of changes will be the responsibility of the
<tt class="classname">ComponentProperties</tt> class (e.g.
<tt class="classname">M6XProperties</tt>). The wizard, wizard
pages, and, possibly, the <tt class=
"classname">ComponentProperties</tt> register as &quot;interested&quot;
objects.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e93" id="d0e93"></a>Component
Properties Classes</h3>
</div>
<p>Corresponding to each abstraction layer component type (e.g.
<tt class="classname">M6XComponent</tt>) there should be a
<tt class="classname">ComponentProperties</tt> class (e.g.
<tt class="classname">M6XProperties</tt>). This is implemented in
the abstraction layer alongside the component and exposes the
accessible attributes of the component as &quot;properties&quot;.</p>
<p>The <tt class="classname">ComponentProperties</tt> class
implements any validation methods required for the component
attributes (and attaches them to the appropriate &quot;properties&quot;). In
many cases it needs to implement a validation check that cross
checks properties for consistency. This can be used to maintain an
additional &quot;<span class="emphasis"><em>isValid</em></span>&quot;
property.</p>
<p>Each abstraction layer component has a factory method (or a
constructor) and instance methods &quot;<tt class=
"methodname">getProperties</tt>&quot; and &quot;<tt class=
"methodname">setProperties</tt>&quot; all of which accept the
corresponding <tt class="classname">ComponentProperties</tt>
class.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e126" id="d0e126"></a>Object
interactions</h3>
</div>
<div class="c2"><img src=
"/var/uploads/journals/resources/griffiths_almost_a_pattern_sequence.png" align=
"middle"></div>
<p>When a wizard (for example) is invoked it creates an instance of
the corresponding <tt class="classname">ComponentProperties</tt>
object. (The <tt class="classname">ComponentProperties</tt> object
could then be initialised from an existing component if this is
appropriate - which is the case for a properties dialog.)</p>
<p>During construction the <tt class=
"classname">ComponentProperties</tt> object sets up the validation
for any properties and also attaches listener methods to any
properties that have an overall effect. (For example properties
that affect the overall self-consistency of the <tt class=
"classname">ComponentProperties</tt>.)</p>
<p>The wizard adds &quot;listener&quot; methods on itself to selected
properties. That is, to any properties that affect the wizard
globally - for example requiring adding/removing wizard pages, or
enabling/disabling &quot;finish&quot;.</p>
<p>Each wizard page is initialised with a reference to the wizard's
<tt class="classname">ComponentProperties</tt> object. It then
controls the associate between dialog controls and the properties
and can add &quot;listener&quot; methods on itself to any properties that
affect the content or behaviour of the page.</p>
<p>When the &quot;Finish&quot; button is selected the component is
constructed using (or has its attributes set from) the <tt class=
"classname">ComponentProperties</tt> object.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e158" id="d0e158"></a>An outline
implementation</h2>
</div>
<p>The following code outlines an implementation of the &quot;Property&quot;
generic used in the above solution (full source code has been was
supplied - I presume it will find its way onto the C Vu disc.):</p>
<pre class="programlisting">
template&lt;typename MyValueType&gt; class M6Property
{
public:
</pre>
<p>A constructor for an unvalidated value:</p>
<pre class="programlisting">
M6Property(MyValueType initValue);
</pre>
<p>This constructor accepts both an initial value and an object and
a method on that object that provides the validation check:</p>
<pre class="programlisting">
template&lt;typename MyValidatorObject&gt;
M6Property(
  MyValueType initValue,
  MyValidatorObject&amp; validator, 
  int (MyValidatorObject::* method)(MyValueType));
</pre>
<p>Methods to access and modify the value. &quot;setValue&quot; returns a
non-zero error code if the validation fails:</p>
<pre class="programlisting">
MyValueType getValue() const;
int setValue(MyValueType newValue);
</pre>
<p>Methods to allow objects (normally the &quot;user interface&quot; and the
owning &quot;component properties&quot;) to register for notification of
changes to the value of the property.</p>
<pre class="programlisting">
template&lt;typename MyListenerObject&gt;
void addListener(
  MyListenerObject&amp; listener,
  void (MyListenerObject::*method)(MyValueType));
template&lt;typename MyListenerObject&gt;
void delListener(MyListenerObject&amp; listener, 
void (MyListenerObject::* method)(MyValueType));
};
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e181" id="d0e181"></a>Known
uses</h2>
</div>
<p>As stated in the introduction Kevlin Henney of QA says he's used
a similar implementation (although he's currently too busy to give
details). In addition John Merrells (the overload editor) has used
a similar idea in a client server environment in which the
aggregated properties are passed across the network.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e186" id="d0e186"></a>References:</h2>
</div>
<div class="bibliomixed">
<p class="bibliomixed">Diagrams are basically OMT (ISBN
0-13-630054-5 Rumbaugh et. al.) with pointless modifications by
Select software.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
