UML class diagram Five relationship and code correspondence

Source: Internet
Author: User

Turn:

Coupling strength comparison of five relationships in UML class Diagrams: dependency < Association < aggregation < combination < inheritance

I. Dependency relationship:

(i) Description

Dashed + arrow

Can be described as:Uses a

Dependency is one of the least coupled relationships among the five types of relationships in a class.

Because both of these relationship classes do not add properties when generating code.

(ii) Relationship between dependency graphs and code

(PS: Dependency:Animal dependent on water(animal dependent on water))

[CSharp]View Plaincopyprint?
    1. Public class Animal ()
    2. {
    3. Public Animal () {}
    4. }
    5. Public class water ()
    6. {
    7. Public Water () {}
    8. }

You can see nothing added in the code for the generated two classes .

(c) Consider:

How does the Animal class use the water class? Or how does the dependency relationship actually manifest itself?

1. Form of expression 1

The water class is global, and the Animal class can call it

2. Form of expression 2

The water class is a variable in a method of the Animal class, and the Animal class can invoke it.

[CSharp]View Plaincopyprint?
    1. Public class Animal {
    2. Public void grownup () {
    3. Water water =null;
    4. }
    5. }

Note 1: The lifetime of the water class, which is instantiated when the Grounup method of the Animal class is called .

Note 2: holding the water class is a method of Animal rather than a Animal class , this is the most important!

3. Form of expression 3

the water class is either a parameter or a return value for a method in the Animal class

[CSharp]View Plaincopyprint?
    1. Public Animal {
    2. Public water grownup (waterwater) {
    3. return null;
    4. }
    5. }


Note: The Water class is held by a method of the Animal class. The life cycle ends with the execution of the method .

Ii. related relations

(i) Description

Solid line + arrow

Can be described as: hasa

A solid line for association relationships, which indicates that the coupling between classes is stronger than

When generating code, the class of the association relationship increases the property.

(ii) Correspondence between the relationship and the code

PS: The water class is associated with the Climate class (water and climate).

[CSharp]View Plaincopyprint?
    1. Public Classwater {
    2. Public Climate M_climate;
    3. Public Water () {}
    4. }
    5. Public class Climate {
    6. Public Climate () {}
    7. }

In the generated code, the Climate class is added to the properties of the water class .

(iii) Types of related relationships

Associations have both one-way and two-way associations.

1, one-way association: Water class and Climate class One-way association (such as), the water class is called the source class, theClimate class is called the target class. The source class understands all the properties and methods of the target class, but the target class does not understand the source class information.

2. Bidirectional Association: The source class and the target class know each other's information. If you change between the water class and the Climate class to a bidirectional association.

[CSharp]View Plaincopyprint?
    1. public class water {   
    2.     public climate m_climate;   
    3.     public water () {}   
    4. }  
    5. public class climate  {  
    6.     public water m_ water;  
    7.     public  Climate ()  {}  
    8. }  


In the generated code, the properties of the two classes are added!

(iv) Thinking:

What is the difference between dependency and association?

1, from the point of view of whether the property of the class increases

(1) The two classes that have dependencies do not increment the properties. One of these classes acts as either a parameter or a return value for a method of another class, or a variable of a method.

(2) The two classes in which the relationship occurs, one of which becomes the property of another class, and the property is a more tightly coupled, longer-lasting relationship.

2.from the perspective of the life-cycle of the relationship:

(1) A dependency is generated only when the method of the class is called and ends with the end of the method.

(2) An association relationship is generated when the class is instantiated, and when the class is destroyed, the relationship ends. Correlation relationships have a longer lifetime than dependency theory.

(v) Refinement of association relationships: aggregation, composition

1. Description

(1) aggregation relationship, denoted by a hollow diamond plus an arrow

(2) The combination of the relationship, with a solid diamond plus arrows, the coupling between the class is stronger than the aggregation!

2, aggregation and combination are a kind of correlation relationship, in the end how to distinguish between them?

(1) aggregation and composition of generated code

(PS: This figure shows that the Wild Goose group is a kind of aggregation of wild Geese)

[CSharp]View Plaincopyprint?
    1. PUBLIC&NBSP;CLASSGOOSEGROUP&NBSP;{&NBSP;&NBSP;
    2.      public goose goose;  
    3.      public goosegroup (Goose goose)  {  
    4.                this.goose = goose ;   
    5. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
    6. }   

(PS: This figure shows that the wild goose is a combination of wings and the like)

[CSharp]View Plaincopyprint?
    1. Public Classgoose {
    2. Public Wings Wings;
    3. Public Goose () {
    4. Wings = New Wings ();
    5. }
    6. }

(2) different constructor functions

The constructor for the aggregation class contains another class as a parameter. The Wild Goose (Goose) is used as a parameter in the construction function oftheWild Goose Group (goosegroup). The Wild Goose (Goose) can be separated from the Wild Goose group and exist independently.

The constructor of a composite class contains the instantiation of another class . indicates that the wild goose class before instantiation, must first instantiate the wing class (Wings), these two classes tightly coupling together, the same life altogether to destroy. Wings (Wings) can not be separated from the Wild Goose (Goose) and independent existence.

(3) The encapsulation of information is different.

In the aggregation relationship, the client can know both the wild Goose and the wild goose, because they are all independent.

In the combinatorial relationship, the client only knows the Wild goose class, does not know the existence of the wing class, because the wing class is tightly encapsulated in the Wild Goose class.

Third, generalization

(i) Description

Solid line + arrow

Can be described as: isa

Generalization is also called inheritance, and subclasses inherit all the properties and methods of the parent class, and can extend the parent class as needed.

(ii) The correspondence between the generalization relation and the code

(PS:Bird class inherits Animal class, Bird is an animal)

[CSharp]View Plaincopyprint?
    1. Class bird:animal{
    2. }

(c) Consider:

1. Doesthe subclass inherit the parent class, and does it really inherit all the properties and methods of the parent class?

subclasses do inherit all the properties and methods of the parent class, except for the private type members of the parent class! Access will be an error!

2.is generalization and inheritance a thing?

subclasses inherit the parent class, and the parent class generalizes subclasses. These two words are from a different angle!  

3, why to use more combination less inheritance?

Inheritance and composition each have advantages and disadvantages.

class inheritance is statically defined at compile time and can be used directly, and class inheritance can easily change the implementation of the parent class. But class inheritance also has some shortcomings. First, because inheritance is defined at compile time, it is not possible to change the implementation inherited from the parent class at run time. To make things worse, the parent class usually defines at least part of the behavior of the subclass, and any changes to the parent class can affect the behavior of the child class. If the inherited implementation is not suitable for solving the new problem, the parent class must be overridden or replaced by another more appropriate class. This dependency limits flexibility and ultimately limits reusability.

object composition is dynamically defined at run time by obtaining a reference to another object. Because the combination requires the object to have a well-defined interface, and the object can only be accessed through the interface, we do not break the encapsulation, as long as the type is consistent, the runtime can also use one object to replace another object, further, because the implementation of the object is based on the interface write, so there are fewer dependencies on the implementation.

Iv. realization of the relationship

Dashed + arrow

(PS:Widegoose class implements the IFly interface.) The Wild Goose realizes the Flying interface)

[CSharp]View Plaincopyprint?
    1. Class widegoose:ifly{
    2. }

Realization of Relationship key understanding interface Definition

Interface (interface), an interface is a special kind of abstract class that contains only the definitions of constants and methods, without the implementation of variables and methods.

UML class diagram Five relationships and code correspondence

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.