Document directory
- 3.1 Association, aggregation, and combination
- 3.3 Generalization
- 3.2 dependency
- 3.4 Implementation
It is intercepted from the staruml tool interface, and the UML relationships are represented from top to bottom: Association, unidirectional Association, aggregation, combination, generalization, dependency, and implementation.
Figure 3 UML relational set
3.1 Association, aggregation, and combination
Association is a structural relationship that specifies the relationship between the object of a transaction and the object of another transaction. Aggregation and combination are strongly correlated, indicating the relationship between the whole and the part.
Aggregation is not responsible for part of the life cycle, and the combination is responsible for part of the life cycle. The association relationship needs to be identified based on actual scenarios. For example, the relationship between the military and soldiers can be understood as aggregation. When a soldier retires, it is separated from the army. However, if the soldiers have to live and live together in a war, and the soldiers have no life if the army is gone, they can be understood as a combination.
UML example:
Figure 3-1 Association, aggregation, and combination
C sample code:
[CPP]
View plaincopyprint?
- // A Association/aggregation/combination of B
- Struct {
- Struct B * B;
- Void (* Create) (B * B); // method 1
- };
- Struct {
- Struct B * B;
- Void (* SETB) (B * B); // method 2, single B
- };
- Struct {
- Struct B * bset [N];
- Void (* registerb) (B * B); // method 3, Set B
- };
// A Association/aggregation/combination of B <br/> struct a {<br/> struct B * B; <br/> void (* Create) (B * B); // method 1 <br/>}; </P> <p> struct a {<br/> struct B * B; <br/> void (* SETB) (B * B); // method 2, single B <br/> }; </P> <p> struct a {<br/> struct B * bset [N]; <br/> void (* registerb) (B * B ); // method 3, Set B <br/>}; <br/>
3.3 Generalization
Generalization is a special/general relationship. It can also be seen as an inherited relationship.
The following is a typical example of inheritance in C language, which is copied from the Linux kernel source code.
UML example:
Figure 3-2 Inheritance (generalization)
C sample code (strikethrough version ):
[CPP]
View plaincopyprint?
- Struct kobject {
- Const char * Name;
- Struct kobject * parent;
- };
- Struct cdev {
- Struct kobject kobj; // inherit from kobject
- Const struct file_operations * OPS;
- Dev_t dev;
- Unsigned int count;
- };
- Struct scullc_dev {
- Void ** data;
- Struct scullc_dev * next;
- Struct cdev; // inherits cdev
- };
Struct kobject {<br/> const char * Name; <br/> struct kobject * parent; <br/> }; </P> <p> struct cdev {<br/> struct kobject kobj; // inherit kobject <br/> const struct file_operations * OPS; <br/> dev_t dev; <br/> unsigned int count; <br/>}; </P> <p> struct scullc_dev {<br/> void ** data; <br/> struct scullc_dev * Next; <br/> struct cdev; // inherit cdev <br/>}; </P> <p>
Note: inheritance is not a struct pointer, but a struct.
3.2 dependency
Dependency is the semantic relationship between two things. A change in one thing (independent thing) affects the semantics of another thing (dependent thing. The most common dependency is the definition of another class in the constructor of a class.
UML example:
Because class customers rely on interfaces to implement class operations, we model interaction with interfaces asDependency.
Figure 3-3 dependency
C sample code:
[CPP]
View plaincopyprint?
- // Method 1
- Void a_method (struct a *)
- {
- B _method (); // B is the region class (Global instance). This dependency method is the most popular in our system.
- }
- // Method 2
- Void a_method (struct a * a, const struct B * B); // B is passed as a parameter
- // Method 3
- Void a_method (struct a *)
- {
- Struct B * B = B _create (); // B is instantiated in method.
- }
// Method 1 <br/> void a_method (struct a * A) <br/>{< br/> B _method (); // B is a global instance ), this dependency method is the most <br/>}</P> <p> // method 2 <br/> void a_method (struct a *, const struct B * B); // B is passed as a parameter </P> <p> // method 3 <br/> void a_method (struct a *) <br/>{< br/> struct B * B = B _create (); // B is instantiated in method A <br/>}</P> <p>
3.4 Implementation
Implementation is the semantic relationship between class elements. One Class element specifies the contract that is executed by another class element.
Next we will take the Linux device driver as an example. The scullc_dev class implements the file operation interface file_operations.
UML example:
Figure 3-4 Implementation
C sample code:
[CPP]
View plaincopyprint?
- // Implement interface functions
- Int scullc_open (struct inode * inode, struct file * filp)
- {
- Struct scullc_dev * dev;/* device information */
- /* Find the device */
- Dev = container_of (inode-> I _cdev, struct scullc_dev, cdev );
- /*...*/
- /* And use filp-> private_data to point to the device data */
- Filp-> private_data = dev;
- Return 0;/* success */
- }
- Ssize_t scullc_read (struct file * filp, char _ User * Buf, size_t count,
- Loff_t * f_pos)
- {
- Struct scullc_dev * Dev = filp-> private_data;/* The first listitem */
- Struct scullc_dev * dptr;
- Int quantum = Dev-> quantum;
- /*...*/
- Return retval;
- }
- //...
- // File operation interface instance
- Struct file_operations scullc_fops = {
- . Owner = this_module,
- . Llseek = scullc_llseek,
- . Read = scullc_read,
- . Write = scullc_write,
- . IOCTL = scullc_ioctl,
- . Open = scullc_open,
- . Release = scullc_release,
- };
- //
- Static void scullc_setup_cdev (struct scullc_dev * Dev, int index)
- {
- Int err, devno = mkdev (scullc_major, index );
- Cdev_init (& Dev-> cdev, & scullc_fops); // register the interface
- Dev-> cdev. Owner = this_module;
- Dev-> cdev. Ops = & scullc_fops;
- Err = cdev_add (& Dev-> cdev, devno, 1 );
- /* Fail gracefully if need be */
- If (ERR)
- Printk (kern_notice "error % d adding scull % d", err, index );
- }
// Implement interface functions </P> <p> int scullc_open (struct inode * inode, struct file * filp) <br/>{< br/> struct scullc_dev * dev; /* device information */</P> <p>/* find the device */<br/> Dev = container_of (inode-> I _cdev, struct scullc_dev, cdev ); </P> <p> /*... */</P> <p>/* and use filp-> private_data to point to the device data */</P> <p> filp-> private_data = dev; </P> <p> return 0;/* success */<br/>}</P> <p> ssize_t SCU Llc_read (struct file * filp, char _ User * Buf, size_t count, <br/> loff_t * f_pos) <br/> {<br/> struct scullc_dev * Dev = filp-> private_data;/* The first listitem */<br/> struct scullc_dev * dptr; <br/> int quantum = Dev-> quantum; <br/> /*... */</P> <p> return retval; <br/>}</P> <p> //... </P> <p> // file operation interface instance </P> <p> struct file_operations scullc_fops ={< br/>. owner = this_module, <br/>. llseek = scullc_llseek, <br/>. read = scullc_read, <br/>. write = scullc_write, <br/>. IOCTL = scullc_ioctl, <br/>. open = scullc_open, <br/>. release = scullc_release, <br/>}; </P> <p> // <br/> static void scullc_setup_cdev (struct scullc_dev * Dev, int index) <br/>{< br/> int err, devno = mkdev (scullc_major, index); </P> <p> cdev_init (& Dev-> cdev, & scullc_fops ); // register the API <br/> Dev-> cdev. owner = this_module; <br/> Dev-> cdev. ops = & scullc_fops; <br/> err = cdev_add (& Dev-> cdev, devno, 1 ); </P> <p>/* fail gracefully if need be */<br/> If (ERR) <br/> printk (kern_notice "error % d adding scull % d", err, index); <br/>}< br/>