Ing between modular C code and UML Object Model (3) -- UML relationship

Source: Internet
Author: User
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?
  1. // A Association/aggregation/combination of B
  2. Struct {
  3. Struct B * B;
  4. Void (* Create) (B * B); // method 1
  5. };
  6. Struct {
  7. Struct B * B;
  8. Void (* SETB) (B * B); // method 2, single B
  9. };
  10. Struct {
  11. Struct B * bset [N];
  12. Void (* registerb) (B * B); // method 3, Set B
  13. };

// 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?
  1. Struct kobject {
  2. Const char * Name;
  3. Struct kobject * parent;
  4. };
  5. Struct cdev {
  6. Struct kobject kobj; // inherit from kobject
  7. Const struct file_operations * OPS;
  8. Dev_t dev;
  9. Unsigned int count;
  10. };
  11. Struct scullc_dev {
  12. Void ** data;
  13. Struct scullc_dev * next;
  14. Struct cdev; // inherits cdev
  15. };

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?
  1. // Method 1
  2. Void a_method (struct a *)
  3. {
  4. B _method (); // B is the region class (Global instance). This dependency method is the most popular in our system.
  5. }
  6. // Method 2
  7. Void a_method (struct a * a, const struct B * B); // B is passed as a parameter
  8. // Method 3
  9. Void a_method (struct a *)
  10. {
  11. Struct B * B = B _create (); // B is instantiated in method.
  12. }

// 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?
  1. // Implement interface functions
  2. Int scullc_open (struct inode * inode, struct file * filp)
  3. {
  4. Struct scullc_dev * dev;/* device information */
  5. /* Find the device */
  6. Dev = container_of (inode-> I _cdev, struct scullc_dev, cdev );
  7. /*...*/
  8. /* And use filp-> private_data to point to the device data */
  9. Filp-> private_data = dev;
  10. Return 0;/* success */
  11. }
  12. Ssize_t scullc_read (struct file * filp, char _ User * Buf, size_t count,
  13. Loff_t * f_pos)
  14. {
  15. Struct scullc_dev * Dev = filp-> private_data;/* The first listitem */
  16. Struct scullc_dev * dptr;
  17. Int quantum = Dev-> quantum;
  18. /*...*/
  19. Return retval;
  20. }
  21. //...
  22. // File operation interface instance
  23. Struct file_operations scullc_fops = {
  24. . Owner = this_module,
  25. . Llseek = scullc_llseek,
  26. . Read = scullc_read,
  27. . Write = scullc_write,
  28. . IOCTL = scullc_ioctl,
  29. . Open = scullc_open,
  30. . Release = scullc_release,
  31. };
  32. //
  33. Static void scullc_setup_cdev (struct scullc_dev * Dev, int index)
  34. {
  35. Int err, devno = mkdev (scullc_major, index );
  36. Cdev_init (& Dev-> cdev, & scullc_fops); // register the interface
  37. Dev-> cdev. Owner = this_module;
  38. Dev-> cdev. Ops = & scullc_fops;
  39. Err = cdev_add (& Dev-> cdev, devno, 1 );
  40. /* Fail gracefully if need be */
  41. If (ERR)
  42. Printk (kern_notice "error % d adding scull % d", err, index );
  43. }

// 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/>

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.