Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

Graphs Versus Objects


Objects Of My Affection

2008 seems a good time to experiment with a truly futuristic idea—an online store! First, we'll need sellers and buyers (using a Person class with a name field) with something to sell and purchase. Figure 1, the object-oriented UML class diagram for our example, depicts the knowledge representation.

Here we declare the representation in Java. Note the mixture of representation with actual processing. As the complexity grows, this becomes difficult to separate and improve. We use line numbers to denote that the following is an abridged code version:

3 public class PurchasableItem {
5 private float cost;
6 private String manufacturer, label;
8 public PurchasableItem (String label, float cost, String manufacturer) {
// Call appropriate setters
13 }
// Typical getters and setters follow ...
35 }

Figure 1: UML class diagram.

Add a Transaction class and we're ready to open our doors:

3 public class Transaction {
7 private Person buyer, seller;
8 private PurchasableItem containsItem;
9 private Date sellDate, shipDate;
10 private String label;
12 public Transaction(Person buyer, Person seller, PurchasableItem containsItem, Date sellDate, Date shipDate) {
13    super();
14    setBuyer(buyer);    // Call appropriate setters
                          // with remaining parameters ...
19  }
59 }

Let's watch our first sale happen as we create instances provided by the class definitions. (The full code is online.)


//Matt ...
Person seller = new Person("Matt Fisher");
// ... is selling his special toaster ...
PurchasableItem toaster = new PurchasableItem("High-wolf shiny toaster", (float)49.95, "Dualit");
pItems.add(toaster); 
// pItems is a HashSet of PurchasableItems
// John ...
Person buyer = new Person("John Hebeler");
// ... is buying the toaster very soon!
Calendar sellDateCalendar = Calendar.getInstance();
sellDateCalendar.set(2008, 3, 24, 10, 0, 0);
Date sellDate = sellDateCalendar.getTime(); 
// similar code follows for shipDate
Transaction tran = new Transaction(buyer, seller, toaster, sellDate, shipDate);
transactions.add(tran);


Keeping track of our marketplace requires some custom queries that interrogate our objects:


// Now, how many items has John bought?
     int count = 0;
     for (Transaction t : transactions) {
         if (t.getBuyer() == buyer) {
             count++;
         }
     }
   System.out.println("John has purchased " + count + " item(s)");
     // Now, what has John bought?
     count = 0;
     System.out.println("John has bought:");
     for (Transaction t : transactions) {
         if (t.getBuyer() == buyer) {
           System.out.println("   " t.getContainsItem().getLabel());
         }
     }


The resulting output is:


John has purchased 1 item(s)
John has bought:
   High-wolf shiny toaster
 

Our objects quickly created a basic solution. Additional queries require additional coding. This works to constrain the variety and power of the questions. As we continue to code, we realize we created a proprietary solution. Integration becomes difficult in two ways—more of the same instances and different types or classes of instances. The former requires custom integration code and is subject to our chosen storage method (for this example, it is merely in-memory arrays). The latter, different type of classes, creates an N2 problem as we write custom code to combine objects from a different class. (Imagine that a similar solution did not create a transaction class but rather a user purchase class.)


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.