Component
Component
In previous hours, you learned about diagrams that deal with conceptual entities.
For example, a class diagram represents a concept—an abstraction of items that fit
into a category. A state diagram also represents a concept—changes in the state of
an object.
In this hour, you’re going to learn about a UML diagram that represents a different
kind of entity: a software component.
In UML 1.x, data files, tables, executables, documents, and dynamic link libraries were
defined as components. In fact, modelers used to classify these kinds of items as deploy-
ment components, work product components, and execution components. UML 2.0 refers to
them instead as artifacts—pieces of information that a system uses or produces.
198 Hour 12
1. Clients can envision the structure and the functionality in the finished system.
3. Technical writers who have to provide documentation and help files can
understand what they’re writing about.
4. You’re ready for reuse.
Let’s explore that last one. One of the most important aspects of components is
the potential they provide for reusability. In today’s rapid-fire business arena, the
quicker you bring a system to fruition, the greater your competitive edge. If you
can build a component for one system and reuse it in another, you contribute to
that edge. Taking the time and the effort to model a component helps reuse occur.
Reviewing Interfaces
I elaborated on this idea in Hour 5, “Understanding Aggregations, Composites,
Interfaces, and Realizations.” As I mentioned then, an interface is a set of opera-
tions that allows you to access a class’s behavior—like the control knob that
enables you to get a washing machine to perform washing machine–related oper-
ations. Think of an interface as a class that only has operations—no attributes.
Bottom line: The interface is a set of operations that a class presents to other
classes.
Working with Component Diagrams 199
Here’s another important point: A component can make its interface available so
that other components can utilize the interface’s operations. In other words, a
component can access the services of another component. The component that
offers the services is said to present a provided interface. The component that
accesses the services is said to use a required interface.
This is also a good illustration of reuse. You can reuse a component in another
system (like the replacement engine for the sports car) if the new system can
access the reused component through that component’s interfaces. If you can
refine a component’s interfaces so that a wide array of other components can
access them, you can engineer that component for reuse in development projects
across your whole enterprise.
This is where modeling interfaces comes in handy. Life is easier for a developer
trying to replace or reuse a component if the component’s interface information is
readily available in the form of a model. If not, the developer has to go through
the time-consuming process of stepping through code.
FIGURE 12.1
The component
icon in UML 1.x
and the two ver-
sions of the com-
ponent icon in
UML 2.0.
Figure 12.2 shows that if the component is a member of a package, you can pre-
fix the component’s name with the name of the package. You can also show the
component’s operations in a separate panel.
Working with Component Diagrams 201
FIGURE 12.2
Adding information
to the component
icon.
Speaking of artifacts, Figure 12.3 shows a couple of ways to represent them, and it
also shows how to model the relationship between a particular kind of artifact
(an executable) and the component it implements. As you can see, you can place
a notation symbol in the artifact icon, analogous to the UML 1.x component
symbol in the component icon.
FIGURE 12.3
«Artifact» «Executable» Modeling the rela-
tionship between
WordProcessor.exe WordProcessor an artifact and a
component.
«Implement» «Implement»
«Component» «Component»
WordProcessor WordProcessor
Representing Interfaces
A component and the interfaces it realizes can be represented in two ways. The
first shows the interface as a rectangle that contains interface-related informa-
tion. It’s connected to the component by the dashed line and large open triangle
that indicate realization. (See Figure 12.4.)
202 Hour 12
FIGURE 12.4
You can represent «Component» «Interface»
an interface as a Key
rectangle that
contains Calculator
information, stateChanged()
connected to the
component by a
realization arrow.
Figure 12.5 shows the second way. It’s iconic: You represent the interface as a
small circle connected to the component by a solid line. (Compare Figures 12.4
and 12.5 with Figures 5.6 and 5.7.)
FIGURE 12.5
You can represent «Component»
an interface as a
small circle Key
Calculator
connected to the
component by a
solid line
FIGURE 12.6
«Component» «Interface» «Component» Two ways of show-
Key ing realization and
dependency in the
Calculator Robot same diagram.
stateChanged()
«Component» «Component»
Key
Calculator Robot
FIGURE 12.7
«Component» «Component» A white box view of
Calculator Robot the components in
Figure 12.6.
«provided interface» «required interface»
Key Key
The example comes from Rogers’s Hour 16 (“Building a Complex User Interface”).
The Java code creates an application called ColorSlide. This is a set of three slid-
ers that enable you to mix amounts of red, green, and blue to create a color. One
slider corresponds to each of those colors. The location of each slider determines
the amount of its color that goes into the mix. The created color appears in a
panel below the sliders.
204 Hour 12
Figure 12.8, taken from Rogers’s book, shows the finished product. Of course, the
figure is in shades of gray, so you can’t actually see the created color. The posi-
tioning of the sliders in the figure creates North Texas Mean Green, a color that
apparently holds great significance to students and alumni of the University of
North Texas.
FIGURE 12.8
Rogers
Cadenhead’s
ColorSlide
application (from
Teach Yourself Java
2 in 24 Hours, Third
Edition).
To help you understand the thought process behind this program, I’ll take you
through a sequence of component diagrams. The objective is for you to see
how the program takes shape and at the same time learn some modeling
techniques.
Figure 12.9 sets the stage by showing the packages that supply the Java ele-
ments used in the program. The acronym awt stands for “abstract windowing
toolkit,” a group of components that display and control a graphic user inter-
face (GUI). The specific components for this program are Color (which displays
a color), GridLayout and FlowLayout (which arrange the elements in the GUI),
and Graphics and Graphics2D (which paint the GUI—that is, they render it
onscreen).
The name on the tab of the other major package, swing, is a group of compo-
nents that you can add to a graphic user interface. The names of the compo-
nents in the package in this figure are pretty self-explanatory: JSlider is a slid-
er, JFrame is a frame, JPanel is a panel (an area within the frame), and JLabel
is a label.
Working with Component Diagrams 205
«Component» «Component»
GridLayout JLabel swing.event
«Component»
Graphics2D «Interface»
ChangeListener
«Component»
FlowLayout
Figure 12.10 shows the highest level of analysis for our components. It presents, in a
general way, the idea that ColorSlide inherits from JFrame and provides
ChangeListener, a required interface for a Person who interacts with ColorSlide.
Interaction between ChangeListener and ColorSlide takes place through a port. The
results of that interaction are sent to Color, as indicated by the arrow from the port to
Color. UML 2.0 refers to the ball-and-socket connection as an assembly connector and
to the arrow as a delegation connector. (The concept of connectors is new in UML 2.0.)
«Component»
ColorSlide
swing.event::ChangeListener
«Component»
awt::Color
206 Hour 12
Note that the package names appear as prefixes for the component names. (Strictly
speaking, awt is really java.awt and swing.event is really javax.swing.event, but I
decided to cut down on the clutter.) In Java, a program imports packages at the
beginning of the code, meaning that the programmer doesn’t have to specify the
package for each component throughout the program. The remaining figures
reflect the import of the packages and don’t include the package names.
Figure 12.11 moves to another level of analysis and shows that ColorSlide is an
aggregation whose components are JSlider, JPanel, and JLabel, with the indi-
cated multiplicities. Because the program deals with red, blue, and green, you can
see why the model specifies three sliders and three labels (one label per slider). It
specifies four panels because each slider has to have its own area, and the part
that displays the color has to have a designated area, too.
«Component»
ColorSlide
ChangeListener
«Component»
Color
3 4 3
Next, Figure 12.12 takes into account the laying out of the components and the
rendering of the GUI. The keyword «Arrange» shows that GridLayout and
FlowLayout arrange the panels, sliders, and labels. (I won’t go into the details of
Working with Component Diagrams 207
how they do the arranging.) The keyword «Paint» indicates that Graphics and
Graphics2D handle the rendering. (Again, I’ll skip the details.) These keywords
aren’t built into UML. I added them for clarity.
«Component» «Component»
ColorSlide «Paint» Graphics
ChangeListener
«Component»
Color
«Paint» «Component»
Graphics2D
3 4 3
If you’ve been following along closely, you might have become aware of a slight
disconnect. Figures 12.11 and 12.12 show JSlider as a component and
ChangeListener as the interface. A user can create colors only by manipulating
the sliders. Each time a slider moves, the movement causes the displayed color to
change. How do you show the relationships between the sliders and the interface?
The next level of analysis provides the answer and shows that the program cre-
ates instances of the components in the GUI. To model those instances, you can
use the icons for objects that you learned in Hour 3. What about those sliders? In
Java, when you create an object (like an instance of a slider) you can register it as
a change listener. In this case, registering a slider-object as a change listener
208 Hour 12
means that when the slider moves, the movement is noted and the displayed
color changes as a result.
Figure 12.13 shows this level of analysis and presents the objects that make up
ColorSlide. The ChangeListener is a required interface for the three instances of
JSlider. A delegation connector connects the port to current, an instance of
Color. The canvas object is an instance of a class called ColorPanel, a child class
of JPanel. For completeness, the figure shows the inheritance relationship
between ColorPanel and JPanel.
«Component»
ColorSlide
red:JSlider
«Component»
blue:JSlider blueLabel:JLabel bluePanel:JPanel Graphics2D
«Paint»
current:Color canvas:ColorPanel
«Component» «Component»
ColorPanel JPanel
Why bother creating the ColorPanel class? How, exactly, do you register an object
as an interface? How do those awt components work? You’ll just have to read
Rogers’s book to find out.
Working with Component Diagrams 209
Association
Generalization
Dependency
Realization Sequence
Grouping Extension
:Name1 1: Message()
«Stereotype»
Package {Constraint}
:Name2
Communication
Annotation
Note
Activity
Summary
A component is a modular part of a computer system, distinguishable from an
artifact, which is a piece of information that system uses or creates. Components
define a software system’s functionality.
A component provides interfaces that allow other components to access it. For an
accessing component, the interface is said to be required.
210 Hour 12
In UML 1.x, the component icon is a rectangle with two small rectangles overlaid
on its left side. In UML 2.0, the component icon is a rectangle with the keyword
«Component» near the top. For continuity in the near term, UML 2.0 recommends
using a tiny 1.x component icon in the upper right corner of the new icon. The
artifact icon is a rectangle with the keyword «Artifact» near the top. You can put
a note symbol in its upper right corner.
You can represent an interface in either of two ways. One representation is a rectan-
gle containing information about the interface and connected to the component
with a dashed line and an empty triangle. The other is a small circle connected to
the component with a solid line. In UML 2.0, you can use a ball-and-socket notation
to show that an interface is provided by one component and required by another.
The ball is the small circle I just mentioned. The socket is an open semi-circle con-
nected with a solid line to another component. The ball represents a provided inter-
face, whereas the socket represents a required interface.