简体   繁体   中英

Exposing aggregation through the interface vs. delegation

I have an Employee object which aggregates a few other objects, such as HRData and AssignmentHistory . In the past all of this logic was contained directly in the Employee object, but for testability and manageability I've split it to use aggregation. However, instead of exposing the aggregate objects directly, I've used delegation so that clients would be kept unaware of the internal working. For example, instead of doing this:

employee.getHRDataOn("2010-01-01").getProfile();

Clients would do this:

employee.getProfileOn("2010-01-01");

I really liked this because it followed a "black box" approach, which meant that I could change the implementation at will without affecting the clients, while still consisting of small, testable objects internally. Problem is the Employee object has grown considerably, as it now has 5 aggregate objects, and it's interface is littered with getXXXOn() methods.

Which approach do you use and why? Is there an alternative that I overlooked? My problem with using the delegate approach is that the interface becomes massive, and my problem with exposing the aggregate objects is that the code is less flexible and that the client needs to know which aggregate is responsible for what. Any suggestions?

考虑更改Employee以提供GetEmployeeDataOn(Date)方法,以及用于EmployeeDataOnDate的类,该类存储该日期并具有类似GetProfile()的方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM