    <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  :: Data Attribute Notation - Part 2</title>
        <link>https://members.accu.org/index.php/articles/568</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">Programming Topics + Overload Journal #29 - Dec 1998</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/c65/">Programming</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/c199/">29</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+199/">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;Data Attribute Notation - Part 2</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 27 December 1999 17:23:23 +00:00 or Mon, 27 December 1999 17:23:23 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<p>This is the second in the series on Data Attribute Notation
(DAN). DAN is an object-oriented coding style that emphasizes data
abstraction. DAN binds the abstract concepts defined in a project's
analysis and design stages with the actual implementation
stage.</p>
<p>This article covers how DAN can represent relationships that
occur in most problems. Also discussed are functions as attributes
and how DAN can represent iterator classes.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e26" id="d0e26"></a>Static and
Dynamic Relationships</h2>
</div>
<p>Relationships can be static or dynamic. A static relationship is
always true, even if there are no instances of classes representing
those relationships. For example, class definitions declare a
static relationship between the components of the class, even if
the class is never instantiated. This is the <span class=
"emphasis"><em>definition relationship</em></span>. The truth value
of dynamic relationships is determined during execution. For
example, Ted is married to Alice as long as they are not
divorced.</p>
<p>The married example shows that analysis and design determines
whether a relationship is static or dynamic. If the system design
does not support divorce, then Ted being married to Alice is a
static relationship.</p>
<p>In a system that allows for divorce, marriage is a dynamic
relationship. That is, the relationship must be checked at run-time
to see if Ted is married to Alice. Note that even here, a static
relationship is needed to relate Ted to Alice. For example, each
person needs a Spouse attribute. Then you can ask if Ted's Spouse
is Alice and Alice's Spouse is Ted. If they are, then Ted is
married to Alice. Thus, there needs to be a static relationship
like spouse to evaluate a dynamic relationship like marriage.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e38" id="d0e38"></a>Representing
Relationships</h2>
</div>
<p>In this article, we use declarative code to represent
relationships. Declarative code is easy to write and easy to check
for correctness. It can be <span class=
"emphasis"><em>non-procedural</em></span>, since most declarations
may be re-ordered.</p>
<p>The rest of this article uses people and car owners as
examples.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e48" id="d0e48"></a>Relationships
As Functions</h2>
</div>
<p>The listing below shows the function <tt class=
"function">owner()</tt> that returns a non-zero value if the given
person owns the given car model.</p>
<pre class="programlisting">
class Person { /*...*/ };
enum Model { Ford, Chevy };

