chapter 2
UML - unified modeling language
Overview
This chapter gives a short overview of the Unified Modeling Language (UML), which is the modeling language for object-oriented societies. If you have never learned UML, this chapter will make you have a minimal understanding so you can read the graphic included in this book.
In this chapter,
l I will describe what is UML and why use it.
l I will discuss the UML map for this book:
--Class Diagram
- interaction
What is UML?
UML is a visualization language for creating a program model (means a graphic tag with a semantic). For program model, I refer to a graphical expression of the program, we can see the relationship between the objects existing in the code.
UML has several different diagrams - some for analysis, some for design, is used for implementation (or more accurately, used for spreading, ie, code release) (see Table 2-1 ). Depending on the purpose, each figure shows the relationship between different entities.
Table 2-1 UML diagram and its purpose When you ... use UML map ... In the analysis phase, use an example of examples (for example, users, and other systems), and the function points I need to implement. Active maps focus focus on the workflow of the problem domain (people and other actual works, the subject domain) of the program, the logic flow of the program. Note: Because this book is mainly designed, I will not here involve an example or activity diagram. Observe the object interactive mutual chart, which shows how specific objects are interacting. Since they deal with specific cases rather than in general, they can help when testing demand and inspection design. The most popular interaction is the sequence diagram. In the design phase class diagram, they detail the relationship between classes and classes. Observe the behavior of the object, these behaviors different state diagrams due to the status of the object, which details the possible transitions may be in different states and these states. At the deployment stage, they show that different modules will be deployed. I won't discuss them here.
Why use UML
UML is mainly used for communication - and my own, my team member, and my customers. In the field of software development, bad demand (or incomplete or inaccurate) is generally existed. UML gives us tools to collect better needs.
UML gives us a method to judge whether you have the same understanding of the system. Since the system is complex, there are different types of information that must be expressed, and UML provides different figures to specifically express different information.
A simple way to see UML value is to recall the last few design reviews. If you have reviewed it, people began to discuss and describe their code, but there is no model similar to UML, can be sure, their conversations are not only puzzling and very cost-effective. UML is not only a good way to describe object-oriented design, but also forced designers to think about him or her throughout the process (because it must be written).
Class Diagram
The most foundation in the UML map is a class diagram. It describes the relationship between classes and classes and classes. The type of relationship may be:
l When a class is another class of "species": IS-A relationship
l There is an association between two classes
- A class "contains" another class: HAS-A relationship
- Another class "use" another class
These topics have variables. For example, saying that something can contain other things.
l The included person is part of the included person (just like the engine in the car).
l I have a group of things that can exist independently (just like aircraft on the airport).
The first example is called a combination, and the second example is called polymerization [1]. Figure 2-1 describes a few important things. First, each rectangle represents a class. I can represent the three things of the class using UML:
L-class name
L-class data member
L class (function)
Figure 2-1 Class diagram - It three variants.
I have three different ways to show these things.
l The leftmost rectangle only shows the name of the class. I use this way to express classes when there is no more detailed information.
l The middle of the rectangle shows the names and methods of the class. Among them, Square [2] has a Display method. The plus ( ) before the display (method name) indicates that this method is public - this is to say that other classes can also call it.
l The rightmost Rectangle shows my previous things (class names and methods) and the data members of the class. Among them, the minus sign (-) in front of the data member Length (-) shows the value of this data member is private, that is, in addition to the object it belongs, there is no other thing to access it [3].
UML tag method for access
You can control the visibility of the membership data and methods of the class. You can use UML to mark your desired visibility for each member. The following is three visibility in most object-oriented languages:
l PUBLIC: Tagged with a plus number ( ).
This indicates that all objects can access this data or method.
l Protected: Tag with a well (#).
This indicates that only this class and all its derived classes (including its derived class derived class) can access this data or method.
l Private: Tagged with a minus (-).
This means that only this class can access this data or method. (Note: Some languages further limit the limitations on a specific object.)
Class maps can also show relationships between different classes. Figure 2-2 shows the relationship between the Shape class and several derivatives.
Figure 2-2 Types showing the relationship between IS-A
Figure 2-2 shows a few things. First, the arrow under the Shape class means that several classes that point to Shape are derived from Shape. Also, the slope of Shape means it is an abstract class that defines an interface for its derived class.
There are two different HAS-A relationships. An object can have another object, where the object being included is part of the object - or not. In Figure 2-3, I showed the "owned" airplane. The aircraft is not part of the airport, but I can still say that the airport has them. This type of relationship is referred to as aggregation.
Figure 2-3 Types showing the relationship between HAS-A
In this class diagram, I also showcase an aircraft or a jet either a helicopter. It can be seen that Aircraft is an abstract class because its name is italic. This indicates that a airport will have a jet or helicopter, but they will treat them as the same (aircraft). Open (hollow) rhombus on the right side of the AirPort class demonstrates the polymerization relationship.
Another HAS-A relationship contains an object being included in an article that is part of the object. This type of relationship is also known as a combination.
Figure 2-4 Class diagram showing the combination and use
Figure 2-4 Indicates that the wheel is part of a car (that is, the car consists of wheels and other things). This relationship called a combined HAS-A is expressed in solid rhizome. This picture also shows that a car uses a gas station. This relationship is represented by a dotted line with an arrow, also known as a dependency.
Whether combined or aggregation, they all contain an object that contains one or more other objects. However, combining suggests that the object being included is part of the object, and the polymerization means that the object is more like a group of things. We can think of a combination is a relationship that is not shared, where the life of the object is controlled by its included object. Here, properly use constructor and destructive methods make the object's creation and destruction easier. Figure 2-5 Class diagram using a comment
Comments in UML.
A new symbol appears in Figure 2-5: Comments. Among them, the box that contains "open rhombus means aggregating" messages is a comment. They look like a sheet of paper folded backwards behind the right corner. You often see a straight line connecting them to a specific class, indicating that they are associated with that class.
The class diagram shows the relationship between classes and classes. In combination and aggregation, this relationship will be more clear in terms of the object of the type of type. For example, the airport does have a plane, but it is more clear that a specific airport will have a specific aircraft. Now the problem is coming - "How many aircraft will have a plane?" This is called the base of the relationship. I am showing this in Figure 2-6 and 2-7.
Figure 2-6 Base of the aircraft - aircraft relationship
Figure 2-6 Tell us that when I have a plane, it has 0 to any number (here, in an asterisk, but sometimes uses the aircraft "N" indicated). "0..1" on the side of AirPort indicates that when I have a plane, it can be included in 0 or 1 airplane field (it may be in the air).
Figure 2-7 Base of automobile - tire relationship
Figure 2-7 Tell us that when I have a car, it has 4 or 5 tires (which may have or have no spare tires). The tires are accurately in a car. I have heard that some people assume that there is an object when there is no specified base. this is not right. If there is no specified base, this indicates that there is no hypothetical assumption.
As before, the dashed line between Car and Gasstation in Figure 2-7 indicates that there is dependence. UML uses a broken line with an arrow indicates the syntax relationship (meaning) between the two model elements.
Interact
The class diagram shows the static relationship between the classes. In other words, they do not show us any behavior. This is useful, but sometimes I need to show the object from these instantiated objects to work together.
The display object is a UML figure that interacts with each other is called interactive. The most common interaction is the sequential diagram, as shown in Figure 2-8.
We read the sequence diagram from top to bottom.
l Each rectangle on the top represents a specific object. Although there are many rectangular names, pay attention to the previous colon in front of the class name. Some rectangles have other names - such as Shap1: Square.
l The top of the top gives the name of the class (right side of the colon) and an optional object name (front of the colon).
l Vertical line indicates the life of the object. Unfortunately, most UML drawing programs do not support this, they will draw straight from top, which makes an object actually become unclear.
l I showcase the horizontal line between these vertical lines to send a message to each other.
l Sometimes return values and / or objects are explicitly displayed, and sometimes this is very implicit.
Figure 2-8 Sequence diagram of shape programs
For example, in Figure 2-8,
l At the top, I saw that Main sent a "get the shape" message to a shapedb object (not named). After being requested "Get Shape", Shapedb will:
- Instantiate a collection
- Instantiate a square
- Add a square to the collection
- Instantiate a circle
- Add a circle into the collection V
- Return the collection to the call (main)
I read the remainder of the figure from top to bottom, understand the remaining action. This picture is called a sequence diagram because it shows the order of the operation. Object: class tag
In some UML diagrams, you want to reference an object and indicate which one of it is derived. This can be achieved by connecting them with a colon. In Figure 2-8, Shape1: Square refers to the Shape1 object that is instantiated from the Square class.
to sum up
The purpose of UML is to enrich your design and communicate them. Don't worry too much whether you create graphics in the "correct" way, you only need to consider the concept of communicating the design in the best way. in other words,
l If you think something needs to be said, then use a comment to express it.
l If you don't know an icon or a symbol, and have to find it to get its meaning, that is, use a note to explain. Because others may not know its meaning.
l Depending on clarification.
Of course, this means that you should not use UML in an inexpensive way to communicate properly. When drawing, just consider what you want to communicate.
[1] Gamma, Helm, Johnson and Vlissides (four people) are called "polymerization", the second "combination" - just contrary to UML. In fact, the writing of the four-person group is before the UML is finally determined, and their current definitions and UML are consistent. The external power of the UML is given to the UML. There are several different modeling languages in front of UML, each has its own marking method and terminology.
[2] As with this, we use bold anywhere to reference a class name.
[3] In some languages, the same type of object can share their private data.