Relationships between classes - for example with java

There are six general relationship between java classes, dependent, association, aggregation, composition, inheritance (generalization), achieved.

Coupling: dependence <association <Polymerization <compositions <inheritance <achieved

Dependencies

In simple terms, that is dependent on use. A class will be used Classes B, this relationship has the chance, temporary. For example the use of a class of attributes, methods, or in the method as a parameter, is output as a return value or in the method. [Depends-a]

the Person class {public 
public static void Speak () {
System.out.println ( "speak");
}
}
class Student {
// use parameter dependencies occur embodiment
public void speak1 (the Person Person) {
person.speak () ;
}
// local variable dependency occurs
public void speak2 () {
the Person Person = new new the Person ();
person.speak ();
}
// static variable dependency occurs
public void speak3 () {
person.speak () ;
}
}

 

connection relation

A class will use class B, which is a strong dependence, long-term is not accidental. Performance in the code is: A class member variable classes containing B. Association may be bidirectional, it can be one-way. In JAVA, unidirectional association as follows: Class A Class B which uses, as a Class B type wherein A member variable. Bidirectional association as follows: Class A Class B which was used as a member variable; while Class B, Class A was also used as a member variable. [Association]

 

Phone {class public 
public void Call () {
System.out.println ( "call");
}
}
class the User {
// implemented using variables associated with the form member
Phone Phone = new new Phone ();
public void use () {
Phone .call ();
}
}

 

Aggregation relationship

A special case of the relationship, the relationship is strong, and is an integral part of the relationship, i.e., a has-a relationship. Between the whole and the part is detachable, they have their own life cycle. And integral part of the relationship and the relationship between different combinations is: The whole comprises a number of parts, but here are the same part. It is a one-way relationship between the class (or entity) between. For example: wallet and money wallet will be money, money which does not contain wallet; this is a natural way relationship.

 

In the polymerization relationship, two types (or entity) may be present alone, they do not affect each other; that is: a class (or entity) in the presence of other class do not affect the presence or absence of polymerized therewith. In JAVA, the relationship between the polymerization is generally used to achieve variable member, the association of both the polymerization performance of the code is the same, merely differ in semantics.

 

class Wallet {public 
Money Money;
// polymerization relationship as a member variable of the class assignment method using the set general
public void setCounts (Money Money) {
This.money = Money;
}
}

 

Combination of relationship

 

Is a combination of limited polymerized form, represents the part-of relationship; wherein the two entities (or classes) are highly dependent on each other. Like for example: the human heart and the human heart needs to survive, the human heart also needs to survive.

 

In other words, when the class (entity) dependent on each other and their life is the same (if a person has died, then the other is dead), then it is a composition. For example: If there are no human heart it does not make sense.

the Person class {public 
Brain Brain;
Heart Heart;

member // variable composition relationship generally assigned in constructor
Public the Person (Brain Brain, Heart Heart) {
This.brain = Brain;
This.heart = Heart;
}

public void Think () {
System.out.println (brain.toString () + "Reflection");
}
public void beat () {
System.out.println (heart.toString () + "jitter");
}
}

Combination dependent differences:

1. polymerization is a weak relationship; composition is a strong relationship.

2. Polymerization represents a has-a relationship, a one-way relationship; represents a combination of part-of relationship.

Two types (or entities) 3. The polymerization may be present alone, they do not affect each other; one of the associated one association may be independent, dependent not strong. In contrast, the combination of the two entities (or category) is highly dependent on one another, there are interactions between them.

4. In the polymerization relationship two classes (or entity) is not synchronized life cycle; however, two classes (or entity) in the combined relationship life cycle are synchronized.

 

Inheritance (generalization) relationship

I.e., "is-a" relationship, for a particular showing the general relationship. It represents a parent-child relationship class and class (or interface with the interface). In general, if a class A extended class B, class A from class B contains not only inherited method, but also has some additional features. In JAVA, represented by the keyword extends inheritance

Realization relationship

The relationship between classes and interfaces, a class representation implements one or more interfaces. Well defined set of operations of the interface, the implementation class to perform specific operation of the interface. Use implements expressed in java.

Reference links

https://blog.csdn.net/bupa900318/article/details/80650886

https://cloud.tencent.com/developer/article/1478673

Guess you like

Origin www.cnblogs.com/hhd-shuai/p/12497646.html