    <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 3</title>
        <link>https://members.accu.org/index.php/articles/544</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 #30 - Feb 1999</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/c174/">30</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+174/">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 3</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 February 1999 16:50:51 +00:00 or Fri, 26 February 1999 16:50:51 +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 third in the series on Data Attribute Notation
(DAN). DAN is an object-oriented coding style that emphasizes data
abstraction. It is meant to closely relate abstract concepts as
defined in both the analysis and design stages of a project to the
project's actual implementation. The intent is to minimize the
differences amongst these three stages. This part of the series
discusses the equivalence between DAN and designs centered around
procedural classes like Michael Schelkin's Abstract Data
Interchange classes[<a href="#Schelkin">Schelkin</a>].</p>
<p>Many systems are centered more around actions that need to be
taken (action-centric) rather than around the data that needs to be
manipulated (data-centric). For example, a messaging system is an
action-centric application that invokes a call-back routine when a
given event occurs. While the event is a piece of data, the main
concern is the message handling. Another example is a dispatching
system. Its main concern is effectively using its personnel and
inventory resources. Of secondary interest is any single call,
individual service person or spare part.</p>
<p>This article tries to unify the treatment of data-centric and
action-centric systems.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e31" id="d0e31"></a>Action-Centric
Systems</h2>
</div>
<p>An action-centric system uses objects as &quot;wrappers&quot; for
procedure code. There is little if any data associated with an
object of an Action class. Listing #1 shows an Action class as
Michael Schelkin would define it.</p>
<pre class="programlisting">
class Action
{
public:
  virtual int run() = 0;
};

static int si;

class Reset : public Action
{
public:
  virtual int run() { si = 0; }
};
class EndIt : public Action
{
public:
  virtual int run() { si = -1; }
};

void doIt(Action *ap)
{
  ap-&gt;run();
}
...
  Reset *rp = new Reset;
  EndIt *ep = new EndIt;
...
  doIt(rp);  // Reset::run()
...
  doIt(ep);  // EndIt::run()
</pre>
<p><span class="bold"><b>Listing #1: Action classes</b></span></p>
<p>In Part 2 of this series [<a href="#Charney">Charney</a>], I
discussed programs being composed of fragments that could be
combined in various ways. Listing #2 shows a sample using iterators
and strings representing parts of programs.</p>
<pre class="programlisting">
class Reset { };
class Init { /* ... */ };
class Body { /* ... */ };
class Term { /* ... */ };

Init i1(&quot;
  Reset r;
  AllPetOwners apo;
  APOiter apoI(apo);
  &quot;);
Init i2(&quot;apoI &lt;&lt; r;&quot;);
Body b2(&quot;
  while(Next(apoI))
    cout &lt;&lt; apoI &lt;&lt; endl;
  &quot;);
Term t1(&quot;return 0;&quot;);
Fragment f1(i2,b2,Term(&quot;&quot;));
Fragment prog(i1,f1,t1);
</pre>
<p><span class="bold"><b>Listing #2: Fragments Using
Strings</b></span></p>
<p>As Michael Schelkin pointed out, these strings could be
instances of Action classes or their derivatives. Also,
initialization and termination code can be implemented using
constructors and destructors. So Listing #2 could be rewritten
using Action classes as shown in Listing #3.</p>
<pre class="programlisting">
AllPetOwners *apop;
APOIter *apoIp;
Reset r;

void init1()
{
  apop = new AllPetOwners;
  apoIp = new APOiter(*apop);
}

void term1()
{
  delete apop;
  delete apoIp;
}

void init2() { *apoIp &lt;&lt; r; }
void body2()
{
  while(Next(*apoIp))
    cout &lt;&lt; *apoIp &lt;&lt; endl;
}

class Action
{
public:
  virtual int run() = 0;
};

void doIt(Action&amp; a)
{
  a.run();
}

class Fragment : public Action 
{
  typedef void (*AF)();
  AF i;  // initialization code
  AF b;  // body code
  AF t;  // termination code
public:
  Fragment(AF ii, AF bb, AF tt):
  i(ii), b(bb), t(tt) 
  { if (i) i(); }
  ~Fragment() { if (t) t(); }
  virtual void run()
  { if (b) b(); }
};

// initialize f1 using init2
Fragment f1(init2,body2,Term(0));

// initialize prog using init1
Fragment prog(init1,f1,term1);

doIt(f1);  // run body2
doIt(prog);  // run f1 (ie. body2)

// destroy prog after term1 run
// destroy f1 with no term code
</pre>
<p><span class="bold"><b>Listing #3: Fragments Using Action
Classes</b></span></p>
<p>The function <tt class="function">doIt()</tt> is a simple form
of an <span class="emphasis"><em>applicator function</em></span>.
An applicator function takes one or more arguments, one of which is
another function that is applied to the remaining applicator
arguments. In Listing #3, <tt class="function">doIt()</tt> invokes
the function that is a member of <tt class="function">doIt</tt>'s
argument. There are no other arguments. In Listing #4, the
<tt class="function">doIt()</tt> function is defined to handle
<tt class="classname">Action</tt> classes that take arguments.</p>
<pre class="programlisting">
class X { /* some class */ };
class Y { /* another class */ };

