There are three most common relationships between classes: dependency (uses-a), aggregation (has-a) and inheritance (is-a).
Let’s take the online bookstore order system as an example to explain the concepts of these three relationships in detail.
The main function of the online bookstore order system is: registered users can log in to the online bookstore to purchase books, fill in orders online, and pay for the purchase of books. When the bookstore confirms that the purchase payment has been received, the book will be mailed according to the address left by the user. Several categories can be established in this system, including books, accounts, orders, addresses, etc., as shown in the figure below:
Dependency (uses-a)
Dependencies are the most common relationships in the class. For example, the order class needs to access the user account class, so the account class needs to be referenced in the order class, that is, the order class depends on the account class, but the book class does not need to rely on the account class.
If you modify the account class, it will affect the order class. The essence of dependency is that methods in a class can operate instances of another class. In actual programming, it is recommended to minimize the number of interdependent classes. As shown in the figure below:
Aggregation (has-a)
Because the order needs to indicate what books to order, this involves the book category, that is, it includes the book category. The difference between aggregation and dependency is that the order class may not own all account class objects, but must own all book class objects, because the objects of the book class are the main purpose of the order, as shown in the figure below:
inherit
Inheritance means that one class can call all data members of another class and does not need to be redefined in the current class. This chapter has already explained it very clearly.