• Tidak ada hasil yang ditemukan

CHAPTER

UCP 5 UUCP TCF EF For the running example we obtain

6.5 Collections

Simple collections of objects that do not have any particular meaning or structure should not be, in principle, represented as concepts, but as associations. Any association role already represents a collection of objects of the associated class. Thus, considering again the example ofFigure 6.15, fleetis a role that associates a set of cars to a person. The model presented inFigure 6.25is, there- fore, unnecessary and redundant.

Author Publisher

Book

*

*

*

1.. 1

FIGURE 6.23

The model ofFigure 6.22correctly represented with two binary associations.

Budgetltem 1

* *

* 1

1 Spendable

Project

FinancialYear

FIGURE 6.24

Model where a ternary association is replaced by a new concept.

132 CHAPTER 6 Conceptual Modeling: Fundamentals

Thus, unless the fleet has its own attributes or associations (for example a person having multi- ple fleets located in different places) the correct form to represent a simple collection of objects is through an association role, not through a class (Figure 6.26).

Thus, the team must be aware that if the collection has particular attributes or associations, then it must be represented as a concept. For example, anOrderis initially understood as a set of items, but anOrderalso has attributes such as issue date, total value, discount, etc., and associations such as the one with the customer, who may have multiple orders. If that is the case, the collection is a concept. However, a collection with no particular attributes or associations is just a collection, and therefore it is modeled as an association role.

Association roles may be thought as representing abstract data types(Liskov, 1974) in concep- tual models. In design models they would also represent concrete data types. Abstract data types are defined only by behavior, not by structure. They appear, for example, in the following:

• Thesetstructure, in which elements do not repeat and have no defined position.

• Thebagstructure, in which elements may repeat but still do not have a defined position.

• Theordered setstructure, in which elements do not repeat but have a defined position.

• Thesequencestructure, in which elements may repeat and have a defined position.

Theconcretedata types, on the other hand, are physical implementations for the abstract versions.

For example, a list may be implemented in many forms, such as an array, binary tree, linked list, etc.

In the case of concrete data types, besides behavior, there is also a physical structure that defines them.

6.5.1 Set

An association role, by default, is aset, that is, a structure where elements do not repeat and where there is no defined position for them. InFigure 6.26, thefleetis an example of a set.

If someone tries to link the same car to the same person a second time, nothing happens, because it already belongs to the set.

Fleet AVOID!

owner

Person 1 1 Car

1 * FIGURE 6.25

A simple collection being inadequately represented as a class.

Car

owner fleet

Person 1 *

FIGURE 6.26

A simpler model for the situation presented inFigure 6.25.

133 6.5 Collections

6.5.2 Ordered set

Suppose a book that is not yet in stock has a list of people interested in buying it as soon as it is available. If it cannot be assured that the quantity of books received will fill all the orders, then the fairer way to attend to these customers is by establishing a line from the oldest reservation to the newest. This requires that the customers have a position in the line.

If the same customer cannot reserve the same book more than once, then it is still a structure with no elements repeated, but now with defined positions. This leads to theordered setstructure.

In the diagram, the role may be marked with the constraint {ordered} to indicate that it is a collec- tion where elements do not repeat but have defined positions, as seen inFigure 6.27.

The constraint {ordered} over an association role means that the elements linked on that role have position (first, second, third,. . ., last), but still cannot be repeated.

6.5.3 Bag

There are situations in which the position of the elements does not matter, but each element may appear more than once in the collection. That structure is called abag.

For example, one may need to know how many people viewed the details of a book, and it may be important also to know how many times each one viewed it. It is not necessary to know who viewed it first, but the information on the number of views is necessary. That is a typical case for using a bag. Each time a person views the details of a book, a new link is added on the association defined as a bag. The model is shown inFigure 6.28.

6.5.4 Sequence

The sequence allows elements to be repeated and also assigns a position to them. For example, imagine that people that buy a given value worth of books qualify for receiving a gift, but a limited quantity of gifts is available. Thus, a list of people that qualify to receive the gift may be built and the gifts will be distributed to the first members of the list as soon as gifts become available. If someone again buys more than the limit established to receive a gift, she will enter the list again.

Book Customer

*{ordered} reservation

* FIGURE 6.27

An ordered set of reservations for a book.

Book Customer

*{bag} viewer

*

FIGURE 6.28 An example of a bag.

134 CHAPTER 6 Conceptual Modeling: Fundamentals

Thus, in that structure the position of an element is relevant and each element may appear more than once: this corresponds to a sequence structure, as seen inFigure 6.29.

The multiplicity

on the Livir side means that not all customers are qualified for a gift and that they may qualify more than once.

