Home > Articles > Programming > Java

This chapter is from the book

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 3–11 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 11Figure 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 3–12.

Figure 12Figure 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 3–13.

Figure 13Figure 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:

  • dependency
  • association
  • aggregation
  • composition

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.

Dependency

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 3–14.

Figure 14Figure 3-14 Object dependency.

Association

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 3–15).

Figure 15Figure 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 3–16 involve a line connecting the two classes with a diamond at the object that represents the greater whole.

Figure 16Figure 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 3–17.

Figure 17Figure 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 3–2 summarizes the relationships discussed in the previous sections.

Table 3-2 Object Relationships

Association

Description

Dependency

Short term, transitive relationship. Associated object is quickly discarded by the object which initiated the relationship. Associated object can exist on its own.

Association

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.

Aggregation

Long-term relationship and mutual dependency. Part of a greater whole. May participate in multiple aggregate relationships.

Composition

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 relationships—that 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 3–18.

Figure 18Figure 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 3–19.

Figure 19Figure 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 3–20, 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 20Figure 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 3–21.

Figure 21Figure 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 3–22 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 22Figure 3-22 Shopping cart class diagram with subsystem.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020