UML class diagram relationship and its corresponding code

Keywords: C++ Design Pattern UML

0. Introduction

This blog It's very well written. Most of it is reprinted from this blog in case of forgetting. At the same time, some content is added and expressed in C + +. It's all repeated wheels. It's shameless to mark it as original.

1. Overview of UML class diagram relationships

Various categories and relationships under text interpretation

classMeaning and relationship
Food classFood, People, food
Adress classAddress class, People class, associated address class
PeopleGroup classCrowd class, PeopleGroup class, aggregate People class
Heart classHear t class and heart class are combined into People class
Animal classAnimals, People inherit Animal
Eatting interfaceThe People class implements this interface
  • Relational coupling: inheritance (generalization) = implementation > composition > aggregation > Association > dependency

The following describes and gives the object code from low to high from the perspective of relational coupling

2. Dependencies

1. Relationship description

  • Definition: the usage relationship between classes, i.e. Use a. one class uses another class, or one class depends on the services of another class

  • Class A depends on class B, which can be used as parameters, local variables and method return value types of class A methods
    Or the static method of calling class B in the A like method.
    Class A method has class B variables

2. Examples

To complete the eat method, the People class needs to use the Food class, that is, the Food class needs to provide the getName method as a service

Class People{
	//The Food class is used as an argument to the eat method
	public void eat(Food food){
		std::cout<<"What food are you eating"<<food.getName())<<std::endl;
	}
}

3. Relationship

1. Relationship description

  • Definition: indicates that a class has another class, which can be expressed as has a
  • Class A is associated with class B, and class B is the member variable of class A

2. Examples
Each People object has a contact address

Class People{
	//Address class as member variable
	private Address address_;
}

3. Think: why is dependency less coupled than correlation?

  • It depends on the local variables of the corresponding object method and associates the member variables of the corresponding object.
  • Member variables and objects have the same life cycle, that is, class A has always been associated with class B
  • Only when the local variable is called can class A have A dependency on class B
  • Therefore, it can be inferred from the duration of the relationship that the degree of dependence is lower than the degree of correlation coupling

4. Aggregation and combination relationship - two forms of association relationship

1. Relationship description

  • Aggregation: describes that there is a "whole part" relationship between two classes. The life cycle of some classes depends on itself, which is more coupled than the association relationship
  • Composition: it is a special case of aggregation and a "whole part" relationship. The life cycle of some classes depends on the whole, which is more coupled than aggregation

2. Comparison between the two

Some aggregation / combination relationships are also marked with numerical scale:


Aggregation:

Class PeopleGroup
{
	Public:
		PeopleGroup(People people)
			: people_(people){}
	//People as a class member variable
	private: 
		People people_;
}

Combination:

Class People
{
public:
	People()
	{
		heart_ = new Heart();
	}
private:
	//Heart class as member variable
	Heart heart_;
}

3. Relationship comparison

  • Common ground: the "part" class is the member variable of the "whole" class, and the member variable type can also be an array of "part" type
  • Differences: constructors are different.
    • Aggregation: the "whole" class contains the "part" class as the parameter. The constructor of the PeopleGroup class needs the parameter of the People type. The People class can exist independently of the PeopleGroup class, that is, the part can exist independently of the whole.
    • Combination: the "whole" class contains the instantiation of the "part" class. The Heart class must be instantiated before the instantiation of the People class. The Heart class cannot exist independently of the People class, that is, the part cannot exist independently

5. Inheritance (generalization) relationship

1. Relationship description

  • Subclasses inherit the properties and methods of the parent class, except private members and constructors
  • The relationship is described as is a, for example, man is an animal
  • The subclass inherits the parent class, and the parent class generalizes the subclass

2. Examples

class People : public Animal{
}

3. Thinking: origin of synthesis (aggregation) Reuse Principle

The essence of this principle is: why use less inheritance relations and more combination (synthesis) aggregation relations

  • 1. The subclass inherits all the public and protected methods of the parent class, even if the parent method is harmful or useless to the subclass
  • 2. The coupling degree is high, and the change of parent class will also cause the change of child class, which may also cause the infinite expansion of class system
  • 3. The inherited subclass actually needs to be determined during compilation, which can not meet the situation that the object can be determined only in runtime. Composition can be much more flexible than inheritance, and an object can be determined at run time. If a subclass wants to instantiate its parent class, it must instantiate it first. Therefore, the object to be instantiated has been determined at compile time and cannot dynamically generate the specified object

Composition aggregation relationship is the core of structural design patterns. Typical design patterns include agent pattern, adapter pattern, decoration pattern and so on

5, Implementation relationship

Relationship description and examples

  • Class implements the interface, and the coupling degree between the implementation and generalization relationship is the same. Let's see an example below
  • For example, the People class implements the Eating interface and rewrites the eat method

class People implements Eating(){
	//If the method is rewritten, the method of the interface will not be written. Ha ha
	public void eat(Food food){
		System.out.println("What food are you eating"+food.getName())
	}
}

6. Final summary

  • Relational coupling: inheritance (generalization) = implementation > composition > aggregation > Association > dependency

UML diagram

code:

//uml.h
#ifndef _UML_H_
#define _UML_H_
#include <iostream>
#include <string>
using namespace std;

class Animal{
private:
public:
    Animal();
    ~Animal();
};

class Address{
private:
public:
    Address();
    ~Address();
};

class Food{
private:
    string name_ = "Apple";
public:
    Food();
    ~Food();
    string GetFood() {return name_;}
};

class Heart{
private:
    /* data */
public:
    Heart(/* args */);
    ~Heart();
};

class Eatting
{
private:
    /* data */
public:
    Eatting();
    ~Eatting();
    virtual void eat(Food food) = 0;
};

// Inheritance: "is a" relationship
class People : public Animal ,public Eatting{
private:
    // Association (ownership relationship -- person owns Address): the Address class is used as a member variable
	Address address_;
    // Combination (relationship between whole and part -- Heart is part of human): Heart class as member variable
	Heart heart_;
    // Attribute: person name
    string name_;
public:
    People(/* args */);
    ~People();
    // Dependency (use relationship -- People class uses Food class): Food class is the parameter of People class eat() method
    // Rewrite, is this also the implementation of the interface?
    void eat(Food food)
    {
        std::cout<<name_ <<"I am eating"<<food.GetFood()<<std::endl;
    }
};

class PeopleGroup{
public:
private:
    // Aggregate (relationship between whole and part -- People are part of the population) People as a class member variable
    People people_;
};

#endif

Posted by hhawkins on Fri, 15 Oct 2021 12:55:50 -0700