Tip of the Week: Modeling with Interfaces
Posted by Eitan Suez Thu, 07 Sep 2006 03:44:00 GMT
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.
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.
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.
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.
The first thing I do is define the interface Event:
public interface Event extends ComplexEObject
{
}
It's not much of an interface but it turns out that ComplexEObject already includes the contract that I will need: the requirement that every event have a title:
public Title title();
I still want to define this interface as it serves as a mechanism for qualifying event types.
Next, let's go ahead and define a new object to model a BOF:
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);
}
}
Finally, let's retrofit our existing class Talk:
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();
As you can see from the above diff, all I had to do was specify that Talk implement Event.
Finally, remaining is broadening Session's definition. Rather than define an association to a Talk, we'll revise that to be be an association to an Event. I won't show you all the edits. Here's the gist of the change:
- public Talk getTalk() { return talk; }
- public void setTalk(Talk talk)
+ public Event getEvent() { return event; }
+ public void setEvent(Event event)
And that's it. Now let's take a look at how these changes manifest themselves in our application:
Polymorphic Queries: Listing Events

Discussion:
JMatter models the polymorphic association using Hibernate's implicit polymorphism mechanism, otherwise known as any associations.
Polymorphic Instantiation: Creating a new Event

Discussion:
JMatter will prompt the user whether they wish to create a talk or a BOF.
The calendar:

Discussion:
We see two events on this calendar. The first is a talk on JMatter, and the second is a BOF on Programming. :-)


