6 major relationships of UML class diagrams

Source: Internet
Author: User

The drink remodeling series has been completed by about 1/3, and in these articles I used a few simple class diagrams to describe refactoring strategies. In a later article, I might use a slightly more complex UML class diagram to introduce. But before I do, I think it's necessary to introduce the 6 major relationships in UML class diagrams first. The 6 major relationships are inheritance (inheritance), implementation (Implementation), Dependency (dependency), Association (Association), Aggretation (aggregation), and composition (combination). In these 6 relationships, dependencies, associations, aggregations, and combinations are more confusing, and I'll explain the differences between them.

Introduction to UML Class diagrams

UML is the abbreviation of Unified Modeling language, which translates into Chinese as "Unified Modeling Language", which is a standardized modeling language for object-oriented software. It is a graphical language that supports modelling and software system development, providing modeling and visualization support for all phases of software development, including requirements analysis to specification, to construction and configuration.

Different organizations of different software on the classification of UML is different, basic all the UML software contains the following 4 major categories

1. Use case diagram
2. Class Diagram
3. Activity diagram
4. Sequence diagram UML class diagram

UML class diagrams are used to represent a set of objects, interfaces, collaborations, and relationships between them. A class diagram describes a static relationship that is valid throughout the life of the system and is the most common diagram in the modeling of object-oriented systems.

UML Tools

There are many tools and software on the market, everyone tastes different, I do not introduce more, my most common UML tool is Visio.


Of course as a. NET Developer, the Ultimate version of Visual Studio also provides UML modeling capabilities.

Inheritance and implementation inheritance (inheritance)

Inheritance represents a class (interface) that inherits the functionality and properties of another class (interface) that describes the relationship between the parent class (interface) and the Child Class (interface).
Inheritance describes the "is a kind of" relationship, for example: Manger is a type of employee, and the manager inherits all of the employee's functions (for example: swipe in, perform work) and attributes (ex: Employee name, entry time).

In UML, inheritance uses a solid hollow arrow to indicate that the hollow arrow points to the parent class (interface).

Implementation (Implementation)

Implementation indicates that the class implements the functionality of the interface.

In UML, inheritance uses a dashed hollow arrow to indicate that the hollow arrow points to the interface.

The difference between inheritance and implementation

Although both inheritance and implementation in C # use symbols: to denote (using extends in Java to represent inheritance, implements represents implementation), there are some differences.

1. Inheritance occurs between classes and classes or interfaces and interfaces, for example: subclasses inherit the parent class, and the child interfaces inherit the parent interface.

Subclass Inherits Parent Class:

Public abstract class animal{        }public class bird:animal{        }

The child interface inherits the parent interface:

public interface itransportation{    void Move (); public interface ivehicle:itransportation{        }

2. Implementations occur between classes and interfaces, for example: A class implements a method of an interface.

Public interface Ivehicle:itransportation{}public class car:ivehicle{public    void Move ()    {        Console.WriteLine ("Car Running up ...");}    }

3. In C #, multiple inheritance is exactly a multi-implementation.

Unlike the syntax of the C + + language, C # classes cannot inherit multiple classes at the same time. C # can inherit only one class, but it can implement multiple interfaces at the same time.
For example: The Controller class in ASP. Inherits the Controllerbase class and implements the Iactionfilter, Iauthenticationfilter ... and other interfaces.

Public abstract class Controller:controllerbase, Iactionfilter, Iauthenticationfilter, Iauthorizationfilter,    IDisposable, Iexceptionfilter, Iresultfilter, Iasynccontroller, iasyncmanagercontainer{}
Dependency, association, aggregation, and composite dependency (Dependency) dependencies are the most basic relationships between objects.
Dependencies are established when an object (the caller) needs to invoke the method of another object (callee) to do some work.

In UML, a dependency relationship uses a dashed arrow to indicate that the arrow is pointing to the relying party.

For example, in a Web service, the client needs to invoke service operations, which means that the client relies on the service.

An Association (association) Association represents an association between multiple objects.
Each object has its own life cycle, and there is no dependency between the objects.

In UML, association relationships are represented by a straight line.

For example: Student and teacher belong to "association", multiple student can be associated to a teacher, and a student can be associated to multiple teacher.
However, there is no "subordinate" or "containment" relationship between teacher and student.

Aggregation (Aggregation) Aggregation embodies the relationship between the whole and the part, that is, the "has-a" relationship.
The whole and the part can be separated, that is, the whole and the parts can have their own life cycle.

In UML, an aggregation relationship is represented by a hollow diamond-shaped arrow, which points to the whole.

For example: A department has multiple employee,department as a whole, and the employee in department is part of the department.
Department and employee have their own life cycle, and when a department is revoked, the employee can go to another department or leave the office.
Department is still present when the employee transfers to another department or leaves the office.

The combination (composition) composition embodies the relationship between the whole and the part, that is, the "is-a" relationship.
Combinatorial relationships are more powerful aggregation relationships, and the whole and part are inseparable.
The end of the whole life cycle also means that part of the life cycle ends.

In UML, a composite relationship is represented by a solid Diamond arrow, which points to the whole.

For example, a house has multiple rooms, and the room is part of the house. The life cycle of a room depends on the life cycle of the house, and when the house is torn down, the room does not exist.

Differences in dependencies, associations, aggregations, and combinations

dependencies, associations, aggregations, and combinations can all be generically used as "dependencies."
Dependencies between objects are also established when objects Form Association, aggregation, or composition relationships.
They show a different degree of dependence, and these 4 relationships are performed in the following ways: Composite > Aggregation > Association > Dependency.

Association, aggregation, and composition are 3 relationships that are often confusing, and the biggest difference is the life cycle of the object.

1. Affinity Relationship: Each object has its own life cycle, and there is no dependency between objects
2. Aggregation relationship: The whole and part are inseparable, the whole and the part can have their own life cycle
3. Combinatorial relationships: The whole and the part are inseparable, and the whole life cycle ends, which also means that part of the life cycle ends.

6 major relationships of UML class diagrams

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.