DOM: The Dynamic Object Model

Recall that the Types as Objects Pattern associates each instance of an Instance class with one instance of an InstanceType class, which is itself a subclass of Instance.

The Dynamic Object Model (DOM) is a refinement of the Types as Object pattern that provides each instance object with an attribute-value map. This is a map that associates attribute names (Strings) with attribute values (Instances). In addition, each instance type is provided with an attribute-type map. This is a map that associates attribute names (Strings) with attribute value types (InstanceTypes).

The InstanceType class is provided with a put method that allows users to put new associations in the attribute-type map and a method for getting the type associated with a given attribute name.

Finally, the Instance class is provided with setter and getter methods for setting and getting attribute values by name. Every instance must satisfy the constraint:

{self.get(name).type = self.type.get(name)}

A. Draw a UML class diagram showing DOM as described above. (Hint: use qualified associations).

B. Sketch an implementation of DOM in Java or C++.

C. Write a test harness for the code in part B showing how you would create the objects implied by the following domain description:

There are three types: person, string, boolean, and number. A person has four attributes name, age, male?, and spouse. Tom Smith is a 33 year-old man who is married to Barbara.

D. The DOM can be further enhanced by introducing the relationship:

InstanceType X inheritsFrom InstanceType Y

Modify the getter methods of Instance and InstanceType to accommodate inheritance.

E. Declare an abstract method class so that methods could also be attributes. Methods are invokable instances.

self.get(methodName).invoke(self, args)

F. Write a test harness for the code in parts D and E showing how you would create and test the objects described below:

Monsters in a computer game have an integer attribute called health. A monster dies when its health is below 1. When a monster attacks it strikes its victim. Attacking reduces a monster's health by 10%.

A vampire is a type of monster that bites when it attacks. Vampires can be males or females.

Godzilla is a monster with health = 10%. Daracula is a male vampire with health = 100%. Both attack a victim.