In Java and other object-oriented design patterns, there are six main relationships between classes: dependency, association, aggregation, combination, inheritance, and implementation. Their Coupling Degree increases sequentially.
1. Dependency)
The dependency is defined as: For two relatively independent objects, when an object is responsible for constructing an instance of another object or dependent on the service of another object, the two objects are mainly dependent. Definition is obscure, but it is more intuitive in Java: Class A uses Class B, class B is called as a method parameter of Class A, a local variable in the method, or a static method. In the example above, the people class depends on the book class and the food class. The book class and the food class appear in the people class as parameters of methods in the class.
Sample Code:
Public class people {// book is the public void read (Book) {system. out. println ("read book is" + book. getname ());}}
2. Association ),,
Unidirectional Association:
Bidirectional Association:
For two relatively independent objects, when the instance of one object has a fixed ing relationship with some specific instances of the other object, the two objects are associated. Association is divided into one-way Association and two-way Association. In Java, unidirectional Association is represented by Class B Used in Class A, where class B is a member variable of Class. Bidirectional Association: Class A uses Class B as the member variable, and Class B also uses Class A as the member variable.
Sample Code:
Public class son {// the class that acts as a member variable in the association relationship will usually assign Father = new father (); Public void getgift () {system. out. println ("slave" + father. getname () + "get gift");} public class father {son = new son (); Public void givegift () {system. out. println ("to" + son. getname () + "gift ");}}
3. Aggregation)
Aggregation is a type of association. The coupling degree is stronger than Association. Their code performance is the same, but the difference is only in semantics: the objects of the Association relationship are mutually independent, there is an inclusive relationship between the objects of the aggregation relationship, and they are the "Whole-individual" relationship.
Sample Code:
Public class people {car; house; // classes in the aggregate relationship that act as member variables generally use the set method to assign values to public void setcar (car) {This. car = car;} public void sethouse (house) {This. house = house;} public void Driver () {system. out. println ("car model:" + car. getType ();} public void sleep () {system. out. println ("I am sleeping in the House:" + house. getaddress ());}}
4. Composition)
Compared with aggregation, combination is a more coupling Association. A class with a composite relationship represents the association between "Whole-part", and "whole" is responsible for the life cycle of "part". They are co-occurrence and co-occurrence; and the "part" exists independently. In this example, there is a combination between people and soul and body. At the beginning of a person's life cycle, there must be both a soul and a body. When a person's life cycle ends, the soul and body die. Neither the soul nor the body can exist independently. They must exist as part of a person.
Public class people {SOUL soul; body; // member variables in the composite relationship are usually assigned public people (SOUL soul, body) {This. soul = soul; this. body = body;} public void Study () {system. out. println ("learning to use the soul" + soul. getname ();} public void eat () {system. out. println ("Eat with body:" + body. getname ());}}
5. Generalization)
Inheritance represents the parent-child relationship between classes (or interfaces. In Java, the extends keyword indicates the inheritance relationship. In the UML legend, the inheritance relationship is represented by a solid line + hollow arrow, and the arrow points to the parent class.
6. Implementation)
Indicates the method that a class implements one or more interfaces. The interface defines the set of operations, and the Implementation class completes the specific operations of the interface. Use implements in Java. In the UML legend, the implementation relationship is represented by a dotted line + a hollow arrow, and the arrow points to the interface.
In Java, the extends keyword is inherited and the implements keyword is used, which is intuitive. The code is not demonstrated.
From: http://blog.csdn.net/zhengzhb/article/details/7187278
In Java and other object-oriented design patterns, there are six main relationships between classes: dependency, association, aggregation, combination, inheritance, and implementation. Their Coupling Degree increases sequentially.
1. Dependency)
The dependency is defined as: For two relatively independent objects, when an object is responsible for constructing an instance of another object or dependent on the service of another object, the two objects are mainly dependent. Definition is obscure, but it is more intuitive in Java: Class A uses Class B, class B is called as a method parameter of Class A, a local variable in the method, or a static method. In the example above, the people class depends on the book class and the food class. The book class and the food class appear in the people class as parameters of methods in the class.
Sample Code:
Public class people {// book is the public void read (Book) {system. out. println ("read book is" + book. getname ());}}
2. Association ),,
Unidirectional Association:
Bidirectional Association:
For two relatively independent objects, when the instance of one object has a fixed ing relationship with some specific instances of the other object, the two objects are associated. Association is divided into one-way Association and two-way Association. In Java, unidirectional Association is represented by Class B Used in Class A, where class B is a member variable of Class. Bidirectional Association: Class A uses Class B as the member variable, and Class B also uses Class A as the member variable.
Sample Code:
Public class son {// the class that acts as a member variable in the association relationship will usually assign Father = new father (); Public void getgift () {system. out. println ("slave" + father. getname () + "get gift");} public class father {son = new son (); Public void givegift () {system. out. println ("to" + son. getname () + "gift ");}}
3. Aggregation)
Aggregation is a type of association. The coupling degree is stronger than Association. Their code performance is the same, but the difference is only in semantics: the objects of the Association relationship are mutually independent, there is an inclusive relationship between the objects of the aggregation relationship, and they are the "Whole-individual" relationship.
Sample Code:
Public class people {car; house; // classes in the aggregate relationship that act as member variables generally use the set method to assign values to public void setcar (car) {This. car = car;} public void sethouse (house) {This. house = house;} public void Driver () {system. out. println ("car model:" + car. getType ();} public void sleep () {system. out. println ("I am sleeping in the House:" + house. getaddress ());}}
4. Composition)
Compared with aggregation, combination is a more coupling Association. A class with a composite relationship represents the association between "Whole-part", and "whole" is responsible for the life cycle of "part". They are co-occurrence and co-occurrence; and the "part" exists independently. In this example, there is a combination between people and soul and body. At the beginning of a person's life cycle, there must be both a soul and a body. When a person's life cycle ends, the soul and body die. Neither the soul nor the body can exist independently. They must exist as part of a person.
Public class people {SOUL soul; body; // member variables in the composite relationship are usually assigned public people (SOUL soul, body) {This. soul = soul; this. body = body;} public void Study () {system. out. println ("learning to use the soul" + soul. getname ();} public void eat () {system. out. println ("Eat with body:" + body. getname ());}}
5. Generalization)
Inheritance represents the parent-child relationship between classes (or interfaces. In Java, the extends keyword indicates the inheritance relationship. In the UML legend, the inheritance relationship is represented by a solid line + hollow arrow, and the arrow points to the parent class.
6. Implementation)
Indicates the method that a class implements one or more interfaces. The interface defines the set of operations, and the Implementation class completes the specific operations of the interface. Use implements in Java. In the UML legend, the implementation relationship is represented by a dotted line + a hollow arrow, and the arrow points to the interface.
In Java, the extends keyword is inherited and the implements keyword is used, which is intuitive. The code is not demonstrated.
From: http://blog.csdn.net/zhengzhb/article/details/7187278