Person ted(Ford);
int owner(Person&amp; p, Model m);
// ... 
if (owner(ted,Chevy)) // ...
</pre>
<p>The value returned by the <tt class="function">owner()</tt> is
determined at run-time.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e63" id="d0e63"></a>Non-Member Vs
Member Functions</h2>
</div>
<p>Non-member functions have these benefits:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>They handle derived arguments fairly well.</p>
</li>
<li>
<p>They can be extended to n-ary relationships.</p>
</li>
<li>
<p>They express dynamic relationships well.</p>
</li>
<li>
<p>They handle non-commutative relationships well (i.e. they can
distinguish between a @ b and b @ a, where @ is some relationship
between objects a and b).</p>
</li>
<li>
<p>They can be overloaded to handle similar functions for different
types, e.g. home owners or car owners</p>
</li>
</ul>
</div>
<p>A non-member function can be overloaded, but can not serve as
base for another function in the same sense that one class can
serve as a base for another class. A member function of a base
class, on the other hand, can be overridden by a derived class
member function. Further, if the member function is virtual,
invoking the correct function depends on the type of the instance
for which the function is invoked. Thus, relationships represented
by functions can be extended when using virtual member functions.
Another benefit of using member functions is that the implied first
argument (i.e. the <tt class="varname">this</tt> pointer) is not
implicitly converted. This eases the problem of a function
accepting either base or derived class instances as arguments.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e89" id="d0e89"></a>Relationships
As Classes</h2>
</div>
<p>Classes can represent static relationships. For example, an
<tt class="classname">Owner</tt> relationship between a <tt class=
"classname">Person</tt> and a <tt class="classname">Car</tt> is
shown below.</p>
<pre class="programlisting">
class Person { };
class Car { };
class Owner {
  Person p;  // part 1
  Car   c;  // part 2
};
</pre>
<p>A <tt class="classname">FordOwner</tt> relationship can be
defined as a class using composition and inheritance:</p>
<pre class="programlisting">
class Ford : public Car { };
class FordOwner : public Person
{
  Ford f;
};
</pre>
<p>The listing below goes all the way and defines a <tt class=
"classname">ChevOwner</tt> only in terms of inheritance:</p>
<pre class="programlisting">
class Person { };
class Chev { };
class ChevOwner :   
  public Person, public Chev
{};
</pre>
<p>If a <tt class="classname">Person</tt> can be taxed and a
<tt class="classname">Chev</tt> can get &quot;fixed&quot;, then you can get a
<tt class="classname">ChevOwner</tt> &quot;fixed&quot; and taxed all at the
same time. This shows that a class shares all the attributes of any
one of its inherited parts. If the class represents a relationship
and it inherits some of its parts, then what is true for any one of
its inherited parts applies to the relationship as a whole. When a
relationship like <tt class="classname">Owner</tt> inherits some of
its attributes, those parts should make sense in toto. Thus, the
previous example shows poor design. In contrast, a useful derived
class composed only of inherited base classes is a <tt class=
"classname">FilledCircle</tt>. It inherits all attributes from its
two base classes: <tt class="classname">FillPattern</tt> and
<tt class="classname">Circle</tt>.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e142" id="d0e142"></a>Functions are
Attributes</h2>
</div>
<p>DAN states that a class is defined by its attributes. Consistent
with this is the fact that member and friend functions of a class
are also attributes of the class.</p>
<p>For example, if an <tt class="classname">Owner</tt> has to renew
his car license every year, an attribute <tt class=
"varname">Renewed</tt> can be defined. To check if this attribute
is set, a function member <tt class="function">isRenewed</tt> can
be invoked.</p>
<pre class="programlisting">
int isRenewed() 
  { return Renewed(*this)==1; }
// . . .
Owner o;
// . . .
if (o.isRenewed()) // . . .
</pre>
<p>The function <tt class="function">isRenewed</tt> could also be
defined as an attribute class, <tt class=
"classname">IsRenewed</tt>.</p>
<pre class="programlisting">
#include &lt;iostream.h&gt;
#include &lt;string.h&gt;

class Renewed {
  int r;
public:
  Renewed(const int rr=0)
    { r = rr; }
  operator int() const
    { return r; }
};

class Car { };
class Person {
  char *n;
public:
  Person(const char *nn=&quot;&quot;)
    { strcpy(n,nn); }
  friend ostream&amp; operator &lt;&lt;
      (ostream&amp; os, Person&amp; p)
    { os &lt;&lt; p.n; }
};

 class Owner : public Person
{
  Car c;
  Renewed r;
public:
  Owner(const char *n) :
    Person(n) { }
  operator Renewed() const
    { return r; }
  Owner&amp; operator &lt;&lt;
      (const Renewed&amp; rr)
    { r = rr; return *this; }
};

class IsRenewed {
  Renewed r;
public:
  IsRenewed(const Owner&amp; o)
    { r = Renewed(o); }
  operator int() const
    { return r; }
};

int main()
{
  Owner owner(&quot;Ted&quot;);

  owner &lt;&lt; Renewed(1);
  if (IsRenewed(owner))
    cout &lt;&lt; Person(owner)
        &lt;&lt; &quot; has renewed\n&quot;;
  return 0;
}
</pre>
<p>This shows that dynamic relationships can be converted into a
static relationship of some kind as represented by an attribute
class.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e172" id="d0e172"></a>Multiple
Relationships</h2>
</div>
<p>Relationships can be one-to-one, one-to-many, many-to-one and
many-to-many. The previous relationships were all one-to-one. I
have shown that a one-to-one relationship can be represented by a
function or an attribute class. I would like to extend this to the
other relationships. It seems fairly obvious that a one-to-many
relationship can be represented by a function member where the
class instance is the &quot;one&quot; and the &quot;many&quot; is the argument list. In
the case of a non-member function, the first argument is the &quot;one&quot;
and the &quot;many&quot; is the rest of the argument list. (Neither of these
two implementations implies the order of evaluation of the
arguments is the same as the order of the arguments.) In the case
of the many-to-one and many-to-many relationships, things get more
interesting.</p>
<p>The &quot;many&quot; could be represented by a single class containing the
&quot;many&quot;. This &quot;many&quot; class can any form mentioned earlier in the car
and owner example. That is, it could a complete composite of all
the &quot;many&quot;, it could be a mixture of inherited parts and parts
composing the &quot;many&quot; class. It could also be a completely inherited
class where all the parts of the &quot;many&quot; are inherited. The form of
the class is problem dependent.</p>
<p>In a many-to-one relationship, a member function can be used if
the class instances represent the &quot;many&quot; and the &quot;one&quot; represents
the single function argument.</p>
<p>In a many-to-many relationship, a function member can have its
class instance represent the first &quot;many&quot; and the single arguments
represent the second &quot;many&quot;. Non-member functions have two
arguments, each representing a &quot;many&quot;.</p>
<p>In implementing one-to-many, many-to-one and many-to-many
relationships, the most important concept is that the &quot;many&quot; can be
represented by a single class. Given this fact, complete
relationships can be represented by one class per relationship.
This code section shows classes representing each of these
relationships.</p>
<pre class="programlisting">
class Person { };
class Car { };

