我想我在这里需要一个适配器模式,但我似乎在重复方法实现细节
您可以在下面看到我有两个具体的类,它们可以共享几乎所有已通过抽象类提供的实现细节。他们每个人只需要自己实现 2-3 个方法。不过,ImplOne 也实现了 InterfaceTwo,后者还需要实现 1-2 个附加方法。我不想在 ImplOne 中提供与 AbstractImpl 中完全相同的实现细节,但我目前在这里提供的内容似乎不正确。在适配器模式中,从我看到的示例来看,所适配的接口的实现细节似乎是重复的。
InterfaceOne
/ \
InterfaceTwo AbstractImpl
\ / \
ImplOne ImplTwo
You can see below that I have two concrete classes that can share almost all of the implementation details already provided through an abstract class. They each only have to implement 2-3 methods themselves. However, ImplOne also implements from InterfaceTwo, which has an additional 1-2 methods to implement. I don't want to have to provide the exact same implementation details in ImplOne as I do in AbstractImpl, but what I have here currently does not seem right. In the adapter pattern, from the examples I've seen, the implementation details do seem to be duplicated for the interfaces being adapted.
InterfaceOne
/ \
InterfaceTwo AbstractImpl
\ / \
ImplOne ImplTwo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个场景是没有问题的。 ImplOne 必须实现在interfaceOne 或interfaceTwo 中声明的所有方法,除非已经由abstractImpl 实现。 implOne 还必须实现abstractImpl 的抽象方法。作为练习,我建议创建一个不带方法的 ImplOne,并让编译器告诉您需要实现哪些方法。
It is no problem with this scenario. ImplOne must implement all methods declared in interfaceOne or interfaceTwo unless already implemented by abstractImpl. implOne must also implement abstract methods from abstractImpl. As an exercise, I suggest creating an ImplOne with no methods and let the compiler tell you what methods need implementing.