Class relationships in Design Patterns

Source: Internet
Author: User

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:

[Java]View plaincopy
  1. Public class people {
  2. // Book is the form parameter of the read method.
  3. Public void read (Book ){
  4. System. Out. println ("read book is" + book. getname ());
  5. }
  6. }

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:

[Java]View plaincopy
  1. Public class son {
  2. // Classes used as member variables in associations are usually assigned values to classes.
  3. Father = new father ();
  4. Public void getgift (){
  5. System. Out. println ("get a gift" from "+ father. getname () + ");
  6. }
  7. }
  8. Public class father {
  9. Son son = new son ();
  10. Public void givegift (){
  11. System. Out. println ("to" + son. getname () + "gift ");
  12. }
  13. }

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:

[Java]View plaincopy
  1. Public class people {
  2. Car car;
  3. House house;
  4. // The set method is generally used to assign values to classes in an aggregate relationship that act as member variables.
  5. Public void setcar (car ){
  6. This. Car = car;
  7. }
  8. Public void sethouse (house ){
  9. This. House = house;
  10. }
  11. Public void Driver (){
  12. System. Out. println ("car model:" + car. GetType ());
  13. }
  14. Public void sleep (){
  15. System. Out. println ("I am sleeping in the House:" + house. getaddress ());
  16. }
  17. }

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.

[Java]View plaincopy
  1. Public class people {
  2. Soul soul;
  3. Body body;
  4. // Member variables in the composite relationship are usually assigned values in the constructor.
  5. Public people (SOUL soul, body ){
  6. This. Soul = soul;
  7. This. Body = body;
  8. }
  9. Public void Study (){
  10. System. Out. println ("learning to use the soul" + soul. getname ());
  11. }
  12. Public void eat (){
  13. System. Out. println ("Eat with body:" + body. getname ());
  14. }
  15. }

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.

Class relationships in Design Patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.