Monday, November 14, 2011

Component Diagram

Overview

Components are wired together by using an assembly connector to connect the required interface of one component with the provided interface of another component. This illustrates the service consumer - service provider relationship between the two components.
An assembly connector is a "connector between two components that defines that one component provides the services that another component requires. An assembly connector is a connector that is defined from a required interface or port to a provided interface or port."[1]
When using a component diagram to show the internal structure of a component, the provided and required interfaces of the encompassing component can delegate to the corresponding interfaces of the contained components.
A delegation connector is a "connector that links the external contract of a component (as specified by its ports) to the internal realization of that behavior by the component’s parts."[1]
The example above illustrates what a typical Insurance policy administration system might look like. Each of the components depicted in the above diagram may have other component diagrams illustrating their internal structure.


The basics
Drawing a component in UML 2 is now very similar to drawing a class on a class diagram. In fact, in UML 2 a component is merely a specialized version of the class concept. Which means that the notation rules that apply to the class classifier also apply to the component classifier. (If you read and understood my previous article [http://www-106.ibm.com/developerworks/rational/library/content/RationalEdge/sep04/bell/index.html] regarding structure diagrams in general, and class diagrams in particular, you are well under way to understanding component diagrams.)
In UML 2, a component is drawn as a rectangle with optional compartments stacked vertically. A high-level, abstracted view of a component in UML 2 can be modeled as just a rectangle with the component's name and the component stereotype text and/or icon. The component stereotype's text is «component»" and the component stereotype icon is a rectangle with two smaller rectangles protruding on its left side (the UML 1.4 notation element for a component). Figure 2 shows three different ways a component can be drawn using the UML 2 specification.

Figure 2: The different ways to draw a component's name compartment
The different ways to draw a component's name compartment
When drawing a component on a diagram, it is important that you always include the component stereotype text (the word "component" inside double angle brackets, as shown in Figure 2) and/or icon. The reason? In UML, a rectangle without any stereotype classifier is interpreted as a class element. The component stereotype and/or icon distinguishes this rectangle as a component element.
Modeling a component's interfaces Provided/Required
The Order components drawn in Figure 2 all represent valid notation elements; however, a typical component diagram includes more information. A component element can have additional compartments stacked below the name compartment. As mentioned earlier, a component is an autonomous unit that provides one or more public interfaces. The interfaces provided represent the formal contract of services the component provides to its consumers/clients. Figure 3 shows the Order component having a second compartment that denotes what interfaces the Order component provides and requires. [Note: Even though components are autonomous units they still may depend on the services provided by other components. Because of this, documenting a component's required interfaces is useful.]

Figure 3: The additional compartment here shows the interfaces that the Order component provides and requires
Additional compartment showing the interfaces that the Order component provides and requires
In the example Order component shown in Figure 3, the component provides the interfaces of OrderEntry and AccountPayable. Additionally, the component also requires another component that provides the Person interface. [Note: Figure 3 does not show the Order component in its complete context. In a real-world model the OrderEntry, AccountPayable, and Person interfaces would be present in the system's model.]
Another approach to modeling a component's interfaces
UML 2 has also introduced another way to show a component's provided and required interfaces. This second way builds off the single rectangle, with the component's name in it, and places what the UML 2 specification calls interface symbols" connected to the outside of the rectangle. This second approach is illustrated in Figure 4.

Figure 4: An alternative approach (compare with Figure 3) to showing a component's provided/required interfaces using interface symbols
Alternative approach to showing a component's provided/required interfaces using interface symbols
In this second approach the interface symbols with a complete circle at their end represent an interface that the component provides — this lollipop" symbol is shorthand for a realization relationship of an interface classifier. Interface symbols with only a half circle at their end (a.k.a. sockets) represent an interface that the component requires (in both cases, the interface's name is placed near the interface symbol itself). Even though Figure 4 looks much different from Figure 3, both figures provide the same information — i.e., the Order component provides two interfaces: OrderEntry and AccountPayable, and the Order component requires the Person interface.
Modeling a component's relationships
When showing a component's relationship with other components, the lollipop and socket notation must also include a dependency arrow (as used in the class diagram). On a component diagram with lollipops and sockets, note that the dependency arrow comes out of the consuming (requiring) socket and its arrow head connects with the provider's lollipop, as shown in Figure 5.

Figure 5: A component diagram that shows how the Order System component depends on other components
A component diagram that shows how the Order System component depends on other components
Figure 5 shows that the Order System component depends both on the Customer Repository and Inventory System components. Notice in Figure 5 the duplicated names of the interfaces CustomerLookup" and ProductAccessor." While this may seem unnecessarily repetitive in this example, the notation actually allows for different interfaces (and differing names) on each component depending on the implementation differences (e.g., one component provides an interface that is a subclass of a smaller required interface).
Subsystems
In UML 2 the subsystem classifier is a specialized version of a component classifier. Because of this, the subsystem notation element inherits all the same rules as the component notation element. The only difference is that a subsystem notation element has the keyword of subsystem" instead of component," as shown in Figure 6.

Figure 6: An example of a subsystem element
An example of a subsystem element
The UML 2 specification is quite vague on how a subsystem is different from a component. The specification does not treat a component or a subsystem any differently from a modeling perspective. Compared with UML 1.x, this UML 2 modeling ambiguity is new. But there's a reason. In UML 1.x, a subsystem was considered a package, and this package notation was confusing to many UML practitioners; hence UML 2 aligned subsystems as a specialized component, since this is how most UML 1.x users understood it. This change did introduce fuzziness into the picture, but this fuzziness is more of a reflection of reality versus a mistake in the UML 2 specification.
So right now you are probably scratching your head wondering when to use a component element versus a subsystem element. Quite frankly, I do not have a direct answer for you. I can tell you that the UML 2 specification says that the decision on when to use a component versus a subsystem is up to the methodology of the modeler. I personally like this answer because it helps ensure that UML stays methodology independent, which helps keep it universally usable in software development.
Beyond the basics
The component diagram is one of the easier-to-understand diagrams, so there is not much to cover beyond the basics. However, there is one area you may consider somewhat advanced.
Showing a component's internal structure
There will be times when it makes sense to display a component's internal structure. In my previous article on the class diagram, I showed how to model a class's internal structure; here I will focus on how to model a component's internal structure when it is composed of other components.
To show a component's inner structure, you merely draw the component larger than normal and place the inner parts inside the name compartment of the encompassing component. Figure 7 show's the Store's component inner structure.

Figure 7: This component's inner structure is composed of other components
Shows component's inner structure is composed of other components
Using the example shown in Figure 7, the Store component provides the interface of OrderEntry and requires the interface of Account. The Store component is made up of three components: Order, Customer, and Product components. Notice how the Store's OrderEntry and Account interface symbols have a square on the edge of the component. This square is called a port. In a simplistic sense, ports provide a way to model how a component's provided/required interfaces relate to its internal parts. [Note: In actuality, ports are applicable to any type of classifier (i.e., to a class or some other classifier your model might have). To keep this article simple, I refer to ports in their use on component classifiers.
By using a port, our diagram is able to de-couple the internals of the Store component from external entities. In Figure 7, the OrderEntry port delegates to the Order component's OrderEntry interface for processing. Also, the internal Customer component's required Account interface is delegated to the Store component's required Account interface port. By connecting to the Account port, the internals of the Store component (e.g. the Customer component) can have a local representative of some unknown external entity which implements the port's interface. The required Account interface will be implemented by a component outside of the Store component. [Note: Typically, when you draw a dependency relationship between a port and an interface, the dependent (requiring) interface will handle all the processing logic at execution time. However, this is not a hard and fast rule — it is completely acceptable for the encompassing component (e.g., the Store component in our example) to have its own processing logic instead of merely delegating the processing to the dependant interface.]
You will also notice in Figure 7 that the interconnections between the inner components are different from those shown in Figure 5. This is because these depictions of internal structures are really collaboration diagrams nested inside the classifier (a component, in our case), since collaboration diagrams show instances or roles of classifiers. The relationship modeled between the internal components is drawn with what UML calls an assembly connector." An assembly connector ties one component's provided interface with another component's required interface. Assembly connectors are drawn as lollipop and socket symbols next to each other. Drawing these assembly connectors in this manner makes the lollipop and socket symbols very easy to read.
Conclusion
The component diagram is a very important diagram that architects will often create early in a project. However, the component diagram's usefulness spans the life of the system. Component diagrams are invaluable because they model and document a system's architecture. Because component diagrams document a system's architecture, the developers and the eventual system administrators of the system find this work product-critical in helping them understand the system.

No comments:

Post a Comment