    <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  :: Exploring Patterns Part 2</title>
        <link>https://members.accu.org/index.php/journals/593</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 + Programming Topics</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/c65/">Programming</a>
                    (877)
<br />

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

                    -                        <a href="https://members.accu.org/index.php/journals/c176+65/">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;Exploring Patterns Part 2</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 27 August 1999 18:22:42 +01:00 or Fri, 27 August 1999 18:22:42 +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>Before I focus on the pattern for this issue I want to tell you
about something that I learnt whilst doing further research for the
Singleton pattern.</p>
<p>As most of you know, declaring a copy constructor for a class
inhibits the compiler from generating its own. After considerable
pressure from several people (I think I was among the most
vociferous) WG21 &amp; J16 specified that all the following were
classified as copy-constructors (for the sake of example, I am
assuming that I am dealing with a class <tt class=
"classname">MyType</tt>):</p>
<pre class="programlisting">
MyType(MyType &amp;);
MyType(MyType const &amp;);
MyType(MyType volatile &amp;);
MyType(MyType const volatile &amp;);
</pre>
<p>In addition all constructors with a first parameter matching one
of those four and defaults for all the other parameters will also
be copy constructors. The clarification concerned whether the
volatile qualified parameters resulted in inhibiting the compiler
from generating a copy constructor or a default constructor. Note
my wording, I fondly believed that any constructor would only
inhibit one or other but not both of the possible compiler
generated ones. This believe seems to be shared by many good, even
expert, writers. On numerous occasions I have had cause to point
out the flaw in code such as:</p>
<pre class="programlisting">
class MyType
{
  MyType();
public: 
  // whatever
};
</pre>
<p>where the clear intent is that it should not be possible to
create public instances of <tt class="classname">MyType</tt>. The
flaw is due to a quirk of the grammar for declarations that results
in the following code being syntactically correct (though usually
hiding undefined behaviour because storage is copied before it has
been initialised):</p>
<pre class="programlisting">
  Mytype mt = mt;
</pre>
<p>It is the quirky grammar for initialisation in this form that
leads me to strongly recommend that you always use the function
form for user defined types. If you wrote:</p>
<pre class="programlisting">
  MyType mt(mt);
