2010-07-22 52 views
1

对于以下情景设计我有一些疑问: 有一家公司。 典型的公司至少具有以下属性:(1 公司 2.员工在java中为真实生活场景造型

列表的名称

典型的员工具有以下属性: 1.姓名2. 系

典型处已至少以下属性: 1. numberOfEmps 2.描述

每个部门都应该保留该部门的员工数量,所以每当员工被添加或删除时,应分别增加或减少员工部门。

如何使用java类对此进行建模?w

回答

1

有以下类。

公司

String id; // Unique identifier for company. 
String name; // Name of company 

员工

String id; // Unique identifier of employee. 
String Name; // Name of employee 
Department department; // Instance of Department to which this employee belongs. 
Company company; // Instance of Company to which this employee belongs. 

String id; // Unique identifier of department. 
long employeeCount; // Count of employees. 
String description; // Description of department. 

public setEmployeeCount(); // Method which searches through all Employee objects matching current department with its unique id. 

注意。 Department和Employee的反向映射是为了确保在任何时候,其中一个类将Employee对象保留在列表中。如果员工数量增长到很大程度,这可能会成为一个值得关注的问题。

0

您可以创建员工和部门之间的双向关系:

的员工都知道他的部门,该部门知道属于它的员工。

如果更改了员工的部门,并且新部门与当前部门不同,则将该员工从当前部门中删除(如果当前部门不为空),将其添加到新部门中,并且当前部门是更新。

将员工添加到部门时,员工的部门已更改。