Section 13.1. Packages


13.1. Packages

Suppose that during the design of a CMS, you decide to keep classes related to security (for example, performing user authentication) grouped together. Figure 13-2 shows the security package and a few other packages from the CMS in UML. The symbol for a package is a folder with a tab. The name of the package is written inside the folder.

Figure 13-2. Packages in a CMS; each package corresponds to a specific system concern


13.1.1. Contents of a Package

Packages organize UML elements, such as classes, and the contents of a package can be drawn inside the package or outside the package attached by a line, as shown in Figure 13-3. If you draw the elements inside the package, write the name of the package in the folder tab.

The notation shown in Figure 13-3 is used to model Java classes belonging to a Java package. In Java, the package keyword at the beginning of a class specifies that a class is in a package. Example 13-1 shows a Java code sample corresponding to the Credentials class in Figure 13-3.

Figure 13-3. Two ways to show that the Credentials and IdentityVerifier classes are contained in the security package


Example 13-1. The Credentials class is located in the security package in this Java implementation

 package security;   public class Credentials {     ... } 

Packages can also contain other packages, as shown in Figure 13-4.

Figure 13-4. A package that contains another package


It's common to see deeply nested packages in enterprise applications. Java applications typically use the URL-in-reverse package naming convention (omitting the www part of the URL). For example, the ACME company with the URL http://www.acme.com would put all its packages under the acme package, which is under com, as shown in Figure 13-5.

Figure 13-5. Deeply nested packages are common in enterprise applications: the search and indexing packages are shown in a typical package structure for the ACME company


Even at this point, these packages consume a lot of space, and if you want to show classes inside the indexing package, each package containing it would have to expand in size accordingly. Luckily, there's an alternate notation that can be easier to work with. You can "flatten" nested packages to write them as packageA::packageB::packageC, and so on. This converts Figure 13-5 into the less cluttered Figure 13-6.

Figure 13-6. Flattening nested packages


13.1.2. UML Tool Variation

Currently, a small amount of UML tools don't support the notations shown in Figure 13-3. However, almost all tools can show that a class belongs to a package using one of the notations shown in Figure 13-7. The notation to the far right is the standard UML namespace notation, discussed next in "Namespaces and Classes Referring to Each Other."

Figure 13-7. Common ways UML tools show that a class belongs to a package


To specify the package that a class belongs to, most UML tools allow you to enter the package name in a class specification dialog or manually drag the class into the package it belongs to in a tree display of the model elements.




Learning UML 2.0
Learning UML 2.0
ISBN: 0596009828
EAN: 2147483647
Year: 2007
Pages: 175

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