</pre>
<p>You would get a compile time error unless an mt of appropriate
type had been declared in an outer scope. In other words, always
call a constructor explicitly rather than use an implicit
constructor and call to the copy constructor. Actually, as more
programmers recognise that object types (as opposed to value types)
should not have publicly accessible copy constructors the problem
will occur less often.</p>
<p>Back to the main point. Unless you have declared one of the copy
constructor forms, the compiler is at liberty to attempt to
generate one of the form <tt class="methodname">MyType(Mytype const
&amp;)</tt>.</p>
<p>However when I have raised the issue of the missing declaration
of a private copy constructor, all I have ever had is '<span class=
"emphasis"><em>Ahh&hellip; I missed that</em></span>' and the
writer has added a copy constructor. None of them has ever removed
their declaration of a default constructor. I think, that like me,
many of them have always thought in terms of two almost disjoint
sets of constructors, copy constructors and non-copy constructors
(with an overlap caused by defaulting parameters so as to leave a
more general constructor useable as a copy constructor). Even
though I have explicitly made that statement (about two disjoint
sets) in the presence of some of the World's greatest C++ experts,
none have ever corrected me (perhaps some of them just heard what
they expected, and some were too polite - though I doubt it, that
kind of politeness is unhelpful).</p>
<p>In fact, declaring a copy constructor inhibits the compiler from
generating a default constructor as well as any other copy
constructor. This means that if you want to prevent public creation
of a type all you need to do is to declare a private copy
constructor. For example:</p>
<pre class="programlisting">
class MyType
{
  MyType(Mytype &amp;);
public: 
  // whatever
};
</pre>
<p>correctly does what was intended by the earlier flawed case.</p>
<p>So we see that the correct idiom for a class that must not be
publicly constructable is to declare a private copy
constructor.</p>
<p>Now we also need to know what to do if we want to prevent
copying but are happy to allow default construction (needed for
example if you are intending to provide dynamic arrays of the type
- but not the STL containers that require access to a copy
constructor). The fix is easy:</p>
<pre class="programlisting">
class MyType
{
  MyType(Mytype &amp;);
public: 
  MyType(){};
  // whatever
};
</pre>
<p>That default constructor with an empty body tells the compiler
to do exactly what it would have done had it been able to generate
a default constructor for itself.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e70" id="d0e70"></a>The Visitor
Pattern</h2>
</div>
<p><span class="emphasis"><em>When you read the following please do
not take my word for it, wait until the experts have had a look and
either confirm, extend or correct my interpretation. Think of this
as an essay being read by a student at a seminar, only after the
discussion is complete will those involved know confidently what is
true and what is not.</em></span></p>
<p>In general we are concerned with providing stable, well-defined
behaviour for our abstractions. At the same time we want to reserve
the right to change implementation details. This is the main motive
for the concept of <tt class="literal">private</tt>/<tt class=
"literal">protected</tt>/<tt class="literal">public</tt>
interfaces. However there are cases where we have a clear idea
about how we wish to provide our data but wish to reserve the right
to alter behaviour.</p>
<p>One way of tackling this problem is by using a base class to
provide the data (together with <tt class="literal">protected</tt>
read and write member functions) and using derived classes (or
classes with pointers to the desired data structure) to provide the
behaviour. This works well where we have a single fundamental data
type but it does not work when we need a hierarchy. One example
given (in Design Patterns) of such a hierarchy is that of the
different node types required for a parse tree used by a compiler
for a computer language. For example in C the node for an
assignment must provide a left pointer to an expression node that
evaluates as a modifiable lvalue and a right pointer to an
expression node that evaluates to an rvalue. Arithmetic operator
nodes need left and right pointers to nodes for expressions
evaluating to rvalues.</p>
<p>While the data requirements are very stable (fortunately
computing languages infrequently add features requiring new node
types) the desired behaviour can change. Indeed the desired
behaviour will depend on exactly what you are trying to do(compile,
optimise etc.). It is usually unwise to have an interface cluttered
with a large number of member functions, particularly if these are
only tenuously related to each other. Even if we could provide an
exhaustive list of all the behaviour we want in such a class
hierarchy it is probably a poor idea to provide it in the
classes.</p>
<p>The idea behind the Visitor pattern is to allow programmers to
encapsulate coherent behaviour across a number of classes (not
necessarily even in the same hierarchy) into a single class.
Typically, we have something like:</p>
<pre class="programlisting">
class A_type;
class B_type;
class C_type;
// etc.
</pre>
<p>These declarations can be replaced with definitions and the
various classes are usually part of a hierarchy but this is not
necessary for the pattern to work.</p>
<pre class="programlisting">
class MyVisitor
{
public:
  virtual MyVisitor&amp; 
      VisitorToA_type(A_type *) = 0;
  virtual MyVisitor&amp; 
      VisitorToB_type(B_type *) = 0;
  virtual MyVisitor&amp; 
      VisitorToC_type(C_type *) = 0;
  // etc
  virtual ~MyVisitor() throw() {};
};
</pre>
<p>This is an abstract base class from which concrete classes
providing single behaviours can be derived. Actually all the
functions could share the same function name as overloading would
resolve which one to use. Whether you use function overloading or
not is a matter of style (shorter names requiring thoughtful
reading versus longer names providing specific information). If the
visitor is not intended to mutate (change the state of) the host
objects then the parameters in the above should be of type
<tt class="type">const *</tt>.</p>
<p>Each of the classes to be visited must include a member function
of the form:</p>
<pre class="programlisting">
MyVisitor &amp; host(MyVisitor &amp;);
</pre>
<p>Again, <tt class="literal">const</tt> qualification should be
used as appropriate: <tt class="literal">const</tt> member function
if the Visitor is non-mutating and <tt class="literal">const</tt>
qualified parameter if the Visitor is not mutated by visiting.</p>
<p>The body of <tt class="methodname">host()</tt> depends upon the
class in which it is placed so that it calls the appropriate member
function of the Visitor. For example:</p>
<pre class="programlisting">
MyVisitor &amp; A_type::host(MyVisitor &amp; v)
{ 
  return v.VisitorToA_type(this);
}
</pre>
<p>If you have chosen the function overloading mechanism then all
host types will have apparently identical bodies (though the type
of 'this' will select the correct overload version from the
visitor's members functions):</p>
<pre class="programlisting">
MyVisitor &amp; A_type::host(MyVisitor &amp; v)
{
  return v.Visitor(this);
}
</pre>
<p>The return type of the host functions and the members of the
visitor could be void but I have a strong preference for returning
an object for possible reuse. It doesn't cost much but provides one
extra resource for those that wish to use it.</p>
<p>Let me offer a trivial example where you want to be able to
dispatch the data to some form of output. You would write something
such as:</p>
<pre class="programlisting">
class StoreData: public MyVisitor
{
  istream &amp; output;
  // inhibit copying
  StoreData(StoreData const &amp;);
  void operator = (StoreData const &amp;);
public:
  explicit StoreData(istream &amp; out = cout) : output(out) {}
  virtual MyVisitor&amp; 
      VisitorToA_type(A_type *);
  virtual MyVisitor&amp; 
      VisitorToB_type(B_type *) ;
  virtual MyVisitor&amp; 
      VisitorToC_type(C_type *) ;
  // etc
  virtual ~StoreData() throw() {};
};
</pre>
<p>Now we come to a problem. The bodies of the member functions of
<tt class="classname">StoreData</tt> require access to the specific
data of the host classes. This means that each of these classes
must provide <tt class="literal">public</tt> access functions for
its data (this does not break encapsulation but it does restrict
the owners of the host classes (but remember that a pre-condition
for the use of the Visitor pattern is stable data structures).</p>
<p>Remember that the advantage of the Visitor pattern is that you
can retrofit behaviour to a bunch of (usually related) classes. Of
course, you can install any specific behaviour into the classes
themselves, but one major advantage of the Visitor pattern is that
it supports extensible behaviour.</p>
<p>It also works well where you have collections of objects, or
even composites of heterogeneous objects. <i class=
"citetitle">Design Patterns</i> gives the example of something like
a piece of equipment (such as a computer) that is built from
components that are themselves built from components etc. If you
want to cost such equipment you could (if you thought far enough
ahead) provide a visitor object that was passed around collecting
cost information from each sub-component (to do that it would have
to visit each sub-sub component recursively). This may sound
complicated until you realise that all that is required is that the
<tt class="methodname">host()</tt> function of a component
dispatches the visitor to each of the sub-components before calling
the specific member function of the visitor object on itself.</p>
<p>I think that the Visitor pattern is a powerful program technique
that deserves to be widely known. If you are serious about software
development you should work through at least one implementation of
Visitor to ensure that you understand it and will remember to
provide the groundwork where it has potential use. For example, the
Harpist's Hotel project might benefit from a Visitor facility in
all classes that provide charges (a bill is made up from a variety
of costs that are certainly not all from objects in the same
hierarchy; think about meals and rooms.) Of course this problem has
many other solutions (such as ensuring that each object includes a
reference/pointer to a bill object) and the lack of the need for
extensible behaviour probably makes other methods more
appropriate.</p>
<p>Before I wrap this up I want to speculate a bit on the
possibility of avoiding the need for public read/write access
functions for data in host classes.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e161" id="d0e161"></a>Keeping Data
Out of Reach</h2>
</div>
<p>The first thought when tackling this problem (restricting access
to data) is to consider using friendship. Unfortunately there is no
mechanism for granting friendship to a hierarchy of classes and
part of the fundamentals of the design of the Visitor pattern seems
to require a hierarchy. We need a base class so that the parameter
of the <tt class="methodname">host()</tt> functions can provide
polymorphic behaviour (select the type of behaviour that the
visitor is going to add). But that does not mean that we need a
hierarchy of derived types (to which friendship can only be granted
on a one by one basis, which would rather defeat the object of the
exercise).</p>
<p>We have another possibility via templates:</p>
<pre class="programlisting">
class MyVisitor
{
public:
  virtual MyVisitor&amp; 
      VisitorToA_type(A_type *) = 0;
  virtual MyVisitor&amp; 
      VisitorToB_type(B_type *) = 0;
  virtual MyVisitor&amp; 
      VisitorToC_type(C_type *) = 0;
  // etc
  virtual ~MyVisitor() throw() {};
};

enum Operation {Op1, Op2};

// to provide a tool for instantiating template classes
template &lt;Operation op&gt; class Guest : public MyVisitor
{
  // inhibit copying
  Guest(Guest const &amp;);
  void operator = (Guest const &amp;);
public:
  ~Guest() throw() {}
};

// Note this is still an Abstract Base Class and so instances 
// must be specialised, or be used as ABC's
// Here is an example of a specialisation
template &lt;&gt; class Guest&lt;Op1&gt;
{
  istream &amp; output;
public:
  explicit Guest(istream &amp; out = cout) : output(out) {}
  MyVisitor&amp; VisitorToA_type(A_type *);
  MyVisitor&amp; VisitorToB_type(B_type *) ;
  MyVisitor&amp; VisitorToC_type(C_type *) ;
  // etc
};
</pre>
<p>Unfortunately, though templates can declare friends and ordinary
classes can declare instances of templates to be friends there is
no syntax available to declare a template class as a friend. So
this idea may be interesting but it does not solve the problem of
finding a way whereby the various host classes can provide
privileged access rights to a Visitor hierarchy.</p>
<p>My next idea was to try and increase the security by using
namespaces coupled with fuzzing the types used (remember that the
data types/structures for the host classes must be stable if we are
using the Visitor pattern.) This is my first attempt:</p>
<pre class="programlisting">
namespace ControlAccess
{
  typedef int Int; // just as an example
  class Object
  {
    Int value;
  public:
    Object(int i=0):value(i){}
    void set_value(Int v){value = v;}
    Int get_value(){ return value;}
  };
}

int main()
{
  ControlAccess::Object obj;
  obj.set_value(3);
  cout &lt;&lt; obj.get_value();
  return 0;
}
</pre>
<p>I hoped that by hiding the <tt class="literal">typedef</tt> in a
namespace that its implicit use outside the namespace would create
an error. I agree that this was a pretty vain hope because a
<tt class="literal">typedef</tt> does not create real type. Of
course this code compiled.</p>
<p>So next I tried replacing the <tt class="literal">typedef</tt>
by a real type:</p>
<pre class="programlisting">
class Int
{
  int value;
public:
  Int(int i=0):value(i){}
  operator int () {return value;}
};
</pre>
<p>Unfortunately, the so called Koenig lookup allows the compiler
to find the <tt class="type">Int</tt> type in the context of
<tt class="methodname">obj.set_value(3)</tt> and <tt class=
"methodname">obj.get_value()</tt>. I had one last shot in my locker
(remember that my purpose is to make it possible to force
programmers to think about using the access functions they have in
host classes). Consider:</p>
<pre class="programlisting">
class Int
{
  int value;
public:
  explicit Int(int i=0):value(i){}
  int convert_to_int () {return value;}
};
</pre>
<p>Now the two function calls in question fail and require an
explicit cast (for <tt class="literal">seti</tt>) and a call to
<tt class="methodname">convert_to_int (for geti)</tt> to make the
compiler happy.</p>
<p>So, if there is data in a host class that you are reluctant to
make easily accessible to the world at large, but that you do need
to make available to Visitor, you can add this extra layer.
Visitors will have to use this as well, but we are assuming that we
are catering for data that needs thoughtful use.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e217" id=
"d0e217"></a>Conclusion</h2>
</div>
<p>I have learnt a lot while putting this article together, and I
know I have ranged further afield than strictly required for the
topic. However, I think some of the ideas and failed attempts may
prove instructive. What I do know is that the process of active
exploration rather than passive reading is what provides the value
to me. I think the same will apply to you.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e222" id=
"d0e222"></a>Postscript</h2>
</div>
<p>It has just occurred to me that there is one other mechanism
available. Have each host class that has data that you do not want
to make generally accessible declare the visitor ABC (MyVisitor for
example) a friend. Now you can place those access functions that
you want to restrict in the protected interface of the ABC. That
way all the concrete visitors will have the access they need but
no-one else will. Here is some code by way of example.</p>
<pre class="programlisting">
class MyVisitor;
// sample host classes
class First
{
  friend class MyVisitor;
  int val;
public:
  First(int i=0):val(i){}
  MyVisitor &amp; host(MyVisitor &amp;);
};

class Second
{
  friend class MyVisitor;
  float val;
public:
  Second(float f=0.0):val(f){}
  MyVisitor &amp; host(MyVisitor &amp;);
};

class Aggregate
{
  First f;
  Second s;
  int val;
public:
  Aggregate(int v=2, 
            int first = 1, 
            float second=1.0)
    :f(first), s(second), val(v){}
  MyVisitor &amp; host(MyVisitor &amp;);
  int geti(){return val;}
  void seti(int i){val = i;}
};

// now the visitor base class
class MyVisitor
{
protected:
  int getFirstval(First &amp; f){return f.val;}
  void setFirstval(First &amp; f,int i){f.val=i;}
  float getSecondval(First &amp; f){return f.val;}
 void setSecondval(First &amp;f,float i){f.val=i;}
public:
  virtual MyVisitor&amp; visitFirst(First *) = 0;
  virtual MyVisitor&amp; visitSecond(Second *) =0;
  virtual MyVisitor&amp; visitAggregate(Aggregate *) = 0;
  virtual ~MyVisitor() throw() {};
};

class PrintData: public MyVisitor
{
public:
  MyVisitor&amp; visitFirst(First *);
  MyVisitor&amp; visitSecond(Second *);
  MyVisitor&amp; visitAggregate(Aggregate *);
  ~PrintData()throw(){}
};
</pre>
<p>The implementation of the three member functions might go like
this:</p>
<pre class="programlisting">
MyVisitor&amp; PrintData::First(First * f)
{
  cout&lt;&lt; &quot;First is &quot; &lt;&lt; getFirstval(*f);
  return *this;
}

MyVisitor&amp; PrintData::Second(Second * s)
{
  cout&lt;&lt; &quot;Second is &quot;&lt;&lt; getSecondval(*s);
  return *this;
}

MyVisitor&amp; PrintData::Aggregate(Aggregate * a)
{
  cout &lt;&lt; &quot;Aggregates own data is:&quot; &lt;&lt; 
                               a.geti();
}
</pre>
<p>And the implementations of the host functions are:</p>
<pre class="programlisting">
MyVisitor &amp; First::host(MyVisitor &amp; v)
{
  v.visitFirst(this);
  return v;
}

MyVisitor &amp; host(MyVisitor &amp; v)
{
  v.visitSecond(this);
  return v;
}

MyVisitor &amp; host(MyVisitor &amp; v)
{
  f.host(v);
  s.host(v);
  v.visitAggregate(this);
  return v;
}
</pre>
<p>And finally a very short program to use this:</p>
<pre class="programlisting">
int main()
{
  Aggregate test;  // use defaults
  PrintData pd;
  test.host(pd);
  return 0;
};
</pre>
<p>The above code is untested so it is up to you to debug it, in
doing so you will need to understand it.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e243" id=
"d0e243"></a>Post-Postscript</h2>
</div>
<p>Before writing this article I had thought there was a way of
declaring a template class a friend. When I failed to get my code
to compile I checked with a couple of UK C++ experts who opined
that it was not possible, hence the assertion. I have now had a
chance to check the FDIS and find that my original belief is
justified though the syntax is counter-intuitive.</p>
<p>The inclusion of the line:</p>
<pre class="programlisting">
template&lt;Operations&gt; friend class Guest;
</pre>
<p>in each host class should provide the desired access. However, I
cannot find a compiler to compile it. Anyway, I believe that my
postscripted solution is technically better as well as being
compilable with the current compiler releases.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
