Creating a Program


Suppose you want to implement your program by specifying an association between classes in a UML diagram. We want to show you how a UML diagram with associations is turned into code. Refer to the class diagram in Figure 4-17, the following bulleted list, and Listing 4-1 to see how this simple model of client and crash dummy becomes program elements implemented in Java programming code:


Figure 4-17: Class Diagram of clients and crash dummies.

  • Classes: The Client and CrashDummy classes become classes in the Java code in statements such as the following:

     public class Client 
  • Associations: The rents, orders, and blocks associations become combinations of attributes. For example, you implement the blocks association in both directions by declaring the attributes public CrashDummy leader and public CrashDummy follower[] within the CrashDummy class. The diagram shows you can only navigate from Client to CrashDummy (and not the other way around); the rents and orders associations are implemented only in the Client class, as follows:

     public Btree orderedDummies; public List rentedDummy; 
  • Roles: Notice that we use the role names as the names of the attributes used for implementing an association. So the role of leader is implemented as the name of the reference attribute leader by making the following declaration:

     public CrashDummy leader. 
  • The orderedDummy, rentedDummy, and follower roles are also handled as attributes, along the following lines:

     public Btree orderedDummies; public List rentedDummy; public CrashDummy follower[]; 
  • Qualifier: The size qualifier is implemented as Private Integer Size so it is an attribute of CrashDummy. The qualification aspects are implemented using a Btree class named orderedDummies. The Btree class allows you to associate a value for the size qualifier with an instance of CrashDummy. Then, the Btree is used to lookup a CrashDummy by its size.

  • Multiplicity: Finally, the multiplicity is handled by using the following:

    • A simple reference pointer, as in Public CrashDummy leader where multiplicity is to 0..1 or 1.

    • An array as the default for handling multiplicities of more than one, as in Public CrashDummy follower[].

    • A designer-defined container, such as List or B-tree.

What would the diagram in Figure 4-17 look like in a programming language such as Java? Well, if you convert classes to classes and associations to references, then you generate code that looks similar to Listing 4-1.

Listing 4-1: Java Code for Simple Associations

start example
 public class Client  {    public B-tree orderedDummies;    public List rentedDummy;           public Client() {    } } public class CrashDummy  {    public CrashDummy leader;    public CrashDummy follower[];    Private Integer Size;           public CrashDummy() {    } } 
end example




UML 2 for Dummies
UML 2 For Dummies
ISBN: 0764526146
EAN: 2147483647
Year: 2006
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net