About stereotype in UML

Source: Internet
Author: User
Retain stereotype in Java programs

UML has a series of mechanisms that can be used to expand its core concepts, but perhaps the most well-known is stereotype. Stereotype is generally translated as "constructor", which is an extended element.
Modeling Elements of model semantics. The constructor must be based on a specific existing type or class in the meta-model. Constructor can expand the semantics of existing types and classes, but cannot modify their structures. The default representation of constructor is critical.
The word is enclosed by Angle brackets, which naturally exist in some European languages. Because it is like two angle brackets, it is also an accepted representation.

Constructor is applicable to almost any element in UML, including classes, attributes, operations, and associations. For example, a constructor can be used to display classes of a class in a UML diagram. Figure 1 shows the role played by a class in the state design pattern using the constructor.
Patterns. UML defines a large number of standard constructor types. We can either use these standard constructor types or define our own constructor types.

Figure 1: UML constructor is used to represent the role of a class in the design mode.

The messagestatus interface in Figure 1 should have placed the word "interface" in angle brackets. However, in order to distinguish interfaces from other constructors, together is used to create the UML diagram in this article.
The controlcenter tool has been omitted. This is because the interface is different from other constructor models. "In the UML meta-model, the interface also has similar characteristics to the class ".

Until uml1.4 (20001-September), one graphic element in UML can only have one constructor. But in UML
1.4, the OMG (Object Management Organization) removes this restriction. Now a graphic element can have multiple constructors. Many UML tools fail to keep up with this change, so they still do not provide support.

So what is the impact of constructor on our Java code? In a sense, the answer is "none at all", because JAVA does not provide any means for us to classify classes in this way.
(Interfaces and inheritance have been discussed in the previous articles. In UML, they all have their own specific representation methods ). However, on the other hand, we can use the constructor to better understand the Java code
Meaning: first, define the specific meaning of the constructor, and then include the constructor in the source code annotation in the form of a new javadoc mark, effectively reducing the need to manually write to illustrate the meaning of Java code
Description text quantity. The following code snippet is the skeleton code of the sent class in Figure 1. The constructor is added to the annotation in the form of a custom javadoc Tag:


     
      
/**
* @Stereotype concreteState
* @author AuthorName
* @version 0.0001
*/
public class Sent implements MessageStatus {
}

In UML, not only classes can be defined by specifying constructors. Figure 2 shows the dependency between two classes. The constructor is used to represent the type of the dependency. In this example
The factory class object is responsible for creating the object of the item class. The code of the factory class shows how the custom javadoc mark uses the constructor to describe the dependency
System.

Figure 2: add the UML dependency of the instantiate Constructor

Symbol Description: in the previous article, we saw the relationship between the three types. here we see the fourth type. The link is expressed by a solid line and an open arrow (if the link is unidirectional ).
The link is represented by a closed arrow (pointing from the subclass to the superclass) on the solid line, and the realization link is indicated by a closed arrow on the dotted line (pointing from the class that implements the interface to the interface. Now we see the fourth
Combination of Arrow and line type: the dependency between the dotted line and the open arrow.

     
      
public class Factory
{
/**
* @dependency <<instantiate>> Item
* @return a new Item
*/
public Item createItem() {
return new Item();
}
}

You can specify the constructor for operations and properties. As shown in figure 3, two operations are added with constructor to indicate whether they will modify the attribute value. The source code corresponding to Figure 3 uses the custom javadoc mark to describe the constructor information of this method.

Figure 3: Add a UML constructor to the class operation


     
      
public class Sale
{
...
/**
* @Stereotype query
* @return total price of sale
*/
public BigDecimal calcTotal() {
}
...
}

After the custom javadoc tag describing constructor information is added to the Java source code, the advantage is not only that the annotations that need to be compiled manually are reduced, in addition, the UML tool may process these tags and complete the following tasks:

Re-generate a more complete UML diagram from the Java source code (if the javadoc tag is not customized.

Add additional information to the documents generated by javadoc.

For example, together of togethersoft, the modeling tool used in this article
Controlcentre is used to retain the semantic information of UML class diagrams that cannot be directly stored in Java source code.

Other Representation Methods

As mentioned at the beginning of this article, angle brackets are the default method for displaying constructors. In fact, the constructor can also be represented by changing graphical symbols or shapes. The example in figure 4 shows two
<Actor>
Constructor class. Cashier exploitation <
> Constructor symbols are drawn, and the manager class is drawn using the default rectangle. When using replacement symbols, it is difficult to list various attributes and operations of the class, so they are usually omitted. Third Representation
Method, that is, put a small replacement symbol on the right side of the class name in the regular rectangle symbol, but this representation method is not quite seen now.


Figure 4: <actor> constructor replacement symbol

Using substitution symbols in class diagrams indicates that the constructor has a major disadvantage. If some people are not familiar with the symbols used in class diagrams, it is very difficult to understand what a class chart means. In addition, there is one more symbol, and the burden of understanding the image is increased by one point. In this series of articles, we only focus on the most common UML class diagrams.

In addition to changing the symbolic shape, we can also change the color or texture of graphic elements to express the same information. Using Color means we can retain the regular
Graphical shapes and symbols, while expressing the same visual information as changing the shape (if not more ). In addition, compared with abstract shapes, simple color schemes are generally easier to grasp.

Figure 5: Apply colors to class charts

The color class diagram in Figure 5 uses the four-color prototype (archetype) combination of Peter Coad and others to define the class.

Pink
<Moment-interval>
Class indicates the events or activities that must be tracked in a system due to business or legality reasons. Carsale and carrental are two pink classes.

Yellow
<Role>
Class indicates the way to participate in an event or activity. For example, carsalesperson and customer are both yellow classes.

Green classes can be further divided
<Party> (usually a person or organization), <place> (where an event or activity occurs), and <thing> (an object actually involving an event or activity)
.

The fourth prototype is the blue catalog-entry-like-description (abbreviated
<Description>
), Indicating the difference between a car in the real world and the descriptions in the exhibition catalog: The car model belongs to the blue category, it contains a series of values and value ranges to describe all vehicles belonging to this category, while every real car is composed of green
<Thing>
.

Classes belonging to a specific prototype have more or less similar attributes and behaviors. Classes belonging to the same prototype tend to interact with other prototype classes in a predictable way. These features and behavior patterns can help us
Quickly construct a robust and scalable object model, quickly master attributes and operations that may be ignored, and enhance our confidence in the code structure. Figure 5 shows the attributes and operations we may find in various prototype classes.
And typical relationships between various prototypes.

Conclusion: In this article, we understand some useful concepts in UML. If it does not have a direct equivalent concept in Java, how to use UML in Java code to retain high-level design ideas. In the next article, we will say goodbye to the UML class diagram, and then discuss another important UML graphic-the interaction diagram, including the sequence diagram and the collaborative drawing.

From: http://www.uml.org.cn/UMLApplication/UMLApplication30.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.