Java中聚合与组合的区别
聚合(Aggregation
它是关联的一种特殊形式,其中。
- 它表示Has-A的关系。
- 它是一种单向的关联,即单向的关系。例如,一个系可以有学生,但反之亦然是不可能的,因此是单向性的。
- 在聚合中,两个条目都可以单独存在,这意味着结束一个实体不会影响另一个实体。
例子。
Java
// Java program to illustrate the
// Concept of Aggregation
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Student class
class Student {
// Attributes of student
String name;
int id;
String dept;
// Constructor of student class
Student(String name, int id, String dept)
{
// This keyword refers to current instance itself
this.name = name;
this.id = id;
this.dept = dept;
}
}
// Class 2
// Department class contains list of student objects
// It is associated with student class through its Objects
class Department {
// Attributes of Department class
String name;
private List<Student> students;
Department(String name, List<Student> students)
{
// this keyword refers to current instance itself
this.name = name;
this.students = students;
}
// Method of Department class
public List<Student> getStudents()
{
// Returning list of user defined type
// Student type
return students;
}
}
// Class 3
// Institute class contains list of Department
// Objects. It is associated with Department
// class through its Objects
class Institute {
// Attributes of Institute
String instituteName;
private List<Department> departments;
// Constructor of institute class
Institute(String instituteName,List<Department> departments)
{
// This keyword refers to current instance itself
this.instituteName = instituteName;
this.departments = departments;
}
// Method of Institute class
// Counting 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;
}
}
// Class 4
// main class
class GFG {
// main driver method
public static void main(String[] args)
{
// Creating object of Student class inside main()
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");
// Creating a List of CSE Students
List<Student> cse_students = new ArrayList<Student>();
// Adding CSE students
cse_students.add(s1);
cse_students.add(s2);
// Creating a List of EE Students
List<Student> ee_students
= new ArrayList<Student>();
// Adding EE students
ee_students.add(s3);
ee_students.add(s4);
// Creating objects of EE and CSE class inside
// main()
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);
// Lastly creating an instance of Institute
Institute institute = new Institute("BITS", departments);
// Display message for better readability
System.out.print("Total students in institute: ");
// Calling method to get total number of students
// in institute and printing on console
System.out.print(institute.getTotalStudentsInInstitute());
}
}
输出。
Total students in institute: 4
输出 解释。在这个例子中,有一个学院有很多系,如CSE和EE。每个系都有一些学生。因此,我们建立了一个学院类,它有一个对系类的对象或对象数量(即对象列表)的引用。这意味着学院类通过其对象与系类相关联。而系类也有对学生类的对象或对象(即对象列表)的引用,这意味着它通过其对象与学生类相关联。这代表了一种Has-A关系。在上面的例子中。学生有一个名字。学生有一个ID。学生有一个部门,部门有一个学生,正如下面的媒体所描述的。
我们什么时候使用聚合?
代码重用最好通过聚合来实现。
组成
组成是聚合的一种限制性形式,在这种形式中,两个实体彼此高度依赖。
- 它代表了部分关系。
- 在组合中,两个实体都是相互依赖的。
- 当两个实体之间有一个组合时,被组合的对象不能离开另一个实体而存在。
例子。 Java
// Java program to illustrate
// the concept of Composition
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Book
class Book {
// Attributes of book
public String title;
public String author;
// Constructor of Book class
Book(String title, String author)
{
// This keyword refers to current instance itself
this.title = title;
this.author = author;
}
}
// Class 2
class Library {
// Reference to refer to list of books
private final List<Book> books;
// Library class contains list of books
Library(List<Book> books)
{
// Referring to same book as
// this keyword refers to same instance itself
this.books = books;
}
// Method
// To get total number of books in library
public List<Book> getTotalBooksInLibrary()
{
return books;
}
}
// Class 3
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating objects of Book class inside main()
// method Custom inputs
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 number of books
List<Book> books = new ArrayList<Book>();
// Adding books
// using add() method
books.add(b1);
books.add(b2);
books.add(b3);
Library library = new Library(books);
// Calling method to get total books in library
// and storing it in list of user0defined type -
// Books
List<Book> bks = library.getTotalBooksInLibrary();
// Iterating over books using for each loop
for (Book bk : bks) {
// Printing the title and author name of book on
// console
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
输出 解释。在 上面的例子中,一个图书馆可以有许多关于相同或不同主题的书籍。因此,如果图书馆被毁,那么该图书馆内的所有书籍都会被毁。也就是说,没有图书馆,书籍就无法存在。这就是为什么它是构成。书是库的一部分。
Java中聚合和组合的区别
聚合可以被描述为一种 "Has-a "关系,它表示对象之间的关联。 组成意味着一个对象包含在另一个对象中。它是聚合的一种特殊类型(即Has-a关系),它意味着一个对象是另一个对象的所有者,这可以称为所有权关联。 对象之间存在着相互依赖的关系。 有一种单向的关系,这也被称为 "部分 "关系。 这是一种弱关联类型,两个对象都有自己独立的生命周期。 这是一种强关联类型(聚合),子对象没有自己的生命周期。 关联对象可以独立存在,有自己的生命周期。 子对象的生命取决于父对象的生命。只有父对象有一个独立的生命周期。 白钻石的UML表示表示聚合。 黑钻石的UML表示表示组成。 例如,一个学生和一个部门之间的关系。学生可能不存在一个部门。 例如,一个文件包含在一个文件夹中,如果这个文件夹被删除,里面的所有文件都会被删除。没有文件夹,文件就不能存在。