class People  // any # of people
{
  Person **pp;
};

class Fleet    // any # of cars
{
  Car **cp; 
};

// 1:1 relationship of CarOwner

class CarOwner
{
  Person p;
  Car c;
};

// 1:m relationship of one person
// owning any number of cars

 class FleetOwner
{
  Person p;  // one person
  Fleet f;    // many cars
};

// m:1 relationship of a group of
// people owning one Car

class TaxiCoop
{
  People p;  // many people
  Car cp;    // one car
};

// m:m relationship of many cars
// owned by many people

class FleetOwners
{
  People p;  // many people
  Fleet f;    // many cars
};
</pre>
<p>Pure composition was used in all these classes. A mixture of
composition and inheritance might be more appropriate, depending on
the problem.</p>
<p>It is important to note that in any relationship, you must be
able to encapsulate each side of the relationship into a class. For
example, if a number of persons own a number of cars, you have a
group called <tt class="classname">People</tt> and a group called
<tt class="classname">Fleet</tt>. <tt class=
"classname">FleetOwners</tt> is the resulting relationship. In the
case of the one-to-one relationship called <tt class=
"classname">CarOwner</tt>, one side was encapsulated into one
<tt class="classname">Person</tt> and the other side was
encapsulated into a <tt class="classname">Car</tt>.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e209" id=
"d0e209"></a>Iterators</h2>
</div>
<p>The relationship classes we have just discussed often contain
collections. Iterator classes are used to iterate over collections
of objects. The rest of this article uses iterator classes to show
how code normally thought to be procedural in nature can be written
in a declarative fashion using DAN.</p>
<p>Normally, an iterator class has <tt class=
"function">next()</tt>, <tt class="function">prev()</tt> and
<tt class="function">reset()</tt> function members or their
equivalent.</p>
<p>For example, consider iterating over the collection <tt class=
"classname">FleetOwner</tt> so that a report is produced showing
the list of cars owned in the collection <tt class=
"classname">Fleet</tt>. A classic C++ program using iterators would
look something like this.</p>
<pre class="programlisting">
class FleetOwner
{
  friend class FOIter;
  Person p;
  Fleet f;
};

class FOIter
{
  int status;  // =0 if empty
  CurrElem c;  // save cur elem
public:
  FOIter(FleetOwner&amp; fo);
  int next();  // =0 at end
  int prev();  // =0 at start
  void reset();
  friend ostream&amp; operator &lt;&lt;
    (ostream&amp; os, FOIter&amp; foi)
    { return os &lt;&lt; foi.c; }
};

int main()
{
  FleetOwner fo;
  FOIter foI(fo);

  foI.reset();
  while(foI.next())
    cout &lt;&lt; foI &lt;&lt; endl;
  return 0;
}
</pre>
<p>A more DAN-like approach for the same example would look
thus.</p>
<pre class="programlisting">
class FleetOwner
{
  friend class FOIter;
  Person p;
  Fleet f;
};

class Next { };
class Prev { };
class Reset { };

class CurrElem
{
public:
  friend ostream&amp; operator &lt;&lt;
    (ostream&amp; os, CurrElem&amp; c);
};

