简体   繁体   English

通过接口公开聚合与委托

[英]Exposing aggregation through the interface vs. delegation

I have an Employee object which aggregates a few other objects, such as HRData and AssignmentHistory . 我有一个Employee对象,它聚合了其他一些对象,例如HRDataAssignmentHistory 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. 过去,所有这些逻辑都直接包含在Employee对象中,但是为了可测试性和可管理性,我将其拆分为使用聚合。 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. 问题在于Employee对象已经有了相当大的增长,因为它现在有5个聚合对象,并且其接口上杂乱了getXXXOn()方法。

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()的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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