In object-oriented design patterns, there are 6 main relationships between classes and classes : dependency , Association , Aggregation , composition , inheritance , implementation . Their coupling degree is increased in turn.
1. Reliance (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 relatively straightforward:
class a is used. b b is as a class a
In the above illustration: thepeople class relies on the book class and food class, book class and the food class appears in the people class as the parameter form of the method in the class .
Boilerplate code:
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 a 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 .
Boilerplate code:
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 relationship is a kind of association, the coupling degree is stronger than the association, their code performance is the same, only the semantic difference:
The relationship between the objects is independent of each other, and the object of the aggregation relationship between the existence of an inclusive relationship between them is the " whole - individual " relationship.
Boilerplate code:
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 " - part " " " responsible for " section " " section Span style= "font-family:arial;" > " There is no point in being alone.
In the example,people and Soul,body is a combination of relations, when the human life cycle begins, must have both the soul and the body When the life cycle of a person ends, the body of the soul dies, neither the soul nor the body can exist alone, and they must be part of the human being.
Boilerplate code:
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.
inheriting using the extends keyword in java , implementation uses the implements keyword , which is intuitive. It's not a code demo.
The relationship between classes and classes