Class relationships in OO

Source: Internet
Author: User

In OODWe usually need to figure out the relationship between our classes and classes. There are usually the following types:

1. Inheritance
Inheritance may be the most understandable class relationship. It can use the" Is . It usually represents inheritance of classes and classes (abstract classes), inheritance of interfaces and interfaces, and implementation of classes on interfaces. For example, we now have an abstract class. DB , Mysqldb And Sqllitedb Use Extends Keyword inheritance DB Class. Mysqldb And Sqllitedb To implement DB Class defined abstract methods, and can inherit DB The existing method in. We can say that Mysqldb Yes DB . This relationship refers to a parent-child relationship. A parent-child relationship must have its son, but the child can be "blue and blue ". Note: In fact, there are two relationships: generalization and implementation relationships. Class and Class, interface and interface are generalized relationships, while class and interface are only implementation relationships.

2. Dependency
Two independent classes, but one of them uses another class. For example, the second class generates the local variable of the first class, and the second class acts as the parameter of the first class method. That is to say, the first class requires the help group of the second class. The first class uses the second class to work correctly. For exampleMVCIn the modeActionDependentModelTo obtain or update data. Dependency and dependency are only a usage relationship.

3. Join
Two independent classes. When a class has a specific ing with another class, this is an association. The first class usually refers to an attribute of the instance of the second class. For example, in a blog system, a blog corresponds to a specific blogger, while a blog has a specific blog. Note that these two classes are directly at the same level.

4. Aggregation
When the direct relationship between a class and a class is expressed as the whole and a part, it may be an aggregation relationship. Aggregation is mainly reflected in the word "aggregation. For example, students and teachers gather together to form a class. However, if there is a gathering, it is very likely that this class will be dispersed in a year, but all or some of these classes will be reunited with the students to form another class. Note that this class does not cause the absence of teachers or students due to disassociation.

5. Combination
A combination is an aggregation of stronger relationships. A combination not only contains the relationship between all and the whole, but also controls the lifecycle of another subject. For example, a human subject consists of a head, body, and lower extremity. Once a person is gone, his head, body, and lower extremity will no longer exist.

Direct differences between various links
Depending on the strength of the relationship, the dependency is sequential.<Association<Aggregation<Combination.

The difference between dependency and association: dependency is a weak Association. Specifically reflected inCodeThe above is: in the association relationship, the associated class is the attribute of the association class. In the dependency relationship, the dependent class is only the local variable of the dependent party, and the parameter of the method.

The difference between Association and aggregation: Aggregation has the relationship between the whole and the part, but the association does not. The associated classes are at the same level.

Aggregation and combination: they all have the relationship between the whole and the part, but in the combination, the class has the same lifecycle, that is, the whole class disappears, and the partial category does not exist.

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.