当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Aggregation和Composition的区别用法及代码示例


聚合和组合说明了对象之间相互通信时的关系类型,这可以在低级设计中用于说明对象之间的关联。在本文中,我们将讨论 Java 编程语言中聚合和组合之间的差异。

Aggregation

它是一种特殊形式的协会,其中:

  • 它代表Has-A的关系。
  • 它是单向关联,即one-way 关系。例如,一个系可以有学生,但反之亦然是不可能的,因此本质上是单向的。
  • 在聚合中,两个条目都可以单独生存,这意味着结束一个实体不会影响另一个实体。
Example of Aggregation in Java

例子:

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。每个部门都没有。学生的。因此,我们创建一个 Institute 类,它具有或不具有 Object 的引用。 Department 类的对象(即对象列表)。这意味着 Institute 类通过其对象与 Department 类关联。 Department 类还引用了 Student 类的对象或对象(即对象列表),这意味着它通过其对象与 Student 类关联。它代表Has-A 关系。在上面的例子中:学生Has-A姓名。学生Has-A ID。学生 Has-A 部门 部门 Has-A 学生如以下媒体所示。

我们什么时候使用聚合?

代码重用最好通过聚合来实现。

作品

组合是聚合的一种受限形式,其中两个实体彼此高度依赖。

  • 它代表part-of关系。
  • 在组合中,两个实体相互依赖。
  • 当两个实体之间存在组合时,组合对象不能离开另一个实体而存在。

例子:

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

输出说明:在上面的示例中, Library 可以拥有许多相同或不同主题的书籍。因此,如果 Library 被摧毁,那么该特定 Library 内的所有书籍都将被摧毁。也就是说,如果没有 Library ,书籍就不可能存在。这就是为什么它是组合。书是Part-of Library 。

Difference Between Aggregation and Composition in Java

聚合

作品

聚合可以说明为“Has-a”关系,表示对象之间的关联。 组合意味着一个对象包含在另一个对象中。它是一种特殊类型的聚合(即Has-a关系),这意味着一个对象是另一个对象的所有者,这可以称为所有权关联。
对象之间存在相互依赖关系。 存在单向关系,这也称为“part of”关系。
它是弱类型关联,两个对象都有自己独立的生命周期。 它是强类型的关联(聚合),子对象没有自己的生命周期。
关联对象可以独立存在并有自己的生命周期。 孩子的生活取决于父母的生活。只有父对象有独立的生命周期。
白菱形的 UML 表示表示聚合。 Black Diamond 的 UML 表示表示组合。
例如,学生和部门之间的关系。学生可能没有院系而存在。 例如,一个文件包含在一个文件夹中,如果删除该文件夹,里面的所有文件都会被删除。如果没有文件夹,文件就不能存在。


相关用法


注:本文由纯净天空筛选整理自thejavamentor大神的英文原创作品 Difference Between Aggregation and Composition in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。