简体   繁体   English

单独的解码/编码接口或在一个接口中

[英]Separate decode/encode interfaces or in one interface

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. 我现在面临的设计问题是编码器和解码器API是应该在一个接口中还是在单独的接口中。 eg Apache MINA uses separate interfaces 例如,Apache MINA使用单独的接口

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 ? 有什么想法吗?

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. 因此实现是共享的,但是用户代码将编码器和解码器视为独立且独立的概念。

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. 对于带有头文件include的c / c ++等是真的。

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, eg 这样可以更容易地为两者声明单个参数,例如

private IEncoder encoder;
private IDecoder decoder;

public ThingWhichUsesEncodeAndDecode(IEncoder encoder, IDecoder decoder)
{
    this.encoder = encoder;
    this.decoder = decoder;
}

public ThingWhichUsesEncodeAndDecode(IEncoderDecoder both)
{
    this(both, both);
}

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 eg 顺便说一句:如果你单独定义它们,你仍然可以一起使用它们,例如

interface Converter extends Decoder, Encoder { }

consider not having a separate encoder/decoder interface but instead 考虑没有单独的编码器/解码器接口,而是考虑

interface Encodable
{
    Decodable encode();
}
interface Decodable
{
    Encodable decode();
}
class A implements Encodable;
class B implements Decodable;

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 (eg a capture application only encoding, a management application only decoding), use separate interfaces. 如果它们通常是分开的(例如,仅捕获应用程序编码,管理应用程序仅解码),则使用单独的接口。

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

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