In Java and other object-oriented design patterns, there are 6 main types of relationships between classes and classes: Dependency, association, aggregation, composition, inheritance, implementation. Their coupling degree is increased in turn.
1. Dependency (dependence)
Dependencies are defined as: for two relatively independent objects, when one object is responsible for constructing an instance of another object, or a service that relies on another object, the two objects are primarily dependent. The definition is obscure, but the performance in Java is fairly straightforward: Class A uses Class B, where Class B is a method parameter of Class A, a local variable in a method, or a static method call. Class in the legend above: The People class relies on the book class and the Food class, and the book class and the food class appear in the People class as parameter forms of the methods in the class.
Code Sample:
[Java]View Plaincopy < param name= "wmode" value= "Transparent" >
- Public class people{
- //book as the formal parameter of the Read method
- Public void read (book book) {
- System.out.println ("Read the book is" +book.getname ());
- }
- }
2. Association (association),
One-Way Association:
Bidirectional Association:
For two relatively independent objects, when an instance of one object has a fixed correspondence with some specific instance of another object, the two objects are associated. Association relationships are divided into one-way and two-way associations. In Java, a one-way association behaves as follows: Class A uses Class B, where Class B is the member variable of Class A. The bidirectional association is represented by the use of Class B as a member variable in Class A, and Class A as a member variable in Class B.
Code Sample:
[Java]View Plaincopy
- Public class son{
- //A class that is a member variable in an association is typically assigned a value in a class
- Father Father = new Father ();
- Public void getgift () {
- System.out.println ("from" +father.getname () + "Get Gift");
- }
- }
- Public class father{
- Son son = new son ();
- Public void givegift () {
- System.out.println ("Give" +son.getname () + "gift");
- }
- }
3. Aggregation (Aggregation)
The aggregation relation is one kind of correlation relation, the coupling degree is stronger than the association, their code performance is the same, only has the semantic difference: the Relation object is mutually independent, but the aggregation relation object has the containment relation, between them is "the whole-individual" the interrelationship.
Code Sample:
[Java]View Plaincopy
- Public class people{
- Car car;
- House House;
- A class that is a member variable in an aggregate relationship is typically assigned using the Set method
- Public void setcar (car car) {
- This.car = car;
- }
- Public void Sethouse (House house) {
- This.house = House;
- }
- Public void driver () {
- SYSTEM.OUT.PRINTLN ("Model of the car:" +car.gettype ());
- }
- Public void sleep () {
- System.out.println ("I sleep in the house:" +house.getaddress ());
- }
- }
4. Combination (composition)
In contrast to aggregation, a combination is a more coupling relationship. A class that has a composition relationship represents the "whole-part" relationship, and "the whole" is responsible for the life cycle of the "part", and they are symbiotic, and there is no meaning in the "partial" existence alone. In the example, people and soul, the body is a combination of relations, when the life cycle begins, must have both the soul and the body, when the end of the human life cycle, the soul of the body with its demise, whether it is the soul or the body, can not exist alone, they must be a part of human existence.
[Java]View Plaincopy
- Public class people{
- Soul Soul;
- Body Body;
- the member variables in the//composite relationship are typically assigned in the constructor method
- Public people (Soul Soul, body body) {
- This.soul = Soul;
- This.body = body;
- }
- Public void study () {
- System.out.println ("Learn to use the Soul" +soul.getname ());
- }
- Public void eat () {
- System.out.println ("Eat with Body:" +body.getname ());
- }
- }
5. Inheritance (generalization)
Inheritance represents a parent-child relationship between a class and a class (or interface and interface). In Java, the inheritance relationship is represented by the keyword extends. In the UML legend, the inheritance relationship is represented by a solid + hollow arrow, and the arrow points to the parent class.
6. Implementation (Implementation)
Represents a method for a class to implement one or more interfaces. The interface defines the set of operations that are implemented by the implementation class to accomplish the specific operation of the interface. In Java, the implements representation is used. In the UML legend, the implementation relationship is indicated by a dashed + hollow arrow, and the arrow points to the interface.
Relationship of classes in design mode-category summary