Java中的组合
组合是Java中实现has-a关系的一种设计技术。 Java继承用于代码重用目的,我们可以通过使用组合来做同样的事情。组合是通过使用引用其他对象的实例变量来实现的。如果一个对象包含另一个对象,并且如果没有该对象的存在,所包含的对象就不能存在,则称为组合。更具体地说,组合是使用实例变量描述两个或多个类之间引用的一种方式,并且应该在使用实例之前创建实例。
好处 使用组合如下:
- 组合允许重用代码。
- Java不支持多重继承,但通过使用组合我们可以实现它。
- 组合提供了更好的类测试能力。
- 通过使用组合,我们足够灵活,可以用更好和改进的版本替换组合类的实现。
- 通过使用组合,我们还可以在运行时更改成员对象,以动态更改程序的行为。
请记住Java中的某些关键点,如下所示:
- 它代表了一种有关系。
- 在组合中,两个实体相互依赖。
- 当两个实体之间存在组合时,组合对象不能没有另一个实体而存在。例如,图书馆可以没有。相同或不同主题的书籍。因此,如果图书馆被摧毁,那么该特定图书馆内的所有书籍都将被摧毁。这是因为没有图书馆,书籍就无法存在。
- 组合是通过使用引用其他对象的实例变量来实现的。
- 我们必须支持组合而不是继承。
现在让我们最后参考下图,以便对聚合有一个淡淡的暗示,并更好地理解组合是如何工作的,让我们以实时图书馆系统为例。
Real-life Example: Library system
Let’s understand the composition in Java with the example of books and library. In this example, we create a class Book that contains data members like author, and title and create another class Library that has a reference to refer to the list of books. A library can have no. of books on the same or different subjects. So, If the Library gets destroyed then All books within that particular library will be destroyed. i.e., books can not exist without a library. The relationship between the library and books is composition.
执行:
例子
Java
// Java program to Illustrate Concept of Composition
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Helper class
// Book class
class Book {
// Member variables of this class
public String title;
public String author;
// Constructor of this class
Book(String title, String author)
{
// This keyword refers top current instance
this.title = title;
this.author = author;
}
}
// Class 2
// Helper class
// Library class contains list of books.
class Library {
// Reference to refer to list of books.
private final List books;
// Constructor of this class
Library(List books)
{
// This keyword refers to current instance itself
this.books = books;
}
// Method of this class
// Getting the list of books
public List getListOfBooksInLibrary()
{
return books;
}
}
// Class 3
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating the objects of class 1 (Book class)
// inside main() method
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 = new ArrayList();
// Adding books to List object
// using standard add() method
book.add(b1);
book.add(b2);
book.add(b3);
// Creating an object of class 2
Library library = new Library(book);
// Calling method of class 2 and storing list of
// books in List Here List is declared of type
// Books(user-defined)
List books
= library.getListOfBooksInLibrary();
// Iterating over for each loop
for (Book bk : books) {
// Print and display the title and author of
// books inside List object
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