The sequence has two important special cases. When the first element to be removed is always the oldest in the sequence, as in the case ofFigure 6.29, then the structure is also a queue. UML does not define a constraint for queues, but a stereotype on the sequence constraint would do the job: {sequence}{queuec.

When the first element to be removed is the newest in the sequence, than it behaves like astack, and may be stereotyped as such: {sequence}{stackc. Queues and stacks that do not repeat ele- ments may be defined respectively as {ordered}{queuecand {ordered}{stackc.

A general list may have elements removed and inserted at any position. Queues and stacks have elements inserted and removed only at predetermined positions, as explained above, although, in some cases, all of their elements may be browsed.

A curiosity to be mentioned now is that in the real world most collections are strict setsand not sequences, but even so, the most used programming structure is still the sequence, and not the set, which indicates a gap between reality and the data structures used in programming.

6.5.5 Map

When a concept has a unique attribute, amapmay be created from that attribute to the concept, in such a way that it is easier to identify and find specific instances of the concept. For example, as the ISBN is unique for a book, associations toBookmay be qualified by the ISBN, meaning that instead of having a set of books, the origin class has access to a map that allows immediate identifi- cation of any book if its ISBN is known.Figure 6.30shows an example of qualified association.

The small rectangle at the left side of the association in Figure 6.30 represents a qualifier for the class at the opposite side of the association. Notice that instead of “

,” the role of the right side

* {sequence} qualifiedForGift Customer Livir

*

FIGURE 6.29

An example of a sequence.

Book 0..1

1 Livir

isbn

<<unique>> <<immutable>> +isbn : ISBN +title : String

+authorsName : String +year : Integer +pageCount : Natural FIGURE 6.30

An example of qualified association representing a map.

135 6.5 Collections

is now bounded by “0..1”; the association should now be read as follows: “for each possible ISBN there is no more than one book.” Some valid ISBN values may not be associated to any book, because they were not registered in the system or because the ISBN, although valid, has simply not yet been assigned to a book; however, if one ISBN is taken by a book, no other book may share it.

In practice, a qualified association is still an association “to many,” but the objects belonging to the role may be accessed by one of their attributes.

The role at the left side (the qualifier side) is marked with “1,” meaning that each book has a single ISBN, not less or more. This has to necessarily be true, because an ISBN is unique and not optional for the classBook, and therefore, it is mandatory and does not allow repetition.

The qualifier is not only a way to access objects in an easier way. It is also a powerful modeling tool. For example, consider a situation where a person may own many phones, but only one of each kind. How do we represent this? An elegant model for that situation is shown inFigure 6.31.

In the case ofFigure 6.31one person may have up to four phones, but just one of each kind. If more kinds are added to the enumeration later, then more phones may be associated to each person.

If the role on the right side was 1 instead of 0..1, then one person should necessarily have four phones, each one a different kind. That use of a qualified association with a mandatory role usually makes sense only when the qualifier is an enumeration or a finite set.

In the example ofFigure 6.30, the qualifier is an attribute of the qualified class, and in that case it is called aninternal qualifier. On the other hand, inFigure 6.31the qualifier is not an attribute of the class, and it is called anexternal qualifier.

6.5.6 Partition

InFigures 6.30 and 6.31, the multiplicity 1 or 0..1 on the right side of the figure means that the associ- ation is a mapping, that is, for each value of the key there is at most one instance of the qualified class.

What if instead of “1” or “0..1” the multiplicity was marked with “

”? In this case, the associa- tion would read as follows: “for each key there is asetof instances of the qualified class.”

For example, books may be classified by genre. As many books may belong to the same genre, the genre attribute may not be unique. But it is possible to define apartition on the set of books based on their genre, as shown inFigure 6.32.

<<enumeration>>

TelephoneKind

<<Constant>> +home

<<Constant>> +work

<<Constant>> +cellphone

<<Constant>> +message

Person

kind:TelephoneKind

Phone 0..1

1

+number : PhoneNumber FIGURE 6.31

Use of a qualifier as a modeling tool.

136 CHAPTER 6 Conceptual Modeling: Fundamentals

Figure 6.32 states that for each particular value of genre there is a set of books (with zero or more elements). The role multiplicity 1 on the left side indicates that every book has a genre.

6.5.7 Relation

Now, imagine that a book may have more than one genre, as, for example,The Hitchhiker’s Guide to the Galaxy(Adams, 1979), which can be classified both as science fiction and humor. For repre- senting the possibility of a book having more than one genre in the example of Figure 6.32, it would be sufficient to change the multiplicity on the left side of the association to

(if a genre is optional) or 1..

(if it is mandatory). Additionally, the genre should be removed from the attributes ofBook, becoming an external qualifier, because thegenre attribute should allow multiple values.

Figure 6.33shows the resulting relation.

<<unique>> +isbn : ISBN +genre : BookGenre genre:BookGenre

<<Constant>> +mystery

<<Constant>> +romance

<<Constant>> +science fiction

<<Constant>> +historical romance

<<Constant>> +thriller

<<Constant>> +humor

Livir

Book

1 *

<<enumeration>>

BookGenre

FIGURE 6.32

An example of a partition.

<<unique>> +isbn : ISBN genre:BookGenre

<<Constant>> +mystery

<<Constant>> +romance

<<Constant>> +science fiction

<<Constant>> +historical romance

<<Constant>> +thriller

<<Constant>> +humor

Livir

Book

1..* *

<<enumeration>>

BookGenre

FIGURE 6.33

An example of a relation.

137 6.5 Collections

The relation ofFigure 6.33states that a book may have one or more genres and a genre has a set of books associated with it. As a book may have more than one genre, then the qualifier should be necessarily external.