    <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  :: Execute Around Method and Proxy Goulash</title>
        <link>https://members.accu.org/index.php/journals/388</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 #50 - Aug 2002 + 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/c195/">50</a>
                    (7)
<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/c195-67/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c195+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;Execute Around Method and Proxy Goulash</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 August 2002 22:58:28 +01:00 or Fri, 02 August 2002 22:58:28 +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>A recent design discussion resulted in a solution that has
elements with strong similarities to the &quot;Execute Around Method&quot;
and &quot;Proxy&quot; patterns while, in both cases moving outside the scope
of the usual descriptions of these patterns [<a href=
"#Henney2001">Henney2001</a>, <a href="#GOF1995">GOF1995</a>].</p>
<p>This article presents the scenario we encountered as a pattern
story. It notes the similarities and differences to the canonical
forms of &quot;Fa&ccedil;ade&quot;, &quot;Execute Around Method&quot; and &quot;Proxy&quot;
patterns; and, raises the question &quot;are these still the same
patterns - or have they been cooked beyond recognition?&quot;</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e30" id="d0e30"></a>Intent</h2>
</div>
<p>To grant access to specific functionality only between paired
operations.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e35" id="d0e35"></a>Motivation</h2>
</div>
<p>When updating large amounts of state it may be desirable to
ensure that &quot;before&quot; and &quot;after&quot; messages are sent to the owner of
that state so that any necessary preparation or cleanup may be
applied - or that concurrent operations can be inhibited.</p>
<p>Consider a batch update of the product lines available within a
system. A complete list of product lines is supplied by an external
source and used to create, amend or delete product lines available
within the system. Because the only indication that a product is to
be deleted is that no corresponding product line is supplied it is
necessary to accumulate information regarding the product lines
accessed during the update and to deal with deletions when the
update completes. (One of the delights of interworking with
client's &quot;legacy systems&quot; is that, in this instance, they cannot
provide perfectly simple information - such as positive
notification of deletes.)</p>
<p>Typically the product lines are stored as rows within an RDBMS
and accessed via a <i class="firstterm">Broker</i> class that
provides the persistence mechanism. The Broker provides a finer
grained and more extensive interface than is required by the needs
of the application and access is mediated by a <i class=
"firstterm">Director</i> that acts as a Fa&ccedil;ade [GOF1995].
(In an EJB based system the Director would be a stateless session
bean and a Broker used in place of an entity bean to avoid the cost
of unwanted synchronisation.)</p>
<p>Because Java allows classes to grant privileged access to
package members it is possible (and not uncommon) to restrict
access to some Broker methods to the package and place both Broker
and Director in the same package. This adaptation of the pattern
enforces use of the Fa&ccedil;ade by classes outside the package
and allows protocols to be enforced. In particular declaring
&quot;start&quot;, &quot;update&quot; and &quot;finish&quot; methods with package access will
limit the code that calls them to the this package.</p>
<p>In keeping with its role as a Fa&ccedil;ade the Director should
have the responsibility of calling &quot;start&quot; and &quot;finish&quot; methods on
the Broker before and after processing of the batch update.
However, receipt of the update is not the responsibility of the
Director and, in fact, is driven by another part of the system
entirely. (It might be possible to wrap the processing of the input
as an &quot;iterator&quot; passed to the Director - however this would add
complexity to the system.)</p>
<p>This leads us towards a variation of &quot;Execute Around Method&quot;
where the paired operations are &quot;start&quot; and &quot;finish&quot; method calls
(not resource allocation) and whose target is the Broker (not self
invocation on the Resource object as described by Kevlin Henney
[Henney2001]). We still have Kevlin's <i class=
"firstterm">Command</i> object - but not only has Resource been
split into Director and Broker it is not passed to the Command's
applyTo method (&quot;run&quot; in Kevlin's paper).</p>
<p>However, we've not yet resolved the full context - as the
implementation of the Command interface still won't have access to
the necessary Broker methods. But this is where our variation on
Proxy comes into play: we define a BrokerProxy class that, being in
the same package as the Broker, has access to the necessary
functionality and can implement public forwarding functions. It is
this (not a resource) that is passed to the Command's applyTo
method by the Director. (To ensure that the Director isn't bypassed
construction of the BrokerProxy is given package access.)</p>
<p>This usage of Proxy differs from that described in [GOF1995]
because additional functionality is exposed by the Proxy class.
Specifically it doesn't substitute for a Broker. (I feel it is far
closer to Proxy than to Adapter or Bridge.)</p>
<div class="c2"><img src=
"/var/uploads/journals/resources/alan%20griffiths%20-%20Execute%20Around%20Method%20and%20Proxy%20Goulash.png"
align="middle"></div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e64" id=
"d0e64"></a>Participants</h2>
</div>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><span class="bold"><b>Broker (Broker)</b></span> - supplier of
the functionality to which access is controlled</p>
</li>
<li>
<p><span class="bold"><b>Fa&ccedil;ade (Director)</b></span> -
responsible for enforcing the access control protocol</p>
</li>
<li>
<p><span class="bold"><b>Command (Command)</b></span> - declares a
usage (applyTo) method for the functionality</p>
</li>
<li>
<p><span class="bold"><b>ConcreteCommand (UpdateCommand)</b></span>
- user of the access controlled functionality</p>
</li>
<li>
<p><span class="bold"><b>Proxy (BrokerProxy)</b></span> - forwards
calls to the Broker within the controlled scope</p>
</li>
</ul>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e93" id=
"d0e93"></a>Consequences</h2>
</div>
<p>The client code in <tt class=
"methodname">UpdateCommand.applyTo()</tt> has access to the
<tt class="methodname">Broker.update()</tt> method via the
<tt class="classname">BrokerProxy</tt>. The <tt class=
"classname">Director</tt> is able to ensure that <tt class=
"methodname">start()</tt> and <tt class="methodname">finish()</tt>
are invoked at the appropriate points in the <tt class=
"methodname">executeAround</tt> method.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e119" id="d0e119"></a>Sample
code</h2>
</div>
<p>The following code illustrates the implementation of this dish
in Java. We'll assume that a product line comprises its name and
price:</p>
<pre class="programlisting">
public class Product {
  private String name;
  private int price;
  public Product(String name, int price){
    this.name = name;
    this.price = price;
  }
  public String getName() {
    return name;
  }
  public int getPrice() {
    return price;
  }
}
</pre>
<p>A Broker class called <tt class="classname">ProductBroker</tt>
manages the persistent storage for products. It provides methods
for retrieving Product information, and for updating the product
table:</p>
<pre class="programlisting">
class ProductBroker {
  // ...
  List listProductNames() {
    // ...
  }
  Product getProduct(String name) {
    // ...
  }
  void startUpdate() {
    // ...
  }
  void update(Product data) {
    // ...
  }
  void finishUpdate() {
    // ...
  }
}
</pre>
<p>Because the broker doesn't provide public access we provide
public access to the update method via the Proxy class:</p>
<pre class="programlisting">
public class ProductBrokerProxy {
  // ...
  public void update(Product data) {
    broker.update(data);
  }
}
</pre>
<p>The Command interface by which the client code supplies the
update logic is:</p>
<pre class="programlisting">
public interface ProductUpdateCommand {
  public void applyTo(
            ProductBrokerProxy target);
}
</pre>
<p>All of this is co-ordinated by the <tt class=
"classname">ProductDirector</tt> fa&ccedil;ade as follows:</p>
<pre class="programlisting">
public class ProductDirector {
  // ...
  public void updateProducts(
        ProductUpdateCommand command) {
    final ProductBrokerProxy proxy =
               new ProductBrokerProxy();
    synchronized (this) {
      broker.startUpdate();
      command.applyTo(proxy);
      broker.finishUpdate();
    }
  }
}
</pre>
<p>While there appear to be a lot of pieces to the implementation
of the final design the client code is clear and does not rely on
following a &quot;start/update/finish&quot; protocol for correctness.</p>
<pre class="programlisting">
public class UpdateProducts {
  // ...

