In object-oriented methods, after determining the attributes and methods of classes and classes, it is inevitable to study the relationships between classes and classes. These relationships link the entire thing, in real life, there are different relationships between people. Understanding these relationships does not harm our development system. It allows us to work easily and efficiently, just as we handle relationships between good people in our daily lives, it will bring us great benefits. Let's take a look at the relationships between classes and classes in object-oriented systems.
The relationships between classes can be divided into several types: generalization, realization, dependency, and association) (Association, aggregation, and composition )). The generalized relationship and implementation relationship reflect the relationship between a class and a class, or between a class and an interface. There is no reference, which is attributed to a vertical relationship; dependencies and associations refer to references between classes and classes, and between classes and interfaces.
I. Generalized relationship
A generalized relationship is a function that inherits the functions of another class (parent class and parent interface) and can have its own new functions, it is also an inherited relationship. It is identified by extends in Java. In UML, an implementation with a hollow arrow indicates that the Child class points to the parent class, or the child interface points to the parent interface.
The Code is as follows: Class _ A {} class _ B extends _ A {} public class test {public static void main (string ARGs []) {_ A = new _ B ();}}
II. Implementation relationship
The implementation relationship refers to the interface implemented by the class (multiple interfaces can be made ). Use implements in Java and a dotted line with hollow arrow in UML to direct the class to the implemented interface.
The Code is as follows: interface a {} Class B implements a {} public class test {public static void main (string ARGs []) {B = new B ();}}
Note: The above generalization and implementation are one-on-one. Both of them use hollow triangles. The inheritance relationship is more coupled than the interface relationship. Therefore, the inheritance uses solid lines to implement interfaces using dotted lines.
Iii. Dependency
Dependency is the connection between classes. It indicates that one class depends on the definition of another class. Dependency is always unidirectional. Dependency is the least coupled relationship among the five relationships of the class. Because the dependency class does not add attributes when generating code. This weak relationship can be explained by the degree of mutual understanding between classes. In Java, dependency is embodied in local variables, parameters in methods, and calls to static methods .. In UML, dependency is represented by an arrow dotted line pointing to Class B by Class.
The Code is as follows:
Class A{ }Class B{ }
Dependency 1:
Class A is a variable in a method of Class B, and Class B can call it. The Code is as follows:
Class B{ Public void info(){ Private A a; }}
Note: B has an info method. Class A is used as the variable of this method. Class A is instantiated only when class B's info method is called.
Dependency Form 2:
When Class A is a parameter or return value of a method in Class B, the Code is as follows:
Class B { Public A info( A a){ Return null; }}
Dependency representation 3:
One of Class A is a static method, and Class B can call it.
Needless to say, Class A is held by a method of Class B. The life cycle ends with the execution of the method.
Iv. Association
Association is a kind of strong dependency between two classes at the semantic level. This relationship is more dependent than dependency, and has no dependency relationship. The relationship is not temporary, and is generally long-term, it allows a class to know the attributes and methods of another class, and the relationship between the two parties is generally equal. Association can be unidirectional and bidirectional. In Java, associations are implemented using instance variables. In UML, the association relationship is represented by an arrow with a solid line pointing to the associated Class B from association class A. The roles and multiple tags of the associated parties can be marked at both ends of the association.
One-way Association:
Class A { }Class B { Public A a; }
The two-way Association is as follows:
Class A { Public B b; }Class B { Public A a; }
Note: differences between dependency and association:
① From the perspective of adding class attributes:
No attribute is added to any of the dependent classes. One of these classes serves as a parameter or return value for the method of another class, or as a variable of a method.
Two classes with associations, one of which becomes the attribute of another class, and the property is a more closely coupled, more persistent hold relationship.
② From the perspective of the link life cycle:
Dependency is generated only when the class method is called and ends with the end of the method.
Associations are generated when the class is instantiated. When the class is destroyed, the link ends. Compared with dependency, the associated relationship has a longer lifetime.
Because the association relationship is more coupled than the dependency relationship, the association relationship uses the solid line, and the dependency uses the dotted line.
After the association is refined, there are two special cases: Aggregation and combination.
(1) Aggregation relationship: it is the relationship between the whole and the individual, that is, the relationship between has-a. At this time, the whole and the part can be separated, and they can have their own lifecycles, some of them can belong to multiple overall objects, or they can be shared with multiple overall objects. They are represented at the code level and are consistent with their associations. They can only be distinguished at the semantic level. From the Java syntax, It is not out-of-the-box and aggregated. In UML, the aggregation relationship is represented by a hollow diamond plus a solid arrow.
The Code is as follows:
Class B { }Class A{ Public B b; A (B b){ This.b = b; }}
Note: In an association, two classes are at the same level, while in an aggregation relationship, two classes are at an unequal level. One Class indicates the whole, and the other class indicates the part.
(2) composite relationship: it represents a contains-a relationship, which is stronger than aggregation, also known as strong aggregation. It also reflects the relationship between the whole and the part, however, at this time, the whole and part are inseparable. The end of the whole life cycle means that part of the life cycle is over. For example, you and your brain, and the compositing relationship cannot be shared .. It is represented at the code level and is consistent with the association. It can only be distinguished at the semantic level. In UML, the composite relationship is represented by a solid diamond plus a solid arrow.
The Code is as follows:
Class B { }Class A{ Public B b; A (){ b = new Wings(); }}
Note:The difference between the two relationships is that
① Different constructors
The aggregate class constructor contains another class as a parameter. B is used as the parameter in the constructor of. A can exist independently of the wild goose group.
The constructor of the composite class contains the instantiation of another class. It indicates that a must first instantiate B before instantiation. These two classes are closely coupled and co-occurrence and elimination. Class B cannot exist independently from Class.
② Different information Encapsulation
In the aggregation relationship, the client can understand both Class A and Class B, because they are independent
In the composite relationship, the client only recognizes Class A and does not even know the existence of Class B, because Class B is tightly encapsulated in Class.
③ Different lifecycles:
Aggregation is the separation between the whole and the part. They can have their own lifecycles and will not disappear because of the disappearance of one party.
Aggregation is integral and partial, and the end of the overall lifecycle means that the end of the partial lifecycle.