    <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  :: The Pet Project - Monopoly (Part 3)</title>
        <link>https://members.accu.org/index.php/journals/499</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 #36 - Mar 2000 + 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/c168/">36</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/c168-67/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c168+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;The Pet Project - Monopoly (Part 3)</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 March 2000 17:50:56 +01:00 or Sun, 26 March 2000 17:50:56 +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>
<div class="sidebar">
<p class="c2"><span class="remark">Before letting you read on I
find that I must give readers a strong warning lest we be accused
of breach of the Intellectual Property Rights of the owners of
Monopoly&reg;. The source code provided by these articles must
never be used to produce a working electronic version of
Monopoly&reg;. The game is being used purely for the purposes of
providing an exposition of certain computer programming techniques
and for no other purpose.</span></p>
<p class="c2"><span class="remark">Francis Glassborow Journal
Editor, ACCU</span></p>
</div>
<p>In the previous article we looked at my original C++
implementation of Monopoly&reg;, and pointed out a few of the
things that were incorrect in the design. As I mentioned in the
first article, I am now using this 'Pet Project' to teach myself
something about UML and &quot;try-out&quot; a few patterns. Before we dig
deeper into these areas I wish to say a word about what prompted me
to write these articles.</p>
<p>Francis. Well, actually it was the Whiteboard article Francis
wrote in Overload (see Ref [<a href="#Glassborow">Glassborow</a>]).
He had been studying the patterns in <i class="citetitle">Design
Patterns</i> (the GOF book, Ref [<a href="#GoF">GoF</a>]), and had
been writing up the results of his investigations. In the article
he stated he has now decided to &quot;call it a day&quot; because he is
&quot;concerned with understanding and not just a superficial ability to
imitate&quot;.</p>
<p>Well, I make no bones about it, this article is pure imitation.
It discusses my experiences of the <span class=
"emphasis"><em>Proxy</em></span>, <span class=
"emphasis"><em>Bridge</em></span> and <span class=
"emphasis"><em>Visitor</em></span> patterns from the above book. I
really do not know if they are used &quot;correctly&quot; - so don't take
this as a &quot;lesson&quot;. One aim is to show how these experiences worked
(or otherwise). A second aim is to convey ideas about how patterns
may be put into practice, rather than show complete implementations
(so do not go looking for an assignment operator in the Proxy and
Bridge for example).</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e51" id="d0e51"></a>First
experiences</h2>
</div>
<p>My first introduction to patterns was the GOF book (Ref
[<a href="#GoF">GoF</a>]). I read it. It made sense (really). I was
keen to apply what I'd read. And that's where I hit a brick wall. I
thought I could see how to apply patterns to some parts of the
Monopoly program, but when I tried them I felt my original
implementation still to be reasonable (however crude it was with
all the globals). I found it difficult to apply any of the patterns
and end up with a &quot;better&quot; implementation. I re-read the book. It
still made sense. I bought other books (Refs [<a href=
"#Booch">Booch</a>], [<a href="#Martin">Martin</a>], [<a href=
"#Fowler">Fowler</a>] and [<a href="#Buschmann-">Buschmann-</a>])
(and read them, though not yet cover to cover). They all helped but
I still couldn't see any way to seriously apply a pattern.</p>
<p>Something was missing - and it turned out to be one of my most
recent purchases - <i class="citetitle">Pattern Hatching</i> (Ref
[<a href="#Vlissides">Vlissides</a>]). This has shed a light on how
to apply patterns; the question remains &quot;Are they being applied
correctly?&quot;</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e79" id="d0e79"></a>Continuing
thoughts on STL</h2>
</div>
<p>In the last article a statement was made about the <tt class=
"classname">Player</tt> and <tt class="classname">Site</tt> classes
in Monopoly. That is, they need to be reference-based objects. If
they were value based then the following piece of code would not do
what was expected:</p>
<pre class="programlisting">
void Bank::CreditPlayer(Player p, int amount){
  p.cash += amount;
}
void Board::PassGo(){
  int const salary = 200;
  theBank.CreditPlayer(currentPlayer, salary);
}
</pre>
<p>A copy of the <tt class="varname">currentPlayer</tt> object
would be credited with the <tt class="varname">salary</tt>, not the
<tt class="varname">currentPlayer</tt> itself. We force the
<tt class="classname">Player</tt> objects to be reference based by
declaring the copy constructor and assignment operator to be
<tt class="literal">private</tt> and not defining them. If we now
try to call the <tt class="methodname">CreditPlayer</tt> method we
end up with error message during compilation to say that <tt class=
"classname">Player</tt>'s copy constructor cannot be accessed.</p>
<p>Passing <tt class="classname">Player</tt> by reference to
<tt class="classname">CreditPlayer</tt> (<tt class="literal">void
Bank::CreditPlayer (Player&amp; p, int amount)</tt>), not only gets
rid of our compilation error but also means the routine achieves
the expected result.</p>
<p>However, making <tt class="classname">Player</tt> objects
reference based also leads to another problem if we start using the
standard template library. The STL container classes require
value-based objects. If we want to use <tt class=
"classname">Player</tt> in <tt class=
"classname">std::list&lt;Player&gt;</tt> then we get the same 'copy
constructor' error message as mentioned previously.</p>
<p>At first sight there appear to be three<sup>[<a name="d0e139"
href="#ftn.d0e139" id="d0e139">1</a>]</sup> ways forward:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>using pointers;</p>
</li>
<li>
<p>using the Proxy pattern;</p>
</li>
<li>
<p>using the Bridge pattern<sup>[<a name="d0e154" href=
"#ftn.d0e154" id="d0e154">2</a>]</sup>.</p>
</li>
</ol>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e158" id="d0e158"></a>1. Pointers</h3>
</div>
<p>Wherever we wish to add a <tt class="classname">Player</tt>
object to a list we do so by making the list entry refer to the
object's address, i.e. we have <tt class=
"literal">std::list&lt;Player*&gt;</tt>. It works<sup>[<a name=
"d0e169" href="#ftn.d0e169" id="d0e169">3</a>]</sup>, but one ends
up with 'ugly' code having to de-reference or take addresses of
objects whenever we use the list. We also enter an area of my own
uncertainty - what happens when <tt class="classname">Player</tt>
is becomes an abstract class and has <tt class=
"classname">HumanPlayer</tt> and <tt class=
"classname">ComputerPlayer</tt> derived from it. I have a niggling
suspicion that this could end up with some implementation dependent
code if we were comparing object addresses.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e182" id="d0e182"></a>2. Proxy</h3>
</div>
<p>GOF definition of the Proxy pattern states that it &quot;provides a
surrogate or placeholder for another object to control access to
it.&quot; Well this sounds like what is needed - if we have a <tt class=
"classname">PlayerProxy</tt>, which is value-based, and it and its
copies are the placeholders for <tt class="classname">Player</tt>,
we could declare it thus:</p>
<pre class="programlisting">
class Player;
class PlayerProxy 
{
public:
  PlayerProxy(Player&amp; p) : myPlayer(p) {};
  PlayerProxy(PlayerProxy const&amp; p) :
                 myPlayer(p.myPlayer) {}
  operator Player&amp; () { return myPlayer; }

private:
  Player&amp; myPlayer;
};
</pre>
<p>We can then use <tt class="classname">PlayerProxy</tt> whenever
we want to put <tt class="classname">Player</tt>s in an STL
container. Note that the <tt class="methodname">operator
Player&amp;()</tt> provides a mechanism to get the original
<tt class="classname">Player</tt> object when its required. However
we talked about 'ugly' code when using pointers and the above still
requires us to deal with proxy objects separately from the original
object - see example at top of next column:</p>
<pre class="programlisting">
Player me (&quot;Nigel&quot;);
PlayerProxy myProxy(me); // Here,
std::list&lt;PlayerProxy&gt; thePlayers; // here
  thePlayers.push_back(myProxy); 
