Relationships between classes in Design Patterns

Source: Internet
Author: User

In the object-oriented design model, there are six main relationships between classes: dependency, association, aggregation, combination, inheritance, and implementation. Their Coupling Degree increases sequentially.

1. Dependency)

Description:

It can be simply understood that a class A uses another class B, and this relationship is contingent, temporary, and very weak, but changes in Class B will affect. It is manifested in code. Class A uses Class B, where class B is used as a method parameter of Class A, a local variable in the method, or a static method call. Note: Avoid bidirectional dependency. Generally, there should be no two-way dependency.

 1 //book.h 2 class Book 3 {} 4  5 //food.h 6 class Food 7 {} 8  9 //people.h10 #include "book.h"11 #include "food.h"12 class People13 {14 void read(Book book);15 void eat(Food food);16 }

2. Association)

Description:

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, two-way Association, and self-Association. One-way Association: Class B is 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. The link must be mapped to an object reference or pointer.

Bidirectional Association:

1 class son 2 {3 Public: 4 void getgift () 5 {6 cout <"from" <father. getname <"get present"; 7} 8 9 private: 10 father & father; 11} 12 13 class father14 {15 public: 16 void setgift () 17 {18 cout <"sent to" <son. getname <"gift"; 19} 20 21 private: 22 son & Son; 23}

3. Aggregation/Combination

(1) aggregation: C1 aggregates C2, but C2 can leave C1 and exist independently (it means that the existence of this class makes sense in the problematic domain of an application. For more information about this sentence, see the explanation in the combination below ).

 1 class Car{} 2 class House{} 3 class People 4 { 5 public: 6     void Sleep(); 7     void Drive(); 8 private: 9    Car car;10    House house;  11 }

(2) Composition: C1 combines C2, and C2 cannot leave C1 and exist independently. However, this depends on the problem domain. For example, in the field of concerned about automobiles, tires must be combined in the automobile industry because it makes no sense when it leaves the automobile. However, in the shops where tires are sold, even if the tires leave the car, it makes sense, and this can be aggregated. In Agile development, we also said that if a combines B, A needs to know the lifecycle of B, that is, a may be responsible for generating or releasing B, or a knows the generation and release of B in some way.

4. Generalization)

Inheritance represents the parent-child relationship between classes (or interfaces.

 1 class Animal 2 { 3     virtual void Eat(); 4     virtual void Sleep(); 5 } 6 class People : public Animal 7 { 8     virtual void Eat(); 9     virtual void Sleep();10     void Study();11 }

5. 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.

Note: Let's talk about the degree of repetition. After reading the above description, we should be clear about the relationship between the various relations and the specific code corresponding to the relationship, the so-called degree of repetition, it is just an extension above. For example, if a and B have a "one-to-many" repetition degree, there is a list in a that stores n references of object B, that's all.

Relationships between classes 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.