单独的解码/编码接口或在一个接口中
我正在创建一个执行从一种形式到另一种形式的转换的实现。
我现在面临的设计问题是Encoder和Decoder API应该在一个接口中还是在单独的接口中。 例如 Apache MINA 使用 单独的接口
我目前正在做这样的事情:
interface Convertor
{
A encode( B b );
B decode( A a );
}
将它们放在一个接口中的基本原理是,您可以集中实现并在一个地方修复任何协议更改。 对此有什么想法吗?
I'm creating an implementation that performs conversion from one form to another.
The design problem I am facing now is whether the Encoder and Decoder API should be in one interface or in separate ones. e.g. Apache MINA uses separate interfaces
I am currently doing something like this:
interface Convertor
{
A encode( B b );
B decode( A a );
}
The rationale for putting them in one interface is that you can centralize the implementations and fix any protocol changes in one place. Any thoughts on this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
拥有单独的接口并不意味着您不能集中实施。 例如,您可以让一个类实现这两个接口。 或者每个类都可以引用实现该协议的公共类。
所以我要做的就是拥有单独的接口,并且至少在一开始就让一个类同时实现这两个接口。 因此,实现是共享的,但用户代码将编码器和解码器视为单独且独立的概念。
Having separate interfaces doesn't mean you can't centralize the implementation. For example you could have one class implement both interfaces. Or each class could reference a common class which implements the protocol.
So what I'd do is have separate interfaces, and at least to begin with, have one class implement both. So the implementation is shared, but user code sees the encoder and decoder as separate and independent concepts.
唯一的问题是,您通常有一个使用解码器的代码部分和一个单独使用编码器的代码部分。 因此,对接口编码部分的更改将强制对解码部分进行不必要的重新编译,反之亦然。
对于包含头文件的 c/c++ 等来说是正确的。
搜索可靠的原则并查看接口隔离原则。
Only thing is that you usually have one code part that will use the decoder and a separate using the encoder. So a change to the encoding part of the interface will force an unnecessary recompile of the decoding part and vice versa.
True for c/c++ etc. with header file include.
Search for solid principles and see Interface Segregation Principle.
好吧,您可以有两个单独的接口,然后是另一个将它们组合在一起的接口。 这将使能够更容易地为两者声明单个参数,例如,
这实际上取决于您设想使用一个部分而不是另一个部分的频率。 大多数时候,我发现编码/解码的东西我需要两个部分都可用,所以我可能只声明一个具有这两种方法的接口 - 但这确实取决于具体情况。
Well, you could have two separate interfaces, and then another interface which combines them. That would make it easier to be able to declare a single parameter for both, e.g.
It really depends on how often you envisage using one part but not the other. Most of the time I find that encoding/decoding stuff I need both parts available, so I'd probably just declare one interface with both methods - but it does depend on the exact situation.
将它们放在同一个接口中的缺点是它强制您的实现在单个类中既是编码器又是解码器。 目前看来这似乎是合理的,但可能并不总是如此。 所以我会问自己这是否应该是一个要求/是否可取?
The disadvantage of having them in the same interface is that it forces your implementations to be both encoders and decoders in a single class. This may seem reasonable currently, but it may not always be that way. So I would ask myself if this should be a requirement/is desirable ?
将它们分开会更加灵活。 如果您编写单独的接口,则始终可以将它们组合成第三个接口,以便在需要编码和解码功能时使用。
反之则不然。 如果您从一开始就编写一个接口,那么您将失去选择仅包含编码或解码函数的灵活性。
Making them separate is a lot more flexible. If you write separate interfaces you can always combine them into a third interface that you use whenever you need both encode and decode functions.
The reverse is not true. If you write one interface from the start, you lose the flexibility to choose to include only encode or decode functions.
通常您会一起使用它们,但有时不会。
这取决于什么对你来说更自然。
顺便说一句:如果您单独定义它们,您仍然可以一起使用它们
例如
Typically you would use them together, but some time not.
It depends on what is more natural for you.
BTW: If you define them separately you can still use them together
e.g.
考虑不使用单独的编码器/解码器接口,而是
consider not having a separate encoder/decoder interface but instead
取决于应用程序。
如果通常在同一个二进制文件中有一个编码器和一个解码器,那么一个接口就可以了。 如果它们通常是独立的(例如,捕获应用程序仅编码,管理应用程序仅解码),请使用单独的接口。
Depends on the application.
If normally you have an encoder and an decoder in the same binary, one interface is fine. If they are usually separate (e.g. a capture application only encoding, a management application only decoding), use separate interfaces.