文章目录
- 组合和聚合的关系
- 组合
- 聚合
- 组合和聚合的比较
组合和聚合的关系
关联是通过其对象建立的两个单独的类之间的关系。关联可以是一对一,一对多,多对一,多对多。
在面向对象的编程中,一个对象与其他对象通信以使用该对象提供的功能和服务。组合和聚集是关联的两种形式。
先放图,三者关系具体如下
Association
组合
// Java program to illustrate the
// concept of Association
import java.io.*;
// class bank
class Bank
{
private String name;
// bank name
Bank(String name)
{
this.name = name;
}
public String getBankName()
{
return this.name;
}
}
// employee class
class Employee
{
private String name;
// employee name
Employee(String name)
{
this.name = name;
}
public String getEmployeeName()
{
return this.name;
}
}
// Association between both the
// classes in main method
class Association
{
public static void main (String[] args)
{
Bank bank = new Bank("Axis");
Employee emp = new Employee("Neha");
System.out.println(emp.getEmployeeName() +
" is employee of " + bank.getBankName());
}
}
输出
Neha is employee of Axis
在上面的示例中,两个单独的类Bank和Employee通过其对象关联。银行可以有很多员工,所以这是一对多的关系。
Aggregation
这是一种特殊的关系形式,其中:
- 它表示有“一种”关系。
- 它是单向关联,即单向关联。例如,部门可以有学生,但反之亦然,因此本质上是单向的。
- 在聚集中,两个条目都可以单独存在,这意味着结束一个实体不会影响另一个实体
聚合
// Java program to illustrate
//the concept of Aggregation.
import java.io.*;
import java.util.*;
// student class
class Student
{
String name;
int id ;
String dept;
Student(String name, int id, String dept)
{
this.name = name;
this.id = id;
this.dept = dept;
}
}
/* Department class contains list of student
Objects. It is associated with student
class through its Object(s). */
class Department
{
String name;
private List<Student> students;
Department(String name, List<Student> students)
{
this.name = name;
this.students = students;
}
public List<Student> getStudents()
{
return students;
}
}
/* Institute class contains list of Department
Objects. It is asoociated with Department
class through its Object(s).*/
class Institute
{
String instituteName;
private List<Department> departments;
Institute(String instituteName, List<Department> departments)
{
this.instituteName = instituteName;
this.departments = departments;
}
// count total students of all departments
// in a given institute
public int getTotalStudentsInInstitute()
{
int noOfStudents = 0;
List<Student> students;
for(Department dept : departments)
{
students = dept.getStudents();
for(Student s : students)
{
noOfStudents++;
}
}
return noOfStudents;
}
}
// main method
class GFG
{
public static void main (String[] args)
{
Student s1 = new Student("Mia", 1, "CSE");
Student s2 = new Student("Priya", 2, "CSE");
Student s3 = new Student("John", 1, "EE");
Student s4 = new Student("Rahul", 2, "EE");
// making a List of
// CSE Students.
List <Student> cse_students = new ArrayList<Student>();
cse_students.add(s1);
cse_students.add(s2);
// making a List of
// EE Students
List <Student> ee_students = new ArrayList<Student>();
ee_students.add(s3);
ee_students.add(s4);
Department CSE = new Department("CSE", cse_students);
Department EE = new Department("EE", ee_students);
List <Department> departments = new ArrayList<Department>();
departments.add(CSE);
departments.add(EE);
// creating an instance of Institute.
Institute institute = new Institute("BITS", departments);
System.out.print("Total students in institute: ");
System.out.print(institute.getTotalStudentsInInstitute());
}
}
输出结果
Total students in institute: 4
在这个例子中,存在一个有多个部门,比如,CSE,EE。每个部门又有多个学生。所以,我们创建一个指向一个或者多个Object(比如Object的List)的Institute类.这说明institute class通过对象和Department class建立联系,同理,Department class也通过对象和 Student class 建立联系。
它代表着有一个关系。
什么时候使用聚合?
使用聚合是实现代码重用的最好方式。
Composition
组合是聚合的一种受限形式,其中两个实体相互高度依赖。
- 它代表的部分与整体的关系
- 在组合中,两个实体互相依赖
- 当两个实体存在组合关系时,如果没有对方的存在,另一方也无法存在
// Java program to illustrate
// the concept of Composition
import java.io.*;
import java.util.*;
// class book
class Book
{
public String title;
public String author;
Book(String title, String author)
{
this.title = title;
this.author = author;
}
}
// Libary class contains
// list of books.
class Library
{
// reference to refer to list of books.
private final List<Book> books;
Library (List<Book> books)
{
this.books = books;
}
public List<Book> getTotalBooksInLibrary(){
return books;
}
}
// main method
class GFG
{
public static void main (String[] args)
{
// Creating the Objects of Book class.
Book b1 = new Book("EffectiveJ Java", "Joshua Bloch");
Book b2 = new Book("Thinking in Java", "Bruce Eckel");
Book b3 = new Book("Java: The Complete Reference", "Herbert Schildt");
// Creating the list which contains the
// no. of books.
List<Book> books = new ArrayList<Book>();
books.add(b1);
books.add(b2);
books.add(b3);
Library library = new Library(books);
List<Book> bks = library.getTotalBooksInLibrary();
for(Book bk : bks){
System.out.println("Title : " + bk.title + " and "
+" Author : " + bk.author);
}
}
}
输出结果
Title : EffectiveJ Java and Author : Joshua Bloch
Title : Thinking in Java and Author : Bruce Eckel
Title : Java: The Complete Reference and Author : Herbert Schildt
组合和聚合的比较
- 依赖关系:聚合意味着子代可以独立于父代而存在的关系。 例如,银行和雇员,删除银行和雇员仍然存在。 而“组合”则意味着孩子不能独立于父母而存在的关系。 例子:人与心,心不与人分开存在
- 关系类型:聚集关系为“具有”,组合关合系为“部分”关系。
- 关联类型:组合是强关联,而聚合是弱关联。