Six relationships between classes in java

Summary

Among the six relationships, the order from weak to strong is: Dependency, Association, Aggregation, Composition, Inheritance, and Realization.

An association is a structural relationship between classes .

Among them, aggregation and combination are special types of association, which represent the relationship between the whole and the part .

If only objects in class B are used in the method of class A, then class A depends on class B.

If class A controls the life cycle of class B, then class A and class B are in a composite relationship.

Line representation of six relationships in UML diagrams

Summary of the Six Relationships

Difference between dependency and association

relationship name specific relationship Distinguished embodiment
rely Use relation (semantic relation) Object a only calls the service of object b
associate Structural relationship Object a can get an instance of object b and call the service

If the one-way association of class A points to class B, there is an attribute B b in class A, that is, class B exists in class A as an attribute.

If class A depends on class B, it will not have this attribute. An instance of class B may exist in the parameters of a method call, or in the local variables of a method.

The difference between aggregation and composition

relationship name specific relationship Distinguished embodiment
polymerization weak structural relationships (whole and part) The whole does not exist, the part exists
combination Strong structural relationships (whole and part) The whole does not exist, the part does not exist

The difference between inheritance and implementation

relationship name specific relationship Distinguished embodiment
inherit general and special will be linked with semantic layer elements, usually within the same model
accomplish Semantic relationship Link elements of different semantic layers, usually within different models

1. Dependence

Concept: Dependence is the weakest relationship among the six major relationships, and it is a weak connection. It is a semantic relationship between two things , and it is a usage relationship . It is manifested that one object just calls the service of another object, and a change of one thing (independent thing) will affect the other thing (dependent thing). semantics.

Concrete embodiment:

Usually the use of class libraries is one of the withdrawals. For example, the sqrt() square root function is called in the program, and class B is used in class A, that is, class B appears in the form of method parameters or local variables in the method. in A.

The code is reflected as follows:

public class Clothes {
    
    
	private String clothesName="大风衣";

	public String getClothesName() {
    
    
		return this.clothesName;
	}
}

public class Food {
    
    
	private String food="糖醋里脊";

	public String getAceType() {
    
    
		return this.food;
	}
	
}

public class Person {
    
    
	private String name;
	
	public void showInfo(Food food,Clothes clothes) {
    
    
		System.out.println(name+"穿"+clothes+"吃"+food);
	}
}


In a UML drawing it is a dashed arrow like this:
rely

In this example, human beings depend on clothes and food. Without clothes and food, people cannot eat and wear, and changes in clothes and food will also affect human changes.

Two, association

Concept: Association is a structural relationship, which describes a chain of connections between a group of objects. There are one-way associations, two-way associations and self-associations (only one class is involved) . Multiple degrees, role names, and description associations can be added to the chain The number of objects and their behavior, etc. In the association relationship, there are special types— aggregation and composition , which are used to describe the structural relationship between parts and wholes.

Concrete embodiment:

self-association: self-contained

self-association

One-way association: class A knows class B without requiring class B to know class A. (class B as an attribute of class A)

Code embodiment:

public class Teacher {
    
    
	private String  tchName="小明";

	public String getTchName() {
    
    
		return this.tchName;
	}

}

public class Student {
    
     
	private String  stuName;
	Teacher xiaoMing=new Teacher();
	public void showInfo() {
    
    
		System.out.println(xiaoMing.getTchName()+"教了这位"+stuName+"同学");
	}
	
}

In UML diagrams, association relationships are represented by solid arrows, as shown below:

one-way association

In this example, one-way association, the student class one-way new the object of the teacher, and calls the method of this object.

Bidirectional association: Class A needs to know about class B, and class B needs to know about class A. (Class AB are attributes of each other)

Code embodiment:

public class Student {
    
     
	private String  stuName="狗蛋";
	Teacher xiaoMing=new Teacher();
	
	public void showInfo() {
    
    
		System.out.println(xiaoMing.getTchName()+"教了这位"+stuName+"同学");
	}

	public String getStuName() {
    
    
		return this.stuName;
	}
}

public class Teacher {
    
    
	private String  tchName="小明";
	Student gd=new Student();

	public String getTchName() {
    
    
		return this.tchName;
	}
	
	public void showInfo() {
    
    
		System.out.println("学生"+gd.getStuName()+"是我的学生");
	}
}

two-way association

In this example, two-way association, the student class new the object of the teacher, and the teacher class also new the object of the student, and call their respective methods.

3. Polymerization

Concept: Aggregation can be understood as a set of references (composition of references) , the whole does not exist, but parts can exist alone. It reflects the relationship between the whole and the part. The whole and the part can be separated, and each has its own life cycle. Parts can belong to multiple whole objects, and can also be shared by multiple whole objects. Aggregation implies that subtypes are independent of The parent type exists.

