<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>JMatter Blog: Tip of the Week:  Modeling with Interfaces</title>
    <link>http://jmatter.org/articles/2006/09/06/tip-of-the-week-modeling-with-interfaces</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Simplify</description>
    <item>
      <title>Tip of the Week:  Modeling with Interfaces</title>
      <description>&lt;p&gt;We are taught that interface-based modeling is preferred over models that do not employ interfaces.  I concur.  In this short tip, I'd like to demonstrate how to do interface-based modeling in JMatter.&lt;/p&gt;

&lt;p&gt;As an illustration, let's take the original Sympster application and extend it.  Sympster is a conference manager demo application.  Up until now, one could use this application to define a symposium, speakers, their talks, and to schedule sessions.  A session is defined as a talk given at a specific date and time, and with a specific duration, at a specific location.&lt;/p&gt;

&lt;p&gt;I'd like to broaden the definition of a session to include events other than speakers' presentations.  What about BOF's (Birds of a Feature session) for example?  Or maybe a panel discussion including a  number of participants.  At NFJS symposia, we actually hold informal BOFs with 2-3 participants where we discuss a particular topic.&lt;/p&gt;

&lt;p&gt;Accompanying this blog entry are the code changes committed to Sympster in subversion revision #84, which essentially implements this enhancement.  Let's walk through the code.&lt;/p&gt;

&lt;p&gt;The first thing I do is define the interface &lt;code&gt;Event&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  public interface Event extends ComplexEObject
  {
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's not much of an interface but it turns out that &lt;code&gt;ComplexEObject&lt;/code&gt; already includes the contract that I will need:  the requirement that every event have a title:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  public Title title();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I still want to define this interface as it serves as a mechanism for qualifying event types.&lt;/p&gt;

&lt;p&gt;Next, let's go ahead and define a new object to model a BOF:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  public class BOF
        extends AbstractComplexEObject implements Event
  {
     private final StringEO title = new StringEO();

     private final RelationalList participants = new RelationalList(Speaker.class);
     public static Class participantsType = Speaker.class;

     public static String[] fieldOrder = {"title", "participants"};

     public BOF() { }

     public StringEO getTitle() { return title; }
     public RelationalList getParticipants() { return participants; }

     public Title title()
     {
        return title.title().append(" with", participants);
     }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, let's retrofit our existing class &lt;code&gt;Talk&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  eitan@ubuntu:~/build-area/jmatter-ba/demo-apps/Sympster/src/com/u2d/sympster$ svn diff -r83:84 Talk.java
  Index: Talk.java
  ===================================================================
  --- Talk.java   (revision 83)
  +++ Talk.java   (revision 84)
  @@ -7,7 +7,7 @@
   import com.u2d.reflection.FieldAt;
   import java.awt.Color;

  -public class Talk extends AbstractComplexEObject
  +public class Talk extends AbstractComplexEObject implements Event
   {
      private final StringEO title = new StringEO();
      private final TextEO talkAbstract = new TextEO();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see from the above diff, all I had to do was specify that Talk implement Event.&lt;/p&gt;

&lt;p&gt;Finally, remaining is broadening Session's definition.  Rather than define an association to a &lt;code&gt;Talk&lt;/code&gt;, we'll revise that to be be an association to an &lt;code&gt;Event&lt;/code&gt;.  I won't show you all the edits.  Here's the gist of the change:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  -   public Talk getTalk() { return talk; }
  -   public void setTalk(Talk talk)
  +   public Event getEvent() { return event; }
  +   public void setEvent(Event event)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And that's it.  Now let's take a look at how these changes manifest themselves in our application:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Polymorphic Queries:  Listing Events&lt;/p&gt;

&lt;p&gt;&lt;img src="/tips/interfaces_tip/listing_events.png" alt="Polymorphic Queries:  Listing Events"/&gt;&lt;/p&gt;

&lt;p&gt;Discussion:&lt;/p&gt;

&lt;p&gt;JMatter models the polymorphic association using Hibernate's 
implicit polymorphism mechanism, otherwise known as &lt;em&gt;any&lt;/em&gt; 
associations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Polymorphic Instantiation:  Creating a new Event&lt;/p&gt;

&lt;p&gt;&lt;img src="/tips/interfaces_tip/creating_events.png" alt="Polymorphic Instantiation:  Creating a new Event"/&gt;&lt;/p&gt;

&lt;p&gt;Discussion:&lt;/p&gt;

&lt;p&gt;JMatter will prompt the user whether they wish to create 
a talk or a BOF.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The calendar:&lt;/p&gt;

&lt;p&gt;&lt;img src="/tips/interfaces_tip/events_calendar.png" alt="Events Calendar"/&gt;&lt;/p&gt;

&lt;p&gt;Discussion:&lt;/p&gt;

&lt;p&gt;We see two events on this calendar.  The first is a talk on JMatter,
and the second is a BOF on Programming.  :-)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;</description>
      <pubDate>Wed, 06 Sep 2006 22:44:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ee987df5a99f489f282026c61a8a966d</guid>
      <author>Eitan Suez</author>
      <link>http://jmatter.org/articles/2006/09/06/tip-of-the-week-modeling-with-interfaces</link>
    </item>
  </channel>
</rss>