// and here we deal with the Proxy class or object.
  Player&amp; currentPlayer(thePlayers.front()); 
// but we can get the original!
</pre>
<p>Another problem with this solution is that, if we're not
careful, it is possible for the proxy object to reference a
non-existent player:</p>
<pre class="programlisting">
PlayerProxy CreatePlayer(std::string name) {
  Player p(name);
  PlayerProxy pp(p);
  return pp;
}
foo {
  std::list&lt;PlayerProxy&gt; thePlayers;
  thePlayers.push_back(CreatePlayer(&quot;Nigel&quot;));
// but the Player object no longer exists!
}
</pre>
<p>It would be much nicer if we could treat the proxy object as if
it was the real player (but value-based) and let it control
creation, lifetime and access to the real reference-based
<tt class="classname">Player</tt> object. So I'm after a <tt class=
"classname">PlayerProxy</tt>, which controls the interface to a
<tt class="classname">Player</tt> but the <tt class=
"classname">Player</tt> performs the actual implementation.</p>
<p>I've already stated I want to treat the proxy object as if it
was the real player, so from now on I rename the classes. I shall
use the class names <tt class="classname">Player</tt> and
<tt class="classname">PlayerImplementation</tt>. <tt class=
"classname">Player</tt> now represents value-based objects;
<tt class="classname">PlayerImplementation</tt> represents the
reference-based object.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e243" id="d0e243"></a>3. Bridge</h3>
</div>
<p><span class="bold"><b>Warning:</b></span> I now believe my next
steps to be wrong, but I'm relating my experiences and this proved
a slight, but nevertheless useful diversion.</p>
<p>Design Patterns (Ref [<a href="#GoF">GoF</a>]) describes Bridge
thus &quot;Decouple an abstraction from its implementation so that the
two can vary independently&quot;.</p>
<p>So my (incorrect) train of thought was, &quot;An abstract class (or
better a pure abstract class) defines an interface, so let's define
an interface but, rather than make it abstract, let's give it a bit
of intelligence and responsibility about the creation of the
implementation object&quot;. The interface class would be value-based,
but only one 'implementation' object would get created. Without
thinking too deeply we end up with something like:</p>
<pre class="programlisting">
class Player {
public:
  Player(std::string name) : myPlayerImp(
      new PlayerImplementation(name)){}

