【发布时间】:2014-07-05 22:21:37
【问题描述】:
我必须在 java 代码中实现以下类图。该图非常复杂,某些部分会造成混乱。这个问题肯定对我和任何读者都有很大帮助,因为它包含 UML 图的几个重要方面。
class Book{
String isbn;
String publisher;
String publishDate;
int pages;
}
class BookItem extends Book{
String barcode;
boolean isReferenceOnly;
}
class Author{
String name;
String biography;
Collection<Book> book;
}
class Account{
String number;
List<History> history;
String openDate;
AccountState state;
public Account(AccountState state){
this.state = state;
}
}
enum AccountState{
Active,
Frozen,
Closed
}
class Catalog implements Search, Manage{
List<BookItem> bookItem;
/* Implement the methods of Manage interface */
void add(BookItem item){ }
void remove(BookItem item){ }
/* Implement the methods of Search interface */
int search(BookItem item){ }
}
class Account{
String number;
List<History> history;
Student student = new Student();
void setStudent(Student student){
this.student = student;
}
}
interface Search{
int search(BookItem item);
}
interface Manage{
void add(BookItem item);
void remove(BookItem item);
}
class Student{
String name;
String address;
Search searchBook = new Catalog();
}
class Librarian{
String name;
String address;
String position;
Search searchBook = new Catalog();
Manage manage = new Catalog();
Account account = new Account();
void setAccount(Account account){
this.account = account;
}
class Library{
String name;
String Address;
List<BookItem> bookItem = new ArrayList<BookItem>();
Catalog catalog = new catalog();
List<Account> accounts = new ArrayList<Account>();
Library(Catalog catalog){
this.catalog = catalog;
}
void setBookItem(List<BookItem> bookItem){
this.bookItem = bookItem;
}
void setAccounts(List<Account> accounts){
this.accounts = accounts;
}
}
我以以下方式实现,但在各种情况下都会出现混淆:
- 如何使用接口 Search 实现 Class Student。
- 如何使用接口 Search 和 Manage 实现 Class Librarian。
- 为什么我们不使用关联而不是使用依赖。
- 如何在这种情况下实现具有使用依赖性的 Enumeration 数据类型 [我刚刚将 AccountState 视为一个类,我认为它是一个错误的实现]。
- 如何在 Account 中使用 AccountState [我刚刚创建了一个 AccountState 对象]。
- 阅读了许多博客后,仍然无法自信地实现聚合和组合。 注意:在此图中存在 3 个聚合和 1 个组合。这些是:
(a) 图书馆由许多帐户组成。 {聚合}
(b) 许多图书项目是图书馆的一部分。 {聚合}
(c) 账户是学生的一部分。 {聚合}
(d) 图书馆必须有目录。 {作曲}
请多多指教,让我好好学习。谢谢。
【问题讨论】:
-
“枚举”不会让你想到
enum数据类型吗? -
那我们为什么要在这里使用它...
-
....因为他们告诉你这样做。它就在图中。
标签: java oop uml aggregation class-diagram