Relationships between classes in UML and examples of Java code

Source: Internet
Author: User
[From frenzied, a Java developer]
A link is a link between things. There are four important relationships between classes: dependency, generalization, association, and implementation. These relationships can be mapped to the code.
Dependency is a usage relationship. Changes that describe the specification of a thing may affect another thing that uses it (and vice versa ). In UML, it is represented as a dotted line pointing to the dependent thing. There are many constructor dependencies, but they are generally not used to avoid excessive complexity. In fact, the generalization, association, and implementation of the other three relationships can be regarded as some dependency, but they have strong semantics and important functions, so they are divided. Generally, when modeling a class relationship, we first model generalization, association, and implementation. The rest of the relationships can be seen as dependencies.
There may be many forms of dependency in the Code, such
Public Class
{
Public B getb (c, d)
{
E = new E ();
B = new B (c, d, e );
}
}
Class A depends on Class B (method return class), Class C and Class D (parameter class), and Class E (class of variable in method ), because changes to these classes may affect Class.

Generalization is the relationship between a general thing (called a superclass or parent class) and a special transaction (called a subclass). For a class, it is generally inherited, for example, public class A extends B. Generally, a solid line with a hollow arrow is used to represent the generalization relationship.

Association is a structural relationship, indicating that the object of one thing is associated with the object of another thing. Given a connection to two types of associations, you can navigate from one class object to another class object.
Generally, the check picture is a solid line connecting the same or different classes. The association can have a direction, that is, navigation. Generally, the navigation is bidirectional and no arrows need to be marked online. In most cases, navigation is unidirectional and can be indicated by an arrow.
Association is generally represented as an attribute in code, for example
Public Class
{
Private B;
}
It can be considered that there is an association between A and B.
If B is also associated with a, they are two-way associations.
Sometimes B is not an attribute of a, but it can also be associated. For example:
Public Class
{
Public B [] getbs ()
{...}
}
The getbs method may be used to query the database and find the association between A and B.

A simple association between two classes represents the structural relationship between two classes of the same status. Aggregation is also an association. Different from simple association, it describes the relationship between the whole and components, that is, the "has-a" relationship, this means that the overall object has part of the object, such as the relationship between the school and the student. There is no inevitable link between the whole and the part of aggregation in the life cycle. Some objects can be created before the whole object is created or destroyed after the whole object is destroyed. Aggregation is represented by solid lines with a hollow Diamond (one end of the whole.
Public class person
{...}

Public class school
{
Private arraylist students;
Private void addstudent (person)
{
Students. Add (person );
}
}
A combination is more correlated than aggregation. A combination is an aggregation association that has a strong relationship with each other and is consistent with the life cycle of some parts. For example, Windows and Windows menus are combined. Life Cycle consistency means that parts must be created at or after the combination, and destroyed before or after the combination is destroyed.

The life cycle of the combination. The combination is represented by solid line with solid diamond.
Public Class Menu
{...}
Public class window
{
Private menu;
}
The main difference between combination and aggregation in code implementation lies in the implementation of the life cycle. components need to be responsible for the creation and destruction of their parts.
Public class school
{
Public School ()
{...} // No person object needs to be created, and students are existing person objects.
Public void destroy ()
{...} // You only need to turn off the school object and disconnect it from all of its person objects. The person object will not be destroyed.
}

Public class window
{
Private menu;
Public window ()
{
Menu = new menu ();
} // You can create a menu object at this time or later.
Public void destory ()
{
Menu. destory ();
} // The associated menu object must be destroyed at the same time or before.
}

Another difference is that an object in a combination can belong to only one component object at a time, while a part of the aggregated object can be aggregated by multiple overall objects, for example, a student can attend multiple schools, and a menu can only be an object in a window at the same time.

The implementation relationship is relatively simple. It refers to the contract that one class element describes the implementation of another class meta guarantee. For a class, a class implements an interface.
Public interface
{
Public void methoda ();
}
Public Class B implements
{
Public void methoda ()
{...}
...
}
Generally, in an object-oriented system, try to use interfaces to reduce coupling between classes. Because the interface is not implemented, it does not depend on a specific implementation. For example, the connection and resultset in JDBC are interfaces, and the implementations of different database vendors can be different.

To sum up
1) Dependency: the link object appears in a local variable or method parameter, or the static method of The Link class is called.
2) Link: the link object appears in the instance variable.
3) Aggregation relationship: the relational object appears in the instance variable.
4) compositing relationship: the link object appears in the instance variable.
5) Generalization: extends
6) Implementation: Implements

 

Aggregation is like this: the relationship between the students and the selected courses is a combination. There is no inevitable relationship between the students and the selected courses. Deleting a course will not affect the students.
The combination is like this: the relationship between orders and order entries is aggregation. They are closely related. Deleting an order makes order entries meaningless.
Combination is a form of aggregation

Related Article

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.