-UML class diagrams are often used to describe the static structure between classes when describing the design mode. The purpose of this article is to allow the readers to understand the UML class diagrams and to figure out several relationships in the UML class diagrams.
-In the subsequent design model learning process, only these two points are adequate. More advanced is beyond the scope of this article.
Johnconnor design pattern note series directory
Johnconnor design patterns Note (I) What you must master before learning the design patterns-Understanding UML class diagrams
Johnconnor design patterns note (2) photocopiers in the procedural world-prototype mode and shortest replication/deep Replication
To be continued ......
The UML class diagram isUML (Unified Modeling Language)Five graphical methodsStatic GraphIt is used to describe the static structure of classes in the system. It not only defines classes in the system, but also represents links between classes, such as associations, dependencies, and aggregation, it also includes the internal structure of the class (class attributes and operations ). A class chart describes a static relationship that is valid throughout the lifecycle of the system.
If you have read some design patterns, you must be familiar with UML class diagrams. It can be said that learning class diagrams is the starting point of the Design Pattern journey. Let's look at an example below.
1. Class Representation
Presumably movie Terminator is deeply rooted in the hearts of the people, since my English name and the protagonist of the same name, then come to a terminator T-800 (Arnold muscle Singh.
Generally, a node in a UML class diagram is a class or interface. From this T-800 class we can see that,
- A class is composed of three layers of rectangles: the class name displayed on the top, the display feature in the middle (attributes or fields), and the operation (method or behavior) displayed at the bottom)
- Feature format: visibility modifier property name: Type <= default value>. The characteristics of T-800 are model, target and so on.
- The operation format is: visibility modifier operation name (<parameter list>): return type. The format of operation parameters is <in/out> Parameter Name: type. In/out to indicate whether the parameter is of the input or output type. The T-800 operation is restarting.
- In the UML class diagram, visibility '+' indicates public, '-' indicates private, and '#' indicates protected.
In addition to the rules of the UML class diagram visible in the preceding example, the following points are added:
- If this class is an abstract class, the class name must use italics.
- If we only want to display high-level details in a class diagram, the information of the following two regions is not necessary.
- Unless an early programming language is used, this in/out indicator is helpful and is generally omitted.
2. Interface Representation
There are two methods to express an interface:
- The rectangle representation is similar to the class representation, but you need to mark <interface> above the interface name, and the interface has only two parts, the interface name and method.
- The name of the lollipop is the name of the interface, and the method is written in the operation part of the class to implement it. The features and Operation Details of T-800 are omitted here.
3. Relationships in UML class diagrams
1.Generalization):Use a solid line with a hollow triangle.
Whether it's a basic T-800, a liquid metal T-1000, or the latest T-X, they all belong to the Terminator series robot:
Generalized relationships are equivalent to inheritance relationships in object-oriented systems. Class T-800, T-1000, T-X are inherited from abstract class (note class name is italic) Terminator class, it is called Terminator class is a generalization of the first three.
There are three requirements for generalized relationships in UML,
- Subclass must inherit all features and operations of the parent class. For example, all the above three subclasses inherit the model and target fields of the parent class.
- In addition to the information consistent with the parent class, the subclass should also contain additional information. For example, the new operation: T-800 can restart (re-set the target), T-1000 can liquefaction, T-X can control other machinery.
- You can use the parent class or subclass.
2. emlpementation: Use a dotted line with a hollow triangle.
In the above example, the T-800 realizes the attack interface, and the Implementation relationship is extended.
In UML, an interface is a set of operations.
3. Dependency: Use a dotted line with arrows..
Machine people rely on internal instructions for action. You can also restart the T-800 to execute other tasks through commands.
Dependency is the weakest horizontal relationship. If the changes of Class A cause changes of Class B, B depends on Class. There are three cases of code dependency:
- Class A is a local variable in Class B (a method.
- Class A is a parameter of a method in Class B.
- Class A sends messages to Class B, which affects Class B changes.
4. Association: uses a solid line representation (with arrows ).
From this point on, everyone may be confused. But it doesn't matter. The relationship will be further analyzed. If you are familiar with World of Warcraft, two good friends playing the 22 arena together are 1-to-1 associations.
If a is treated, the solid line can be pointed to B with an arrow. Because only the former needs to milk the latter, and the latter only needs to cut down the enemy (of course, this is a very low-end approach ).
Associations are often mutual. The two objects associated with each other have no mandatory constraints. They can be removed or associated at any time. There is no agreement on the life cycle issue.
The associated object can be associated with another object, so the association can be shared.
A number indicates the multiple before the two are associated.*"Indicates no limitation,"1"Indicates that there is only one,"0..."Indicates 0 or more,"0, 1"Indicates 0 or 1,"M... n"Indicates M ~ N,"M ...*"Indicates at least m.
5. Aggregation: it is expressed by solid lines with hollow edges.
An aggregate relationship indicates a weak whole-to-part ownership relationship. It is mainly reflected in the different lifecycles of the two, some of which can be separated from the whole existence.
The aggregated objects can be associated with other objects, so the aggregated objects can be shared.
For example, the arms of warcraft are under the control of the Lich King's ideas. Now, John is knocked down by the male and killed by the arms,
The Death knights of the Organization followed suit to the alliance or tribe, which does not mean that they will be over.
In fact, aggregation is also a special case of association. But more closely, you will share everything with your girlfriend, but you will not give a friend with a key pair of their own.
6. Composition: it is expressed by solid lines with solid edges.
A composite relationship represents a strong overall ownership of a part.
In the Terminator series, Skynet is a set of control procedures with its own awareness of the military defense system, controlling all the mechanical corps.
The Skynet system was destroyed, and the mechanical Legion became a pile of copper scrap iron. (I just ran my question to World of Warcraft, er ....)
The subject controls part of the creation, destruction, or transfer, and part of the lifecycle is controlled by the subject. This is the composite relationship. It is also a special case of association.
4. Relationships and differences
Generalization and implementation are two vertical relations, which have no objection.
Dependency, association, aggregation, and combination of the four horizontal relationships, from weak to strong, are difficult to differentiate.
The biggest difference between dependency (... use a...) and other three is that they only use and do not hold references to their dependent objects. Code is not a member of a class, but a parameter or local variable of its method.
Aggregate (... owns ...), combination (... is a part ...), are associated (... has ...).
However, Association tends to be the relationship with two independent and equal things, such as a good friend or a good friend. However, aggregation and combination tend to be the relationship between the whole and the part.
The difference between aggregation and combination lies in the overall grasp of some lifecycles.
Aggregation is like a computer failure. Hard disks and other things can continue to be used. The combination is like a diode radio is broken, so the whole process is scrapped (for average people... do not speak out that you will repair ...)
---------------------------------------------- Split line ---------------------------------------------------
End: add the comment in the class diagram like this:
I have recently studied the design model. If possible, a series will be launched later. The movements I write are generally aimed at beginners like me. Hope everyone will join us!
All of the above figures are drawn using MS Visio. If you are interested, you can also write an article about Using Visio to draw UML class diagrams ^_^.