20.3. Creating Class Definitions from DCDs

At the very least, DCDs depict the class or interface name, superclasses, method signatures, and simple attributes of a class. This is sufficient to create a basic class definition in an object-oriented programming language. Later discussion will explore the addition of interface and namespace (or package) information, among other details.

Defining a Class with Methods and Simple Attributes

From the DCD, a mapping to the basic attribute definitions (simple Java instance fields) and method signatures for the Java definition of SalesLineItem is straightforward, as shown in Figure 20.2.

Figure 20.2. SalesLineItem in Java.


Note the addition in the source code of the Java constructor SalesLineItem(...). It is derived from the create(spec, qty) message sent to a SalesLineItem in the enterItem interaction diagram. This indicates, in Java, that a constructor supporting these parameters is required. The create method is often excluded from the class diagram because of its commonality and multiple interpretations, depending on the target language.

Adding Reference Attributes

A reference attribute is an attribute that refers to another complex object, not to a primitive type such as a String, Number, and so on.

The reference attributes of a class are suggested by the associations and navigability in a class diagram.


For example, a SalesLineItem has an association to a ProductSpecification, with navigability to it. It is common to interpret this as a reference attribute in class SalesLineItem that refers to a ProductSpecification instance (see Figure 20.3).

Figure 20.3. Adding reference attributes.


In Java, this means that an instance field referring to a ProductSpecification instance is suggested.

Note that reference attributes of a class are often implied, rather than explicit, in a DCD.


For example, although we have added an instance field to the Java definition of SalesLineItem to point to a ProductSpecification, it is not explicitly declared as an attribute in the attribute section of the class box. There is a suggested attribute visibility—indicated by the association and navigability—which is explicitly defined as an attribute during the code generation phase.

Reference Attributes and Role Names

The next iteration will explore the concept of role names in static structure diagrams. Each end of an association is called a role. Briefly, a role name is a name that identifies the role and often provides some semantic context as to the nature of the role.

If a role name is present in a class diagram, use it as the basis for the name of the reference attribute during code generation, as shown in Figure 20.4.

Figure 20.4. Role names may be used to generate instance variable names.


Mapping Attributes

The Sale class illustrates that in some cases one must consider the mapping of attributes from the design to the code in different languages. Figure 20.5 illustrates the problem and its resolution.

Figure 20.5. Mapping date and time to Java.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.82.83