Example

This C++ example shows classes Faculty, Department, Teacher, and Course with relations among them implemented in the new style. To make this example simple, we will first implement all these relations  as a doubly linked list of Children that know their Parent, and call this data arrangement Aggregate:

 

This  often occuring data structure does not exist in any standard library because it inserts members, in this case pointers/references, into two different classes, and existing templates/generics cannot do that.

// Schema: Structured Data declaration 
Association Aggregate<Faculty,Department>   departments;
Association Aggregate<Department,Teacher>   teachers;
Association Aggregate<Department,Course>    courses;
Association Aggregate<Teacher,Course>   teaches;

#include zzgen.h // output of pre-processor – see Note 1

// Application Classes: declaration
class Faculty {
    ZZ_Faculty ZZds; // makes this class Structured Data – see Note 2
    // all other code as usual
};

class Department  {
    ZZ_Department ZZds; // makes this class Structured Data – see Note 2
    // all other code as usual
};

// same for other application classes 

// Application Code: use methods such as addTail() or remove() from the Aggregate
Department* d=new Department;
Teacher*    t=new Teacher;
Course*     c=new course;

teachers::addTail(d,t);
courses::addTail(d,c);
teaches::addTail(t,c);

Department* dn=new Department;
courses::remove(c);
courses::addTail(dn,c);

Note 1: Based on the schema, a simple preprocessor creates a file with the declarations of auxiliary classes such as ZZ_Faculty or ZZ_Department. These classes hide all the pieces that implement the Structured Data. Application code is not modified by the preprocessor.

Note 2: This line must be in every Structured Data class, just repeating the class name.

Change of schema

Let’s assume that we want the Teachers to be grouped under the Faculty, not under Department as in the original schema, and instead of the Aggregate, we want Teachers to be in a hash table which supports faster searches. Changing schema is simple (in red):

Association Aggregate<Faculty,Department>  departments;
Association Hash<Faculty,Teacher>          teachers;
Association Aggregate<Department,Course>   courses;
Association Aggregate<Teacher,Course>      teaches;

Application code that uses the teachers relationship may need small code adjustments, however the compiler tells us where these changes are required.

UML class diagram

The schema is really a textual form of a UML class diagram. The Layout program, based on world class algorithms for the design of silicon circuits, reads the schema and creates an eye pleasing visual UML class diagram. This visual aid is a great help on projects with many interacting classes.

exampleUML.png

modified schema generated by Layout

Original schema generated by Layout