  Player(Player const&amp; p) : 
      myPlayerImp(p.myPlayerImp) {}

  ~Player() {delete myPlayerImp; }
  int GetCash() {
    return myPlayerImp-&gt;GetCash();
  }
// etc
private:
  PlayerImplementation* myPlayerImp;
};
class PlayerImplementation 
{
public:
  PlayerImplementation (std::string name) : myName(name), myCash(1500) {}
  int GetCash (void) { return myCash; }
// etc
private:
PlayerImplementation (PlayerImplementation const&amp;);
  PlayerImplementation&amp; operator= (PlayerImplementation const&amp;);
private:
  std::string myName;
  int myCash;
};
</pre>
<p>Of course we ought to think further! What happens in the
following (rather contrived) case?</p>
<pre class="programlisting">
Player CreatePlayer (std::string name) 
{
  Player p (name);
  return p;
}

foo 
{
  Player aNewPlayer (CreatePlayer (&quot;Nigel&quot;));
}
</pre>
<p>Well, the creation of object <tt class="varname">p</tt> will
create the implementation object then, on exit from <tt class=
"classname">CreatePlayer</tt>, <tt class="varname">p</tt> gets
destroyed and the implementation object gets destroyed. We end up
with <tt class="varname">aNewPlayer.myPlayerImp</tt> pointing to a
non-existent <tt class="classname">PlayerImplementation</tt>
object. We only want to destroy the <tt class=
"classname">PlayerImplementation</tt> when the final <tt class=
"classname">Player</tt> object, referencing it, is being destroyed.
So we need to add a reference counting mechanism. I will not do
this here - it's more than adequately covered by Scott Meyers in
Ref [<a href="#Meyers">Meyers</a>].</p>
<p>There's one more important point to note here. It goes back to
my statement about giving the interface (&quot;abstract&quot;) class
<tt class="classname">Player</tt> &quot;a bit of intelligence and
responsibility&quot;. By doing this I'm not using the Bridge pattern at
all. In fact I've simply turned it into a better Proxy pattern than
my previous example. The differences from the original are:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>The choice of names for the classes (The &quot;proxy&quot; class is now
called <tt class="classname">Player</tt> and the real player
objects are of class <tt class=
"classname">PlayerImplementation</tt>)</p>
</li>
<li>
<p>Control over how the real player object is created and
destroyed.</p>
</li>
</ol>
</div>
<p>If you wish to read more about the Bridge pattern and decoupling
abstraction from implementation, then the case study in Ref
[<a href="#Martin">Martin</a>] provides many examples of this.</p>
<p>Well this series of articles started out as a trilogy, but I'll
look at the Visitor pattern and mention a quick word about UML and
Monopoly in the fourth and final part of the trilogy!</p>
</div>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e314" id="d0e314"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Glassborow" id="Glassborow"></a>
<p class="bibliomixed">[Glassborow] Ruminations on Studying Design
Patterns (<span class="citetitle"><i class=
"citetitle">Overload</i></span> 32), Francis Glassborow; ISBN
1354-3172</p>
</div>
<div class="bibliomixed"><a name="GoF" id="GoF"></a>
<p class="bibliomixed">[GoF] <span class="citetitle"><i class=
"citetitle">Design Patterns - Elements of reusable
software</i></span>, Gamma et al; Addison Wesley; ISBN
0-201-63361-2</p>
</div>
<div class="bibliomixed"><a name="Booch" id="Booch"></a>
<p class="bibliomixed">[Booch] <span class="citetitle"><i class=
"citetitle">Object-oriented Analysis and Design</i></span>, Grady
Booch; Benjamin / Cummings; ISBN 0-8053-5340-2</p>
</div>
<div class="bibliomixed"><a name="Martin" id="Martin"></a>
<p class="bibliomixed">[Martin] <span class="citetitle"><i class=
"citetitle">Designing Object Oriented C++ Applications Using the
Booch Method</i></span>, Robert Martin; Prentice Hall; ISBN
0-13-203837-4</p>
</div>
<div class="bibliomixed"><a name="Fowler" id="Fowler"></a>
<p class="bibliomixed">[Fowler] <span class="citetitle"><i class=
"citetitle">Analysis Patterns - Reusable Object Models</i></span>,
Martin Fowler; Addison Wesley; ISBN 0-201-89542-0</p>
</div>
<div class="bibliomixed"><a name="Buschmann-" id="Buschmann-"></a>
<p class="bibliomixed">[Buschmann-] <span class=
"citetitle"><i class="citetitle">A System of Patterns (Pattern -
Oriented Software Architecture)</i></span>, Buschmann et al; Wiley;
ISBN 0-471-95869-7</p>
</div>
<div class="bibliomixed"><a name="Vlissides" id="Vlissides"></a>
<p class="bibliomixed">[Vlissides] <span class=
"citetitle"><i class="citetitle">Pattern Hatching - Design Patterns
Applied</i></span>, John Vlissides; Addison Wesley; ISBN
0-201-43293-5</p>
</div>
<div class="bibliomixed"><a name="Meyers" id="Meyers"></a>
<p class="bibliomixed">[Meyers] <span class="citetitle"><i class=
"citetitle">More Effective C++</i></span>, Scott Meyers; Addison
Wesley; ISBN 0-201-63371-X</p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c3" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e139" href="#d0e139" id=
"ftn.d0e139">1</a>]</sup> <i><span class="remark">I wonder if
another way would be to encapsulate the STL container in a class
that provides a reference based interface. I think this is the way
the STL is intended for use in high level code. Francis
Glassborow</span></i></p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e154" href="#d0e154" id=
"ftn.d0e154">2</a>]</sup> Experienced 'Patternites' bear with me on
this one.</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e169" href="#d0e169" id=
"ftn.d0e169">3</a>]</sup> Or should I say &quot;appears to work&quot;?</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
