Managing Use Case Complexity
A use case should represent a discrete benefit for the actor. A well-defined use case should be expressed succinctly, and the action taking place should happen within a single session for the actor. If that is not the case and a use case involves excessive complexity, then it may require refactoring the use case into one or more separate use cases. Fortunately, UML provides diagrammatic syntax to help manage this complexity.
Use cases can include other use cases, or a use case may extend another use case; these techniques allow use cases to be designed in such a way that complexity of individual use cases is reduced and the overall model retains simplicity. An include operation indicates that the use case absorbs the behavior of another use case. Alternatively, an extend operation indicates that the use case depends on the behavior of the base use case and may optionally add the execution of its events to it.
Figure 311 contains an example of a use case include. In this example, the actor initiates the "navigate system" use case. This use case event includes the events of viewing existing messages and create or deleting a message. Using this technique, the complexity of a use case can be managed using the "divide and conquer" approach, where complex events are broken into smaller, more manageable use case events, which are included into other use case events.
Figure 3-11 Use case include.
Abstraction is an important object-oriented technique that is often used to simplify complex behaviors. The benefits of abstraction can also have an impact on use case analysis. A complex use case can be made abstract and then other use cases can call on its abstract behavior, as shown in Figure 312.
Figure 3-12 Use case abstraction.
Actors can also be generalized in use cases. If different actors have similar interactions with the system, then consolidating those interactions into a single actor could be a useful abstraction. Use case generalization can be used to model this behavior, as shown in Figure 313.
Figure 3-13 Use case actor generalization.
Evaluating Object Relationships
As objects are being identified during the analytical process, relationships between the objects will also be identified. As you identify objects during the OOAD process, you must also identify relationships between objects. These relationships or associations between objects are important and represent one of the key analysis areas where we can exert control over coupling and facilitate cohesion (discussed in more detail later in this chapter).
When an object creates a reference to another object, even though it may carry the reference for only a short time, an object association exists between the two objects. Careful selection of object relationships can lead to more robust, flexible applications. The selection of these relationships have additional meaning when working with J2EE, since the relationships between some objects must be realized through network connections. Passing objects to and from J2EE business tier components (EJBs) is by value, not by reference, as is done in a local JVM. This creates a performance penalty, albeit a very manageable penalty, that must be factored into decisions on how to use these components.
Though Java is a true object-oriented language that thoroughly supports inheritance, careful thought should be given to the use of this feature, since in many cases an alternative is to use a more flexible object relationship. The commonly identified object relationships are as follows:
The distinction between these relationships is based on the duration and the nature of the relationship. This list identifies the relationships from weakest to strongest as explained below.
An object dependency exists when there is a short-term relationship between the objects. For instance, the relationship between a shopping cart and a checkout object would be short term, since once the checkout operation is complete, the checkout object would no longer be needed. The same relationship would exist for a stock transfer object that needed to use a stock item object to get the information on the stock item being transferred. The stock transfer object would have a dependency relationship with the stock item object; the stock transfer object would read the transfer information and could then discard the stock item object and continue processing. Dependency is shown in UML with a dashed line with an arrow between the client class and the class use, as shown in Figure 314.
Figure 3-14 Object dependency.
An object association represents a more long-term association than the dependency relationship. The controlling object will obtain a reference to the association object and then use the reference to call methods on the object. The relationship between a car and a driver is representative of this relationship. The car will have a driver who will be associated with the car for a period of time.
Another example is a business object that performs an end-of-day processing routine by reading the contents of a message queue. The object that provides access to the message queue would have an association with the business object, since the relationship would persist for some time until the message queue was exhausted, at which time it could be discarded (see Figure 315).
Figure 3-15 Object association.
Aggregation and Composition
The aggregation and composition relationships involve a tighter binding between the related objects. The related objects have a long-term relationship and have some level of mutual dependency, which may be exclusive, that defines their existence.
With an aggregation relationship, the contained object is part of the greater whole. There is a mutual dependency between the two objects, but the contained object can participate in other aggregate relationships and may exist independently of the whole. For example, a FileReader object that has been created using a File object represents a mutual dependency where the two objects combine to create a useful mechanism for reading characters from a file. The UML symbols for expressing this relationship as shown in Figure 316 involve a line connecting the two classes with a diamond at the object that represents the greater whole.
Figure 3-16 Object aggregation.
With the composition relationship, the client object is owned by the greater whole. The contained object cannot participate in more than one compositional relationship. An example of this is a customer object and its related address object; the address object cannot exist without a customer object. This relationship is shown with a darkened diamond at the object, which represents the greater whole, as shown in Figure 317.
Figure 3-17 Object composition.
To summarize this nebulous and esoteric topic, a variety of associations may exist, the differences lying in the duration of the association. Table 32 summarizes the relationships discussed in the previous sections.
Table 3-2 Object Relationships
Short term, transitive relationship. Associated object is quickly discarded by the object which initiated the relationship. Associated object can exist on its own.
More long-term relationship than dependency. The controlling object will use the associated object to make method calls. Associated object can exist on its own.
Long-term relationship and mutual dependency. Part of a greater whole. May participate in multiple aggregate relationships.
Long-term relationship and mutual dependency. Part of a greater whole. Can participate in only one compositional relationship. May not be able to exist on its own.
Navigability of Object Relationships
Another topic related to the concept of object associations is the navigability of objects relationshipsthat is, the direction in which object relationships operate. In many cases, the contained object can be used by the container object but not vice versa. For instance, in the composition relationship described earlier between a customer and an address object, the customer object would use the address object to reference the address information, but the address object would have no reason to reference the customer object. This one-way navigability is represented in UML with an arrow towards the direction of the navigability, as shown in Figure 318.
Figure 3-18 Navigation of a composite object.
Interestingly enough, the UML standard is not specific about the meaning of a line with no arrows. A line with no arrow, as shown in Figure 3-15 could indicate bidirectional navigability or possibly even unknown or undecided navigability. The project team should decide on the meaning of this symbol at the start of the project and then maintain consistency throughout the effort.
Multiplicity of Object Relationships
An object association may involve only one of each object, the controlling object and the associated object, or it may involve multiple instances of either. Multiplicity is used to indicate the number of objects that may exist in a relationship. An object may contain one and only one instance of another object, or it may contain several instances of another object.
Following the example of the customer and address object relationship shown earlier, a customer object could have more than one related address object to manage the inclusion of a home address and a business address for the customer. The diagram should indicate the number of instances of an associated object that may exist. This is represented by a set of numbers near the referenced object, which indicates its multiplicity, as shown in Figure 319.
Figure 3-19 A composite object with multiplicity.
Object Association Versus Object Inheritance
You should use object association to extend the capabilities of a class whenever possible. There are alternatives, some that are worth consideration and others that represent poor programming. One alternative is to simply let the object design grow more complex. This has the downside of reducing the manageability, flexibility, and extensibility of the code contrary to the key goals for our J2EE development effort.
Another alternative, and one worth consideration, is the use of the class inheritance. Class inheritance allows one class to be extended with the capabilities of another and is a perfectly acceptable OO design. But there are two issues with this approach.
One issue is that class inheritance adds overhead to the object creation process. For each class in the inheritance hierarchy, the JVM must find the appropriate members and call the appropriate constructor. Relative to a more simple class design, this adds overhead.
The other issue is that Java limits us to a single inheritance model, and thus for complex classes that may be required to exhibit several different behaviors of distinct classes, inheritance is not a good alternative. For instance, if we are modeling a business customer as an object and the customer may also be a preferred retail customer who is also a B2B customer, using inheritance to model this object requires at least three levels of inheritance hierarchy (since Java does not support multiple inheritance). But modeling this object using object association would simply involve adding the distinct business objects (the preferred retail customer object, the B2B customer object) to be members of the business customer object.
There are cases where class inheritance is appropriate. When an important business domain object must be created and it clearly fits the inheritance model (for instance, the classic manager extends employee hierarchy, or a preferred customer extends customer), then inheritance is a good choice. But for a large number of cases, object association is the preferable technique.
Cohesion and Coupling
A mentioned in Chapter 2, it is important to group objects in such a way that members of the group are logically related. If the relationships between members of the group are sound, then we have strong cohesion in the group. But if the relationships are weak or artificial, we have weak cohesion.
The concept of cohesion is applied to different levels of the analytical process. It is applied to an object design (a class) and its members, it is applied to objects in a group (a package), and for large systems it can be applied to packages in a module.
The term coupling applies to the level of dependence between different groups. If there is a high degree of interdependence between groups, the groups are described as having tight coupling. As with cohesion, coupling can be applied to a number of different levels, to members of a class, to objects in a package, and to components in a module.
Shopping Cart Example
Consider the following example for an e-commerce site shopping cart. Though this is a well-worn example, it is illustrative of some of the design issues that must be tackled in creating a J2EE application. The first UML diagram, shown in Figure 320, shows the a subset of the various classes that will be used to create the application. The shopping cart class (Cart) uses a catalog that lists the available stock items for purchase and uses an order object to process the order. For purposes of this example, the specific types of relationship (association, dependency, aggregation, composition) are not identified; they will be clarified in a later design step.
Figure 3-20 Shopping cart class diagram with subsystem.
The question that arises is, how do we group these objects into packages? If we create a Cart package, which makes sense given the cohesion of the objects in that group, we must still manage the relationships with the stock classes, which we will group into the Stock package. The relationships between the Catalog and StockItem and Cart and Order represent coupling between the packages. While this may not seem like much to be concerned about, order processing and selection of stock items for sale often involve the application of a great deal of business logic; this is business logic that, if placed in our ShoppingCart package, would represent a high degree of coupling between the ShoppingCart package and the Stock package. We therefore decide to group classes based on their usage, which is ultimately reflected in cohesion between the members of the package, as shown in Figure 321.
Figure 3-21 Shopping cart class diagram with subsystem.
Since the business logic cannot be eliminated, and some degree of coupling between the two packages is required, the solution is to create a subsystem to manage the processing. This subsystem would reside in the Stock package and would encapsulate the business logic of the access to its package members. This is shown in Figure 322 with the inclusion of the StockAccess class. This object manages access to all stock objects and applies any business logic that is necessary. It exposes a small, concise interface to the other packages that contain members that must access Stock objects. This approach reduces the exposure, the friction, between this package and the other packages, and should there be a change to the Stock package, it is much less likely to affect the other packages.
Figure 3-22 Shopping cart class diagram with subsystem.