relationships between classes in UML diagrams: dependencies, generalizations, associations, aggregations, combinations, implementations
turn from: http://blog.csdn.net/hguisu/article/details/7609483
classes and class diagrams1 Class (Class) encapsulates data and behavior, which is an important part of object-oriented, and it is the collective name of objects with the same attributes, operations, and relationships. 2 in the system, each class has a certain responsibility, the responsibility refers to the tasks of the class, that is, what kind of function to complete, what kind of obligations to bear. A class can have multiple responsibilities, and a well designed class typically has only one responsibility, and when defining a class, it breaks down the responsibility of the class into the properties and operations of the class (i.e., the method).
3 class's attribute is the class's data duty, the class's operation is the class's behavior duty
I. Dependencies (dependence)
Dependencies (dependence): Assuming that Class A changes cause a change in class B, the name B class relies on Class A.
• Dependency (Dependency) is a usage relationship in which the change of a particular thing may affect other things that use it, and use dependencies when it is necessary to represent one thing using another. In most cases, dependencies are embodied in the method of a class using an object of another class as a parameter. • In UML, a dependency is represented by a dotted line with an arrowhead, and the dependent party points to the dependent party.
public class Driver
{public
void drive (car car)
{
car.move ();
}
...
}
public class car
{public
void Move ()
{
...
}
...
}
Dependencies have the following three scenarios:
1. Class A is a local variable in class B (a method);
2. Class A is a parameter of class B method;
3. Class A sends messages to class B, thus affecting class B changes;
1. Dependency is also the connection between class and class
2, dependence is always one-way.
3. Dependencies are embodied in a Java or C + + language as a local variable, as a parameter to a method, or as a call to a static method.
Publicclassperson
{
Publicvoidbuy (Carcar)
{
...
}
}
Ii. generalization relations (generalization)
generalization Relationship (generalization): A is the parent class of B and C, B,c has a public class (parent) A, stating that A is a generalization (generalization, also called generalization) of B,c.
• Generalization relations (generalization), also known as "is-a-kind-of" relationships, are used to describe the relationship between a parent class and a subclass, and a parent class is also called a base class or superclass, and a subclass is also called a derived class. In UML, a generalization relationship is represented by a straight line with a hollow triangle. • Use object-oriented inheritance mechanisms to implement generalization relationships when code is implemented, such as using the extends keyword in the Java language and using the colon ":" in c++/c#.
public class person
{
protected String name;
protected int age;
public void Move ()
{
...
}
public void Say ()
{
...
}
} public class Student extends person
{
private String studentno;
public void Study ()
{
...
}}
In UML, there are three requirements for generalization relationships:
1, the subclass and the parent class should be exactly the same, the parent class has attributes, operations, subclasses should have;
2. In addition to the same information as the parent class, additional information is included in the subclass.
3, can use the instance of the parent class place, also can use subclass of instance;
III. Association Relations (Association)
Association Relationship (Association): The relationships between classes, such as customers and orders, each order corresponds to a specific customer, each customer corresponds to some specific order, and then the link between the basketball player and the team (shown below).
Among them, "employee" and "employer" on both sides of the association indicate the relationship between the two, and the number means the restriction of the relationship between the two, which is the multiplicity of the correlation. There is usually a "*" (means all, no limit), "1" (indicating there is and only one), "0 ..." (representing 0 or more), "0,1" (for 0 or one), "N...M" (for N to M), "m...*" (for at least m).
• Association Relationship (association) is the most commonly used relationship between classes and classes, which is a structured relationship between a class of objects and another class of objects. • In UML class diagrams, objects that are associated with a solid line are connected by a class that, when implemented in a programming language such as Java, C #, and C + +, typically uses the object of one class as the property of another class. • The role name can be annotated on the association line when a class diagram is used to represent an association relationship.
1 Bidirectional Association: By default, the association is bidirectional.
public class Customer
{
private product[] products;
...
}
public class Product
{
private customer customer;
...
}
2 One-way Association: The Association of the class can also be one-way, one-way association with a solid line with an arrow to express.
public class
, Customer {
private address;
...
}
public class address
{
...
}
3) Self-association:There may be some classes in the system where the Property object type is the class itself, and this particular association relationship is called a autocorrelation.
public class node
{
private node subnode;
...
}
4)
Re-count association:A multiplicity association is also called a multiple association relationship (multiplicity), which represents the number of objects of one class that are connected to another class. In UML, multiple relationships can add a number to the associated line directly to represent the number of objects of another class corresponding to it.
Presentation method |
Multiplicity of descriptions |
1..1 |
An object representing another class is related to only one object of that class |
0..* |
An object representing another class has a relationship with 0 or more of the class object |
1..* |
An object representing another class is associated with one or more of the class objects |
0..1 |
An object representing another class has no or only relationship to one of the class objects |
M.. N |
An object representing another class has a relationship with the least m, up to n objects of that class (M<=n) |
public class Form
{
private Button buttons[];
...
}
public class Button
{
...
}
Iv. Aggregation relations (Aggregation)
aggregation Relationship (Aggregation): the whole and part of the relationship, the whole and part can be separated.
• The aggregation relationship (Aggregation) represents a relationship between a whole and a part. Usually after defining a whole class, we analyze the structure of the whole class to find out some member classes, which form the aggregation relationship between the whole class and the member class. • In an aggregation relationship, a member class is part of a whole class, that is, a member object is part of a whole object, but a member object can exist independently of the whole object. In UML, an aggregation relationship is represented by a straight line with a hollow diamond.
public class car
{
private Engine Engine;
Public car (Engine Engine)
{
this.engine = Engine;
}
public void Setengine (Engine Engine)
{
this.engine = Engine;
}
...
}
public class Engine
{
...
}
such as: telephone including a microphone
Computers, including keyboards, monitors, a computer can be with multiple keyboards, multiple monitors with, to determine the keyboard and monitor can be separated from the host, the host can choose other keyboards, monitors composed of computers;
1, the aggregation relation is one kind of correlation relation, is the strong correlation relation.
2. Aggregation is the relationship between the whole and the part, for example, the automobile is made up of engine, tyre and other parts.
3, the aggregation relationship is also achieved through member variables. However, the two classes involved in the association relationship are at the same level, and in the aggregate relationship, two classes are at different levels, one represents the whole and one represents the part.
4, association and aggregation can only be distinguished from Java or C + + syntax, and must examine the logical relationship between the classes involved.
V. Combinatorial relations (composition)
Combinatorial Relations (composition): The relationship between the whole and the part, but the whole and the part can not be separated.
• Combinatorial relationships (composition) also represent the overall and partial relationships between classes, but there is a unified lifetime of the parts and the whole in a composite relationship. Once the whole object does not exist, some objects will not exist, and part of the object and the whole object have the relationship between the die and die. • In a composite relationship, the member class is part of the overall class, and the overall class can control the life cycle of the member class, that is, the existence of the member class depends on the overall class. In UML, a composite relationship is represented by a straight line with a solid diamond.
public class head
{
private mouth mouth;
Public Head ()
{
mouth = new Mouth ();
}
...
}
public class mouth
{
...
}
Such as: Companies and departments, the department is part of the company is a whole, the company A's finance department can not and Company B of the financial department of the swap, that is, the company A can not be separated from their own financial department; the heart of Man and man.
1, the synthesis relation is one kind of correlation relation, is the relation which is stronger than the aggregation relation.
2, it requires that the ordinary aggregation of the object representing the whole is responsible for representing part of the object's life cycle.
Vi. Implementation of relations (Implementation)
implementation Relationship (Implementation): a class or build structure that is used to specify interfaces and solid interfaces, and interfaces are collections of operations that are used to specify a class or a service that is built.
• Interfaces can also have inheritance relationships and dependencies similar to those of classes, but there is an implementation relationship (realization) between interfaces and classes in which the class implements the interface, and the operations in the class implement the actions declared in the interface. In UML, the implementation relationship between a class and an interface is represented by a dashed line with a hollow triangle.
Public interface Vehicle
{public
void Move ();
}
Public class ship implements Vehicle
{public
void Move ()
{
...
}}
}
public class Car implements Vehicle
{public
void Move ()
{
...
}}
}
staruml Usage Instructions-instruction manual
Turn from: http://blog.csdn.net/monkey_d_meng/article/details/5995610
. Summary
STARUML is a tool for generating class diagrams and other types of Unified Modeling Language (UML) diagrams. This is a concise manual describing the creation of class diagrams in the Java language.
STARUML (SU) is a tool that creates UML class diagrams and can automatically generate Java "Stub code". Su can also do Java reverse engineering to produce the corresponding UML diagrams.
In this tutorial, we will use SU to design a pizza pie. Perform the following steps to create a UML diagram as shown below. Su can generate code that reflects the class structure, not concrete actions for any object. So, after using Su to create the chart, you add the remaining functional code to the stub code and fill in what each method should have done.
2. Installation
First, we must first install the software that will be used. STARUML is an open-source software that follows the GPL license (GNU Public License) and provides free downloads.
3. Start
You can start the program after installation.
4. Add New Project
Then a dialog box named: New Project by approach pops up. Select "Empty Project" and press "OK". It is recommended that you do not select the Set as Default check box.
5. Select Module
Select the untitled module in the Model Explorer box on the right.
6. Add Modules
Through the "model" main menu, or right click on the selected model, you can "Add/design model"
7. Add Class Diagram
You can "Add diagram/class Diagram" by using the "model" main menu, or by right-clicking the selected model:
8. Set profile
Use the "Model/profile" menu to set the profile required for the project. This determines the rules and conventions used by the project. Be sure to include the "JAVA Porfile" project.
9. Preservation Works
Save the project immediately so that you don't lose information when there is a problem.
From the "File" menu, select "Save" and select a place to save the project. Your STARUML project should now look like this:
10. Create charts
Now, start to really create the chart, from the default on the left side of the screen "Toolbox" Select the "Class" icon, and then left-click somewhere in the Diagram window. This creates a new class using the generic name. Double-click to rename the class to circle.
11. Add Properties
Right-click the target in the diagram, select "attribute" in "Add" in the pop-up menu (labeled Green), add an attribute (or field) to it, and fill in the desired name "_radius".
L Specific data types, in the property panel (bottom right side of the window), by the doubles word, in the "type" period. In the properties panel at the bottom right of the form, locate the Type input box, and enter double as the _radius property.
The internal data (fields/attributes) of the L class are private because they are strictly used internally by the class. So, in the Properties panel, set _radius to private.
12. Continue the design
Repeat the same process, add the so-called rectangle class and the private members of the double type _width and _height. (The following paragraph is the use of the idea of the matter, the total feeling that the translation department is too good, nine original moved up) The May notice using the ' Model Explorer ' on the ' right being faster to add ', but do however the adding and interfaces themselves in this toolbox (instead of using the "toolbox on the" and "clicking on" the Palette to create The object) won't create the objects in the diagram.
13. Create IShape Interface
L from Toolbox, select "Interface" and click somewhere on the chart. Change the name to IShape. After you create it, select it.
L at the top toolbar, select the "Stereotype Display" drop-down button to change the value to "None". This will change the shape of the previous circle, making it rectangular.
Or at the top of the toolbar, uncheck Suppress Operations. This will enable us to see the methods that the interface has.
L Add a Getarea method to the IShape interface with a return value of double.
n You can click the Red "Operation" button in the pop-up menu by right-clicking the interface icon, and then enter Getarea.
n Sets the return value type. Expand the IShape node in Model Explorer, right-click the Getarea method you just created, and select Add Parameter. In the Properties box, change the name of the argument to empty, change "directionkind" to "return", and change "Type" to Dboule.
L TICK the IsAbstract property box on the IShape and Getarea and their name on the icon will become italic. This is the standard of UML, which means that it is an interface or other pure virtual entity.
14. Add a relationship between a class and an interface
L The circle implements the interface IShape by selecting the arrow from the Toolbox that represents "realization" and dragging it from the circle to the IShape. Repeat the same process, adding implementation relationships for rectangle. This is the implementation of the circle and rectangle for the IShape interface.
n If you want the connector to behave as a right angle, right-click the connector and select the Format/line style/rectilinear menu. You can make your diagram look neater in this way by making the arrows overlap.
15. Adding a class based on the behavior of the interface
L because the circle and rectangle classes implement the IShape interface, they must have the same behavior (method).
N in the Model Explorer panel, copy the Getarea method (press Ctrl-c or right-click and select the Copy menu) and paste it into the circle and rectangle classes.
N These implemented methods are not abstract in the circle and rectangle classes, but are specific. This is because they are actually performing certain behaviors (for example, calculating the area for a circle and rectangle), so do not check the IsAbstract box.
16. Your diagram should now be like this:
17. Add Pizza Class
L add a double private domain _price to pizza.
L Add a common operation GetPrice that returns a double type.
18. Add a IShape reference to the pizza class
L Select the "directedassociation" Arrow from Toolbox, click Pizza Class, and drag to IShape.
n Select the arrow, on the right side of the "Properties" box, change the Name column to "Has-a" and the "end1.aggregation" column to "AGGREGATE" (this illustration shows that pizza and Shape objects are "aggregation" relationships).
(n) Change the "end2.name" column to _shape. This automatically adds a pizza name to the _shape, using the IShape interface of the private domain, the so-called _shape type IShape to pizza pie.
N Change "end2.visibility" to private.
L Create a "winner" method for _shape, named Getshape, to return to IShape. This is the creation of an action, the name is Getshape, returned to IShape.
19. Adding constructors to the Pizza class
L Add a constructor for pizza, right-click, and select "Operation" from the "Add" menu that pops up. From here, add an ordinary operation with the Dboule type price parameter and the IShape type shape parameter
L Add an input parameter, just like an output parameter that adds a return type before, the name of the parameter you specify, such as price and shape, and the appropriate data type.
L Add a constructor with a double radius parameter for circle.
L Add a constructor with the double width and height parameter for rectangle.
20. Your diagram should now be like this:
&