通过接口暴露聚合与委托

发布于 2024-09-30 08:59:27 字数 584 浏览 2 评论 0原文

我有一个 Employee 对象,它聚合了一些其他对象,例如 HRDataAssignmentHistory。过去,所有这些逻辑都直接包含在 Employee 对象中,但为了可测试性和可管理性,我将其拆分为使用聚合。然而,我没有直接公开聚合对象,而是使用委托,以便客户端不知道内部工作。例如,而不是这样做:

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

客户端会这样做:

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

我真的很喜欢这个,因为它遵循“黑匣子”方法,这意味着我可以随意更改实现而不影响客户端,同时仍然由小型可测试对象组成内部。问题是 Employee 对象已经显着增长,因为它现在有 5 个聚合对象,并且它的接口充满了 getXXXOn() 方法。

您使用哪种方法?为什么?有没有我忽略的替代方案?我使用委托方法的问题是接口变得庞大,而暴露聚合对象的问题是代码不够灵活,并且客户端需要知道哪个聚合负责什么。有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩饰不了的爱 2024-10-07 08:59:27

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

Consider changing Employee to provide a GetEmployeeDataOn(Date) method, and a class for EmployeeDataOnDate that stores this date and has methods like GetProfile().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文