Concrete embodiment:

For example, students and teachers form a class. After the class is dissolved, the teacher and students can still exist independently. It is a has-a relationship , the class has a student.

Similarly, there are employees and departments, you and your family, the relationship between the components on the main board and the main board, etc.

Code embodiment:

public class Student {
    
     
	private String  stuName="狗蛋";
	public String getStuName() {
    
    
		return this.stuName;
	}
}

public class Teacher {
    
    
	private String  tchName="小明";
	public String getTchName() {
    
    
		return this.tchName;
	}
}

In the UML class diagram, it is represented by a hollow diamond and an arrow, and the diamond points to the whole, as shown in the following figure:
polymerization

In this example, the class is the whole, and the teacher and the students are parts. When the class does not exist, the teacher and the students can still exist independently. For example, Goudan has entered a higher school, and the second class of the third year he is in is no longer there. , his teacher Xiao Ming also went to teach other classes.

4. Combination

Concept: Combination can be understood as a set of values ​​(synthesis of values). If the whole does not exist, the part does not exist. It is a strong ownership relationship. Portions of class A consist of objects of class B, and class A controls the lifecycle of class B. Composition implies that a subtype cannot exist without a supertype. There is a symbiosis between the whole and the part.

Concrete embodiment:

The relationship between pages and books, the relationship between limbs and people, etc., they have a contains-a relationship.

Code embodiment:

public class Page {
    
    
	private int pageNums=100;//页数
	private String pageType="护眼纸张";//纸的类型
	public int getPageNums() {
    
    
		return pageNums;
	}
	public String getPageType() {
    
    
		return pageType;
	}	
	
}

public class Book {
    
    
	private String bookName="金刚经";
	Page num1=new Page();
	
	public void  showBookInfo() {
    
    
		System.out.println(bookName+"由"+num1.getPageNums()+"页的"
				+num1.getPageType()+"型的纸类型构成");
	}

}

In the UML class diagram, it is represented by a solid diamond and an arrow, and the diamond points to the whole, as shown in the following figure:

combination

In this example, the book is combined with the page, and the page cannot exist independently of the book.

5. Inheritance

Concept: Inheritance, also known as generalization, is the relationship between a class and one or more of its refinement classes, that is, the relationship between general and special . The object of a special element (child element) can replace the object of a general element (parent element) [Liskov substitution] . In this way, the child element shares the structure and behavior of the parent element.

Concrete embodiment:

The relationship between the animal class and the cat class, the relationship between the handover tool class and the airplane class, the cat class is a animal class, and they have the is a relationship.

Code embodiment:

public class Animal {
    
    
	public void eat() {
    
    
		System.out.println("动物会吃东西");
	}
	
	public void speak() {
    
    
		System.out.println("动物会吼叫");
	}
}

//猫类重写了父类的方法
public class Cat extends Animal{
    
    
	public void eat() {
    
    
		System.out.println("猫吃老鼠");
	}
	
	public void speak() {
    
    
		System.out.println("猫会喵喵叫");
	}
}


In the UML class diagram, use a hollow triangle to point to the parent class, and a solid line, as shown in the following figure:
inherit

In this example, the cat class inherits the animal class, and can overload the methods of the parent class. Use the extends keyword to inherit the parent class, and can inherit all the methods and properties of the parent class except private methods and properties.

6. Realization

Concept: A realization is a semantic relationship between classifiers , where one classifier specifies a contract guaranteed to be enforced by another classifier.

Concrete embodiment:

Java implements multiple inheritance through interfaces. An interface or abstract class defines a set of operations, and the specific implementation class completes the specific operations of the interface.

Code embodiment:

public interface PaoMian {
    
    
	public void jiashui();//往泡面加热水
	
	public void waitTime();//等待泡面泡熟
	
	public void eatMian();//开吃
}

public class EatPaoMian implements PaoMian{
    
    

	@Override
	public void jiashui() {
    
    
		// TODO 自动生成的方法存根
		System.out.println("往泡面加入热水");
	}

	@Override
	public void waitTime() {
    
    
		// TODO 自动生成的方法存根
		System.out.println("等待三分钟...");
	}

	@Override
	public void eatMian() {
    
    
		// TODO 自动生成的方法存根
		System.out.println("泡面熟了,开吃");
	}
}

In the UML class diagram, use a hollow triangle to point to the interface, and a hollow line, as shown in the following figure:
accomplish

In this example, the abstract interface Instant Noodles defines three operations: add water, wait, and start eating, and the class Eating Instant Noodles implements this interface and embodies the process operation of instant noodles.

Guess you like

Origin blog.csdn.net/qq_42242452/article/details/124831266