Knowledge review-corresponding code of UML relationships

Source: Internet
Author: User

For more information about the several types of UML relationships, see "links in UML".

To really draw a UML diagram, thoroughly understand the specific meaning of each link, you need to understand the code implementation of each link.

I. Dependency
Dependency is the least coupled relationship among the five relationships of the class.

The dependency can be described as: uses
When the code is generated, no attribute is added to the two link classes.

Public class FilmClip{     public FilmClip() {}}

Public class Channel{     public Channel(){}}

Dependency representation 1: If the channel class is global, the filmclip class can call it.

For example, in VB, any class that uses this module is called dependent on this module.

Form 2: The channel class is a variable in a method of the filmclip class, And the filmclip class can call it.

Representation 3: The channel class is used as a parameter or return value of a method in the filmclip class.

For example:

Public FilmClip{    Public Channel Grownup(Channel channel) {               return null;       }}

Ii. Generalization
The generalized relationship can be described as: Is A. The subclass inherits all attributes and methods of the parent class.








Code:

class Shape{}public class Circle : Shape{}public class Polygon : Shape{}

Iii. Association

(1) bidirectional Association






(2) unidirectional Association






C3-> C4: indicates the acquaintance relationship. C3 knows C4 and C3 can call the common attributes and methods of C4. There is no life cycle dependency. It is generally a reference.

(3) Self-Association










Code:

1. One-way association code:













Public class Water {     public Climate m_Climate;     public Water(){}}

Public class Climate {     public Climate() {}}

The climate class is added to the property of the water class in the generated code.

2. Two-phase association code example:


Public class Water {     public Climate m_Climate;     public Water(){}}

Public class Climate {   public Water m_Water;     public Climate() {}}

In the generated code, the attributes of both classes are added!
3. Aggregation












Aggregation: the geese are aggregated by the geese. The geese are aggregated, but the geese can leave the geese and exist independently.

Public class GooseGroup {    public Goose goose;    Public GooseGroup(Goose goose) {               this.goose = goose;       }}

4. Combination












Public class Goose {   public Wings wings;   public Goose() {       wings = new Wings();    }}

Differences between aggregate combinations:
1) different constructors:
The aggregate class constructor contains another class as a parameter.
In the construction function of goosegroup, goose is used as a parameter. Goose can exist independently from the geese.
The constructor of the composite class contains the instantiation of another class.
It indicates that before instantiation, the geese must first instantiate the wings class (WINGS). These two classes are closely coupled and co-occurrence. Wings cannot exist independently from Goose.
2) Different information Encapsulation
In the aggregation relationship, the client can understand both the geese and geese at the same time, because they are independent.
In the composite relationship, the client only recognizes the geese and does not even know the existence of the wings, because the wings are tightly encapsulated in the geese.


IV. Implementation







Implementation refers to the function of a class to implement interface interfaces (which can be multiple.

Implementation is one of the most common relationships between classes and interfaces.

interface interfaceA{}class classA :interfaceA{}

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.