class Act2
{ // 2 arg action
public:
  virtual int run(X&amp; x, Y&amp; y)=0;
};

void doIt(Act2&amp; a2,X&amp; x, Y&amp; y)
{ a2.run(x,y); }

class MyAct2 : public Act2
{
public:
  virtual int run(X&amp; x, Y&amp; y)
  { /* some action */ }
};

MyAct2 a;
X x;
Y y;

doIt(a,x,y);  // a.run(x,y)
</pre>
<p><span class="bold"><b>Listing #4: <tt class=
"function">doIt()</tt> with 2 or 3 Arguments</b></span></p>
<p>The advantage of <tt class="function">doIt()</tt> is any class
derived from an <tt class="classname">Action</tt> class invokes the
appropriate <tt class="methodname">run()</tt> member function
because of the virtual function mechanism.</p>
<p>Combining the essense of DAN and action-centric code, we come to
the following conclusions:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>objects are known by their attributes</p>
</li>
<li>
<p>an instance of an attribute can be applied to any object having
that attribute</p>
</li>
<li>
<p>an instance of an action class can be applied to an object
having that action class an an attribute.</p>
</li>
<li>
<p>if an action is a class then the action class must be an
attribute of the object for the action to apply to the object.</p>
</li>
<li>
<p>an action performed on an object affects the state of the object
and therefore the value of one or more of its attributes</p>
</li>
</ul>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e115" id=
"d0e115"></a>Questions</h2>
</div>
<p>Does a pointer to instance of an attribute class fit in with
DAN?</p>
<p>If a message handler is an action class, can we say:</p>
<pre class="programlisting">
  MsgHdlr mh;
  Msg m;
  mh &lt;&lt; m;
</pre>
<p>Can we create a MsgHdlr instance based on type of message that
it is meant to handle by defining the instance using constructors
with the message as an argument?</p>
<pre class="programlisting">
  Msg1 m1;
  Msg2 m2;
  MsgHdlr hd1(m1);
  MsgHdlr hd2(m2);
</pre>
<p>In the examples of feeding a dog meat and walking a dog 5 miles,
the first is better handled by a member function while the second
can be handled using DAN.</p>
<pre class="programlisting">
  Meat m;
  Dog d;
  d.feed(m);

  d &lt;&lt; Walk(5);
</pre>
<p>Why?</p>
<p>For example, consider feeding an animal. You can feed meat to a
dog and you can feed oats to a horse. Normally, you would define a
<tt class="methodname">feed()</tt> virtual member function for
<tt class="classname">Dog</tt> and one for <tt class=
"classname">Horse</tt>, each derived from <tt class=
"classname">Animal</tt>. You would then accept as <tt class=
"classname">Food</tt> the derived types <tt class=
"classname">Meat</tt> for <tt class="classname">Dog</tt>s and
<tt class="classname">Oats</tt> for <tt class=
"classname">Horse</tt>s. Thus, you could write the code in Listing
#4.</p>
<pre class="programlisting">
Dog d;  // derived from Animal
Horse h;  // derived from Animal
Meat m;  // derived from Food
Oats o;  // derived from Food

d.feed(m);  // feed meat to dog
h.feed(o);  // feed oats to horse 
</pre>
<p><span class="bold"><b>Listing #4: feed as member
function</b></span></p>
<p>However, if we treat actions as if they were classes, then the
concept of feed could be represented by the action class <tt class=
"classname">Feed</tt>. The fragment in Listing #4 could then be
written as shown in Listing #5.</p>
<pre class="programlisting">
Dog d;  // derived from Animal
Horse h;  // derived from Animal
Meat m;  // derived from Food
Oats o;  // derived from Food

d &lt;&lt; Feed(m);  // feed dog meat
h &lt;&lt; Feed(o);  // feed horse oats
</pre>
<p><span class="bold"><b>Listing #5: Feed as action
class</b></span></p>
<p>While the code in Listing #4 is more usual, it requires that the
member function <tt class="methodname">feed()</tt> for each animal
be overloaded for each food type that the animal eats. An
alternative would have <tt class="methodname">feed()</tt> accept a
generic food type like <tt class="classname">Food</tt>, thereby
losing the context sensitivity originally sought. That is, we can
no longer protect against feeding meat to horses or oats to
dogs.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e189" id="d0e189"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Schelkin" id="Schelkin"></a>
<p class="bibliomixed">[Schelkin] <span class="citetitle"><i class=
"citetitle">The Object or The Action</i></span> by Michael V.
Schelkin, CVu - The Journal for the C Users Group (UK) Volume 5
Issue 1, November 1992.</p>
</div>
<div class="bibliomixed"><a name="Charney" id="Charney"></a>
<p class="bibliomixed">[Charney] <span class="citetitle"><i class=
"citetitle">Data Attribute Notation - Part 2</i></span> by Reginald
B. Charney - The C++ Journal, Vol 2. No. 2 1992</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