class FOIter{
  int status;  // =0 if empty
  CurrElem c;  // save cur elem
public:
  FOIter(FleetOwner&amp; fo)
    { status = 0; }
  operator Next();
  operator Prev();
  operator Reset();
  operator int()
    { return status; }
  friend ostream&amp; operator &lt;&lt;
    (ostream&amp; os, FOIter&amp; foi)
      { return os &lt;&lt; foi.c; }
};

int main()
{
  FleetOwner fo;
  FOIter foI(fo);

  Reset(foI);
  while(Next(foI))
    cout &lt;&lt; foI &lt;&lt; endl;
  return 0;
}
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e239" id="d0e239"></a>Declarative
Code</h2>
</div>
<p>In the listing above, I have written executable code when my
intention was to illustrate writing declarative statements. To
express this fragment of code in declarative format, we need to
agree that all fragments have a basic form. The form is: an
initialization stage, <tt class="classname">Init</tt>; a main
stage, <tt class="classname">Body</tt>; and a termination stage,
<tt class="classname">Term</tt>; any or all of which can be empty.
Further, when two fragments are placed together, the result is also
a fragment. As such, we can declare any fragment to be a class of
the simplified form. The following example illustrates this
technique.</p>
<pre class="programlisting">
class Code { };
class Init : public Code { };
class Body : public Code { };
class Term : public Code { };
class Fragment  // order of parts
{          // in this class
  Init ii;    // determines the
  Body bb;    // order of
  Term tt;    // initialization
public:
  Fragment(Init i,Body b,Term t)
    : ii(i), bb(b), tt(t) { }
  Fragment() { }
  Fragment&amp; operator &lt;&lt;
    (Fragment&amp; f);
};
</pre>
<p>This contains no definition for the insert operator &lt;&lt;
function. This was intentional. Combining code may be problem and
language dependent. In a sequential machine, the termination stage
of one fragment can be concatenated with the initialization stage
of the next fragment. In a parallel machine, all initialization
stages may be executed in parallel. In a C++ program, there is a
difference between static and dynamic data. Thus, initialization
code would need to be subdivided in those two types of data before
initialization could be performed. Regardless of these variations,
the listing below is true.</p>
<pre class="programlisting">
Init i1, i2;
Body b1, b2;
Term t1, t2;
Fragment f1(i1,b1,t1);
Fragment f2;

f2 &lt;&lt; f1;

Fragment p1(i2,b2,t2);
Fragment p2;

p2 &lt;&lt; f1 &lt;&lt; f2;
</pre>
<p>Both <tt class="varname">f1</tt> and <tt class="varname">p1</tt>
are statically defined fragments. This is consistent with languages
like C++ that are static in nature. Languages like LISP exhibit
behavior like <tt class="varname">f2</tt> and <tt class=
"varname">p2</tt> that can only be evaluated at run-time.</p>
<p>Let us return to the example of the iterator. For simplicity
sake, let strings of tokens serve as arguments to the Init, Body
and Term constructors.</p>
<pre class="programlisting">
Init i1(&quot;FleetOwner fo;
      FOIter foI(fo);
      &quot;);
Init i2(&quot;foI &lt;&lt; r;&quot;);
Body b2(&quot;while(Next(foI))
        cout &lt;&lt; foI &lt;&lt; endl;
      &quot;);
Term t1(&quot;return 0;&quot;);
Fragment f1(i2,b2,Term(&quot;&quot;));
Fragment program(i1,f1,t1);
</pre>
<p>This is pure declarative in nature. The only sequencing rule
used is that things must be defined before use.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e279" id=
"d0e279"></a>Conclusions</h2>
</div>
<p>In this part of the series, we have discussed relationships. We
have shown that a relationship can be static or dynamic as defined
by the problem domain. Representing a relationship can be done in a
number of ways. We mainly discussed declarative code using classes.
In using the classes we also discussed the effect of using
composition, partial inheritance and total inheritance. We
introduced classes as forms of relationships. We also discussed
member and friend functions as forms of dynamic relationships. In
defining many-to-one and one-to-many relationships, we spoke about
the concept of combining the &quot;many&quot; into classes expressing the
commonalty of the &quot;many&quot;. We used iterators as an example of coded
relationships and showed how they could be represented using DAN.
From this step, we became more abstract and illustrated how any
program can be represented using DAN-type declarative
statements.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
