In Java and other object-oriented design patterns, there are mainly six types of relationships between classes: dependency, association, aggregation, composition, inheritance, and implementation. Their coupling degree increases in order.
- Vertical Relationships
- Inheritance
- Implementation
- Horizontal Relationships
- Dependency
- Association
Whole and Part
Composite whole and part, clustered whole and part
Inheritance/Generalization Relationship#
Inheritance refers to a class (referred to as a subclass, sub-interface) inheriting the functionality of another class (referred to as a superclass, super-interface) and being able to add its own new functionality. In Java, the inheritance relationship is explicitly identified by the keyword extends
and generally has no controversy in design. In UML class diagram design, inheritance is represented by a solid line with an empty triangle arrow, pointing from the subclass to the superclass or from the sub-interface to the super-interface.
Implementation Relationship#
Implementation refers to a class
implementing the functionality of an interface
(which can be multiple). Implementation is the most common relationship between classes and interfaces. In Java, this relationship is explicitly identified by the keyword implements
and generally has no controversy in design. In UML class diagram design, implementation is represented by a dashed line with an empty triangle arrow, pointing from the class to the implemented interface.
Dependency Relationship#
Simply put, dependency means that a class A
uses another class B
, and this usage relationship is accidental, temporary, and very weak. Once the method is executed, this relationship no longer exists. It means that the functionality was temporarily used, but changes to class B
will affect class A
. For example, if someone wants to cross a river, they need to borrow a boat. The relationship between the person and the boat is a dependency. In terms of code, class B
is used as a parameter by class A
in a certain method. In UML class diagram design, the dependency relationship is represented by a dashed line with an arrow pointing from class A
to class B
. In the entire flow of the program (the lifecycle of the program), in a certain step or a few limited steps, the functionality of another class is needed to complete it.
One class calling another class in a method
In simple terms: it means that a local variable of a class is a reference to an object of another class
Association Relationship#
Association reflects a strong dependency relationship at the semantic level between two classes, such as me and my friend. This relationship is stronger than dependency, does not have the contingency of dependency, and is not temporary. It is generally long-term, and the relationship between the two sides is usually equal. Association can be one-way or two-way. In terms of code, the associated class B
appears in the associating class A
as a property of the class,
ClassC cc = new ClassC()
ClassC.staticVar
Global variable refers to its member variable, static variable
It may also be that the associating class A
references a globally defined variable of type associated class B
. In UML class diagram design, the association relationship is represented by a solid line with an arrow pointing from the associating class A to the associated class B, and the roles and multiplicity of both sides of the association can be annotated.
It means that a class member variable or static variable is a reference to another class or a reference to a class object
Aggregation Relationship#
Aggregation is a special case of the association relationship. It reflects the relationship between a whole and its parts, that is, the has-a relationship. At this time, the whole and the parts are separable, they can have their own lifecycles, and the parts can belong to multiple whole objects or be shared by multiple whole objects. For example, the relationship between a computer and a CPU, a company and its employees, etc., such as a carrier battle group including aircraft carriers, escort ships, carrier-based aircraft, and nuclear-powered attack submarines. In terms of code, it is consistent with the association relationship, and can only be distinguished from the semantic level. In UML class diagram design, the aggregation relationship is represented by a solid line with an empty diamond and a solid line arrow.
The elements in a class's collection are references to objects of another class
Composition Relationship#
Composition is also a special case of the association relationship. It reflects a contains-a relationship, which is stronger than aggregation and is also known as strong aggregation. It also reflects the relationship between a whole and its parts, but at this time, the whole and the parts are inseparable. The end of the lifecycle of the whole also means the end of the lifecycle of the parts, such as a person and their brain. In terms of code, it is consistent with the association relationship and can only be distinguished from the semantic level. In UML class diagram design, the composition relationship is represented by a solid line with a solid diamond and a solid line arrow.
The elements in a collection in a class are references to objects of another class
Summary#
There is not much doubt about the inheritance and implementation relationships, as they represent a vertical relationship between classes or between classes and interfaces. The other four relationships represent references between classes or between classes and interfaces, which are difficult to distinguish and there are many relationships between things that are difficult to accurately locate. As mentioned earlier, these four relationships are all semantic-level, so they cannot be completely distinguished from the code level. However, in general, the strength of the relationships represented by the latter four relationships is as follows: composition > aggregation > association > dependency.
Software development process
Project -> Determine requirements -> Design modules (models) -> Coding -> Testing -> Delivery and deployment -> Post-maintenance