  public static void main(String[] args)
  {
    // Set up some example data...
    final Vector data = new Vector();
    data.add(new Product(&quot;beans&quot;, 27));
    data.add(new Product(&quot;chicken&quot;,
                                525));
    // Process the example data...
    final Iterator iter =
                    data.iterator();
    director.updateProducts(
             new ProductUpdateCommand()
    {
      public void applyTo(
             ProductBrokerProxy target)
      {
        while(iter.hasNext())
        {
          target.update(
                  (Product)iter.next());
        }
      }
    });
  }
}
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e152" id=
"d0e152"></a>Acknowledgements</h2>
</div>
<p>Thanks to Jason Martin and Andrew Rigley who: brought the
motivating example to my attention; allowed me to participate in an
interesting design session that led to the above resolution; and,
reviewed the draft article. Additional thanks are also due to Jason
who supplied the sample code on which the above fragments are
based.</p>
<p><span class="emphasis"><em>Alan Griffiths</em></span> <tt class=
"email">&lt;<a href=
"mailto:alan.griffiths@microlise.com">alan.griffiths@microlise.com</a>&gt;</tt></p>
<p><span class="emphasis"><em>Jason Martin</em></span> <tt class=
"email">&lt;<a href=
"mailto:jason.martin@microlise.com">jason.martin@microlise.com</a>&gt;</tt></p>
<p><span class="emphasis"><em>Andrew Rigley</em></span> <tt class=
"email">&lt;<a href=
"mailto:andy.rigley@microlise.com">andy.rigley@microlise.com</a>&gt;</tt></p>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e175" id="d0e175"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Henney2001" id="Henney2001"></a>
<p class="bibliomixed">[Henney2001] Kevlin Henney, &quot;Another Tale of
Two Patterns&quot; <span class="citetitle"><i class="citetitle">Java
Report</i></span> March 2001 <span class="bibliomisc"><a href=
"http://www.two-sdg.demon.co.uk/curbralan/papers/AnotherTaleOfTwoPatterns.pdf"
target=
"_top">http://www.two-sdg.demon.co.uk/curbralan/papers/AnotherTaleOfTwoPatterns.pdf</a></span></p>
</div>
<div class="bibliomixed"><a name="GOF1995" id="GOF1995"></a>
<p class="bibliomixed">[GOF1995] Gamma, Helm, Johnson, Vlissides,
<span class="citetitle"><i class="citetitle">Design
Patterns</i></span> ISBN 0-201-63361-2</p>
</div>
</div>
</div></p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
