Java中聚合与组合的区别
改进文章
保存文章
喜欢的文章
- 最后更新: 2022年6月9日
聚合和组合描述了对象之间在相互通信时的关系类型,这可能被用于底层设计,描述对象之间的关联。在这篇文章中,我们将讨论Java编程语言中聚合和组合的区别。
聚合(Aggregation
它是关联的一种特殊形式,其中。
- 它表示Has-A的关系。
- 它是一种单向的关联,即单向的关系。例如,一个系可以有学生,但反之亦然是不可能的,因此是单向性的。
- 在聚合中,两个条目都可以单独存在,这意味着结束一个实体不会影响另一个实体。
例子。
Java
// Java program to illustrate the// Concept of Aggregation// Importing required classesimport java.io.*;import java.util.*;// Class 1// Student classclass Student {// Attributes of studentString name;int id;String dept;// Constructor of student classStudent(String name,int id, String dept){// This keyword refers to current instance itselfthis.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 Objectsclass Department {// Attributes of Department classString name;private List<Student> students;Department(String name, List<Student> students){// this keyword refers to current instance itselfthis.name = name;this.students = students;}// Method of Department classpublic List<Student> getStudents(){// Returning list of user defined type// Student typereturn students;}}// Class 3// Institute class contains list of Department// Objects. It is associated with Department// class through its Objectsclass Institute {// Attributes of InstituteString instituteName;private List<Department> departments;// Constructor of institute classInstitute(String instituteName,List<Department> departments){// This keyword refers to current instance itselfthis.instituteName = instituteName;this.departments = departments;}// Method of Institute class// Counting total students of all departments// in a given institutepublic 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 classclass GFG {// main driver methodpublic 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 StudentsList<Student> cse_students =new ArrayList<Student>();// Adding CSE studentscse_students.add(s1);cse_students.add(s2);// Creating a List of EE StudentsList<Student> ee_students=new ArrayList<Student>();// Adding EE studentsee_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 InstituteInstitute institute =new Institute("BITS", departments);// Display message for better readabilitySystem.out.print("Total students in institute: ");// Calling method to get total number of students// in institute and printing on consoleSystem.out.print(institute.getTotalStudentsInInstitute());}} |
输出。
Total students in institute: 4
输出 解释。在这个例子中,有一个学院有很多系,如CSE和EE。每个系都有一些学生。因此,我们建立了一个学院类,它有一个对系类的对象或对象数量(即对象列表)的引用。这意味着学院类通过其对象与系类相关联。而系类也有对学生类的对象或对象(即对象列表)的引用,这意味着它通过其对象与学生类相关联。这代表了一种Has-A关系。在上面的例子中。学生有一个名字。学生有一个ID。学生有一个部门,部门有一个学生,正如下面的媒体所描述的。
我们什么时候使用聚合?
代码重用最好通过聚合来实现。
组成
组成是聚合的一种限制性形式,在这种形式中,两个实体彼此高度依赖。
- 它代表了部分关系。
- 在组合中,两个实体都是相互依赖的。
- 当两个实体之间有一个组合时,被组合的对象不能离开另一个实体而存在。
例子。
爪哇
// Java program to illustrate// the concept of Composition// Importing required classesimport java.io.*;import java.util.*;// Class 1// Bookclass Book {// Attributes of bookpublic String title;public String author;// Constructor of Book classBook(String title, String author){// This keyword refers to current instance itselfthis.title = title;this.author = author;}}// Class 2class Library {// Reference to refer to list of booksprivate final List<Book> books;// Library class contains list of booksLibrary(List<Book> books){// Referring to same book as// this keyword refers to same instance itselfthis.books = books;}// Method// To get total number of books in librarypublic List<Book> getTotalBooksInLibrary(){return books;}}// Class 3// Main classclass GFG {// Main driver methodpublic static void main(String[] args){// Creating objects of Book class inside main()// method Custom inputsBook 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 booksList<Book> books =new ArrayList<Book>();// Adding books// using add() methodbooks.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 -// BooksList<Book> bks = library.getTotalBooksInLibrary();// Iterating over books using for each loopfor (Book bk : bks) {// Printing the title and author name of book on// consoleSystem.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表示表示组成。 | | 例如,一个学生和一个部门之间的关系。学生可能不存在一个部门。 | 例如,一个文件包含在一个文件夹中,如果这个文件夹被删除,里面的所有文件都会被删除。没有文件夹,文件就不能存在。 |
我的个人笔记arrow_drop_up
保存