CTCMap: A New Object Idiom

Author: Martyn Cutcher

email martyn@cutthecrap.biz

website www.cutthecrap.biz

The CTCMap class provides direct declarative support for the implementation of object associations.

What Does This Mean?

Okay, we need to take a couple of steps back.

When you define an object model, you are aware of the kind of associations that exist between different objects.

When you implement such a model using java you will select a number of utility objects, such as ArrayList and LinkedList to help you to provide such associations. You will probably write a number of different methods to help you maintain and manage the associations.

If you are using a database, you will probably not worry about the java associations. Instead you may query the data stored in different tables.

In a database, the object associations are implicit in the data stored in the tables. The data is used to imply the intended association:

SELECT * FROM EMPLOYEES WHERE COMPANY = "The Company".

...or somesuch.

The reason this works is because the database provides a global access mechanism.

Now, it would be possible to do something similar with an "in-memory" database model, but that would really be a little unimaginative.

With CTCMap the associations are managed directly using a combination of double-linked lists and property keys.

The effect of these structures can be demonstrated simply:

import cutthecrap.ctc.CTCMap;

public class Demo {
  public void main(String[] args) {
    CTCMap c = CTCMap();
    c.set("name", "A CTCMap");
    
    CTCMap p = CTCMap();
    p.set("name", "Another CTCMap");
    
    c.set("parent", p);
    
    System.out.println("The object '" + p.get("name") 
      + "' is refernced by 'parent' by "
      + p.getLinkSet().size() + " objects" );
  }
}

Running java Demo should result in

The object 'Another CTCMap' is referenced by 'parent' by 1 objects

printed to the console.

So How Is This A New Idiom?

Well an idiom can be thought of as a low-level pattern that provides some useful functionality. Hence, for example, the "intelligent pointer" idiom used by many C++ programmers.

The common idioms that are used by java programmers include: classes, iterations, lists, maps, threads and exceptions.

Useful idioms support the use of a specific paradigm - such as "procedural programming".

As mentioned already, programmers have learnt how to use the various idioms available to implement systems. But aside from the "class" idiom, none of the patterns available really support "object orient programming". For this an approach to representing object associations in a sensible way is required. This is what CTCMap addresses.

How Should CTCMap Be Used?

The most obvious use for CTCMap is as a base class to be extended to implement required Interfaces.

public class Child extends CTCMap {
  static final String m_parent = "child/parent":
  
  public void setParent(Parent parent) {
    set(m_parent, parent);
  }
  
  public Parent getParent() {
    return (Parent) get(m_parent);
  }
}

public class Parent extends CTCMap {
  Iterator getChildren() {
    return getLinkSet(Child.m_parent).iterator();
  }
}

Alternatively it could be used as a private "connection" object to which requests are delegated. With this approach each "public" object would maintain a single private CTCMap, these objects would themselves be extended CTCMaps to support retrieval of their "real" objects.

public class DelegateCTCMap extends CTCMap {
  Object m_real;
  public DelegateCTCMap(Object link) {
    m_real = link;
  }
  Object getReal() {
    return m_real;
  }
}

But this approach does require significant protocol support.

The Generic Persistent Object Model

CTCMap implements a subset of the generic model. For more information visit the main website.