通过接口暴露聚合与委托
我有一个 Employee 对象,它聚合了一些其他对象,例如 HRData 和 AssignmentHistory。过去,所有这些逻辑都直接包含在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑更改 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().